native_so.dart 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import 'dart:ffi';
  2. import 'package:ffi/ffi.dart';
  3. import 'package:flutter/services.dart' show rootBundle;
  4. import 'id_card_recognition.dart';
  5. // 定义 StructImageInfo 结构体
  6. class StructImageInfo extends Struct {
  7. @Int32()
  8. external int width;
  9. @Int32()
  10. external int height;
  11. @Int32()
  12. external int colorType;
  13. external Pointer<Uint8> dataPointer;
  14. }
  15. class StructModelDataInfor extends Struct {
  16. external Pointer detData;
  17. @Int()
  18. external int detLen;
  19. external Pointer recData;
  20. @Int()
  21. external int recLen;
  22. external Pointer recKeysData;
  23. @Int()
  24. external int recKeysLen;
  25. external Pointer clsData;
  26. @Int()
  27. external int clsLen;
  28. }
  29. // 定义 IDCardRecogResult 结构体
  30. class IDCardRecogResult extends Struct {
  31. @Int32()
  32. // ignore: non_constant_identifier_names
  33. external int numerStatus;
  34. external Pointer<Utf8> name;
  35. external Pointer<Utf8> idNumber;
  36. external Pointer<Utf8> gender;
  37. external Pointer<Utf8> nation;
  38. external Pointer<Utf8> birthdate;
  39. external Pointer<Utf8> address;
  40. @Float()
  41. external double timeSpan;
  42. }
  43. class StructModelBuffer extends Struct {
  44. external Pointer<Uint8> bufferPtr;
  45. @Int32()
  46. external int bufferLength;
  47. }
  48. class TypeInfo {
  49. // 定义 TypeInfo 结构体
  50. }
  51. // 定义 C++ 函数的接口
  52. typedef CreateIDCardRecognitionC = Pointer Function();
  53. typedef CreateIDCardRecognitionDart = Pointer Function();
  54. // 传输模型接口
  55. typedef LoadNetWorksC = Bool Function(Pointer, StructModelDataInfor);
  56. typedef LoadNetWorksDart = bool Function(Pointer, StructModelDataInfor);
  57. // EvaluateOneImage
  58. typedef EvaluateOneImageC = IDCardRecogResult Function(
  59. Pointer, StructImageInfo);
  60. typedef EvaluateOneImageDart = IDCardRecogResult Function(
  61. Pointer, StructImageInfo);
  62. //release
  63. typedef ReleaseIDCardRecognitionC = Void Function(Pointer);
  64. typedef ReleaseIDCardRecognitionDart = void Function(Pointer);
  65. class IDCardRecognitionSoLib {
  66. static final IDCardRecognitionSoLib _instance =
  67. IDCardRecognitionSoLib._privateConstructor();
  68. factory IDCardRecognitionSoLib() {
  69. return _instance;
  70. }
  71. late DynamicLibrary _libIDCardRecognitionSo;
  72. IDCardRecognitionSoLib._privateConstructor() {
  73. _libIDCardRecognitionSo = DynamicLibrary.open('libIDCardRecognition.so');
  74. }
  75. Pointer createIDCardRecognition() {
  76. final createIDCardRecognition = _libIDCardRecognitionSo.lookupFunction<
  77. CreateIDCardRecognitionC,
  78. CreateIDCardRecognitionDart>('CreateIDCardRecognition');
  79. var result = Pointer.fromAddress(0);
  80. result = createIDCardRecognition();
  81. final idCard = result.cast();
  82. return idCard;
  83. }
  84. EvaluateOneImageDart evaluateOneImage() {
  85. final EvaluateOneImageDart evaluateOneImageDart = _libIDCardRecognitionSo
  86. .lookupFunction<EvaluateOneImageC, EvaluateOneImageDart>(
  87. 'EvaluateOneImage');
  88. return evaluateOneImageDart;
  89. }
  90. ReleaseIDCardRecognitionDart releaseIDCardRecognition() {
  91. final releaseIDCardRecognition = _libIDCardRecognitionSo.lookupFunction<
  92. ReleaseIDCardRecognitionC,
  93. ReleaseIDCardRecognitionDart>("ReleaseIDCardRecognition");
  94. return releaseIDCardRecognition;
  95. }
  96. LoadNetWorksDart loadNetWorks() {
  97. final LoadNetWorksDart loadNetWorks = _libIDCardRecognitionSo
  98. .lookupFunction<LoadNetWorksC, LoadNetWorksDart>('LoadNetWorks');
  99. return loadNetWorks;
  100. }
  101. // 定义 Dart 函数来加载模型数据信息
  102. Future<StructModelDataInfor> loadModelDataInfor(
  103. ModelDataInformation modelDataInfo) async {
  104. final ocrv4DetInfer = modelDataInfo.ocrv4DetInfer;
  105. final detBuffer = await getModelBuffer(ocrv4DetInfer);
  106. final ocrv4RecInfer = modelDataInfo.ocrv4RecInfer;
  107. final recBuffer = await getModelBuffer(ocrv4RecInfer);
  108. final ppocrKeysV1 = modelDataInfo.ppocrKeysV1;
  109. final recKeyBuffer = await getModelBuffer(ppocrKeysV1);
  110. final ppocrMobileClsTrain = modelDataInfo.ppocrMobileClsTrain;
  111. final clsBuffer = await getModelBuffer(ppocrMobileClsTrain);
  112. final modelDataInfor = calloc<StructModelDataInfor>();
  113. modelDataInfor.ref.detData = detBuffer.bufferPtr;
  114. modelDataInfor.ref.detLen = detBuffer.bufferLength;
  115. modelDataInfor.ref.recData = recBuffer.bufferPtr;
  116. modelDataInfor.ref.recLen = recBuffer.bufferLength;
  117. modelDataInfor.ref.recKeysData = recKeyBuffer.bufferPtr;
  118. modelDataInfor.ref.recKeysLen = recKeyBuffer.bufferLength;
  119. modelDataInfor.ref.clsData = clsBuffer.bufferPtr;
  120. modelDataInfor.ref.clsLen = clsBuffer.bufferLength;
  121. return modelDataInfor.ref;
  122. }
  123. Future<StructModelBuffer> getModelBuffer(String filePath) async {
  124. final structModelBuffer = calloc<StructModelBuffer>();
  125. try {
  126. final fileData = await rootBundle.load(filePath);
  127. final buffer = fileData.buffer.asUint8List();
  128. final bufferLength = buffer.length;
  129. final bufferPtr = calloc<Uint8>(bufferLength);
  130. final bufferPtrTyped = bufferPtr.asTypedList(bufferLength);
  131. bufferPtrTyped.setAll(0, buffer);
  132. structModelBuffer.ref.bufferPtr = bufferPtr;
  133. structModelBuffer.ref.bufferLength = bufferLength;
  134. } catch (e) {
  135. print(e);
  136. }
  137. return structModelBuffer.ref;
  138. }
  139. }