123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- import 'dart:ffi';
- import 'package:ffi/ffi.dart';
- import 'package:flutter/services.dart' show rootBundle;
- import 'id_card_recognition.dart';
- // 定义 StructImageInfo 结构体
- class StructImageInfo extends Struct {
- @Int32()
- external int width;
- @Int32()
- external int height;
- @Int32()
- external int colorType;
- external Pointer<Uint8> dataPointer;
- }
- class StructModelDataInfor extends Struct {
- external Pointer detData;
- @Int()
- external int detLen;
- external Pointer recData;
- @Int()
- external int recLen;
- external Pointer recKeysData;
- @Int()
- external int recKeysLen;
- external Pointer clsData;
- @Int()
- external int clsLen;
- }
- // 定义 IDCardRecogResult 结构体
- class IDCardRecogResult extends Struct {
- @Int32()
- // ignore: non_constant_identifier_names
- external int numerStatus;
- external Pointer<Utf8> name;
- external Pointer<Utf8> idNumber;
- external Pointer<Utf8> gender;
- external Pointer<Utf8> nation;
- external Pointer<Utf8> birthdate;
- external Pointer<Utf8> address;
- @Float()
- external double timeSpan;
- }
- class StructModelBuffer extends Struct {
- external Pointer<Uint8> bufferPtr;
- @Int32()
- external int bufferLength;
- }
- class TypeInfo {
- // 定义 TypeInfo 结构体
- }
- // 定义 C++ 函数的接口
- typedef CreateIDCardRecognitionC = Pointer Function();
- typedef CreateIDCardRecognitionDart = Pointer Function();
- // 传输模型接口
- typedef LoadNetWorksC = Bool Function(Pointer, StructModelDataInfor);
- typedef LoadNetWorksDart = bool Function(Pointer, StructModelDataInfor);
- // EvaluateOneImage
- typedef EvaluateOneImageC = IDCardRecogResult Function(
- Pointer, StructImageInfo);
- typedef EvaluateOneImageDart = IDCardRecogResult Function(
- Pointer, StructImageInfo);
- //release
- typedef ReleaseIDCardRecognitionC = Void Function(Pointer);
- typedef ReleaseIDCardRecognitionDart = void Function(Pointer);
- class IDCardRecognitionSoLib {
- static final IDCardRecognitionSoLib _instance =
- IDCardRecognitionSoLib._privateConstructor();
- factory IDCardRecognitionSoLib() {
- return _instance;
- }
- late DynamicLibrary _libIDCardRecognitionSo;
- IDCardRecognitionSoLib._privateConstructor() {
- _libIDCardRecognitionSo = DynamicLibrary.open('libIDCardRecognition.so');
- }
- Pointer createIDCardRecognition() {
- final createIDCardRecognition = _libIDCardRecognitionSo.lookupFunction<
- CreateIDCardRecognitionC,
- CreateIDCardRecognitionDart>('CreateIDCardRecognition');
- var result = Pointer.fromAddress(0);
- result = createIDCardRecognition();
- final idCard = result.cast();
- return idCard;
- }
- EvaluateOneImageDart evaluateOneImage() {
- final EvaluateOneImageDart evaluateOneImageDart = _libIDCardRecognitionSo
- .lookupFunction<EvaluateOneImageC, EvaluateOneImageDart>(
- 'EvaluateOneImage');
- return evaluateOneImageDart;
- }
- ReleaseIDCardRecognitionDart releaseIDCardRecognition() {
- final releaseIDCardRecognition = _libIDCardRecognitionSo.lookupFunction<
- ReleaseIDCardRecognitionC,
- ReleaseIDCardRecognitionDart>("ReleaseIDCardRecognition");
- return releaseIDCardRecognition;
- }
- LoadNetWorksDart loadNetWorks() {
- final LoadNetWorksDart loadNetWorks = _libIDCardRecognitionSo
- .lookupFunction<LoadNetWorksC, LoadNetWorksDart>('LoadNetWorks');
- return loadNetWorks;
- }
- // 定义 Dart 函数来加载模型数据信息
- Future<StructModelDataInfor> loadModelDataInfor(
- ModelDataInformation modelDataInfo) async {
- final ocrv4DetInfer = modelDataInfo.ocrv4DetInfer;
- final detBuffer = await getModelBuffer(ocrv4DetInfer);
- final ocrv4RecInfer = modelDataInfo.ocrv4RecInfer;
- final recBuffer = await getModelBuffer(ocrv4RecInfer);
- final ppocrKeysV1 = modelDataInfo.ppocrKeysV1;
- final recKeyBuffer = await getModelBuffer(ppocrKeysV1);
- final ppocrMobileClsTrain = modelDataInfo.ppocrMobileClsTrain;
- final clsBuffer = await getModelBuffer(ppocrMobileClsTrain);
- final modelDataInfor = calloc<StructModelDataInfor>();
- modelDataInfor.ref.detData = detBuffer.bufferPtr;
- modelDataInfor.ref.detLen = detBuffer.bufferLength;
- modelDataInfor.ref.recData = recBuffer.bufferPtr;
- modelDataInfor.ref.recLen = recBuffer.bufferLength;
- modelDataInfor.ref.recKeysData = recKeyBuffer.bufferPtr;
- modelDataInfor.ref.recKeysLen = recKeyBuffer.bufferLength;
- modelDataInfor.ref.clsData = clsBuffer.bufferPtr;
- modelDataInfor.ref.clsLen = clsBuffer.bufferLength;
- return modelDataInfor.ref;
- }
- Future<StructModelBuffer> getModelBuffer(String filePath) async {
- final structModelBuffer = calloc<StructModelBuffer>();
- try {
- final fileData = await rootBundle.load(filePath);
- final buffer = fileData.buffer.asUint8List();
- final bufferLength = buffer.length;
- final bufferPtr = calloc<Uint8>(bufferLength);
- final bufferPtrTyped = bufferPtr.asTypedList(bufferLength);
- bufferPtrTyped.setAll(0, buffer);
- structModelBuffer.ref.bufferPtr = bufferPtr;
- structModelBuffer.ref.bufferLength = bufferLength;
- } catch (e) {
- print(e);
- }
- return structModelBuffer.ref;
- }
- }
|