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 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 name; external Pointer idNumber; external Pointer gender; external Pointer nation; external Pointer birthdate; external Pointer address; @Float() external double timeSpan; } class StructModelBuffer extends Struct { external Pointer 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( 'EvaluateOneImage'); return evaluateOneImageDart; } ReleaseIDCardRecognitionDart releaseIDCardRecognition() { final releaseIDCardRecognition = _libIDCardRecognitionSo.lookupFunction< ReleaseIDCardRecognitionC, ReleaseIDCardRecognitionDart>("ReleaseIDCardRecognition"); return releaseIDCardRecognition; } LoadNetWorksDart loadNetWorks() { final LoadNetWorksDart loadNetWorks = _libIDCardRecognitionSo .lookupFunction('LoadNetWorks'); return loadNetWorks; } // 定义 Dart 函数来加载模型数据信息 Future 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(); 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 getModelBuffer(String filePath) async { final structModelBuffer = calloc(); try { final fileData = await rootBundle.load(filePath); final buffer = fileData.buffer.asUint8List(); final bufferLength = buffer.length; final bufferPtr = calloc(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; } }