id_card_recognition.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import 'dart:ffi';
  2. import 'dart:io';
  3. import 'dart:typed_data';
  4. import 'package:ffi/ffi.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:path_provider/path_provider.dart';
  7. import 'dart:ui' as ui;
  8. import 'native_so.dart';
  9. // 定义 IDCardRecogResult 结构体
  10. class IDCardRecogResultInfo {
  11. int numerStatus;
  12. String? name;
  13. String? idNumber;
  14. String? gender;
  15. String? nation;
  16. String? birthdate;
  17. String? address;
  18. IDCardRecogResultInfo({
  19. this.numerStatus = 0,
  20. this.name,
  21. this.idNumber,
  22. this.gender,
  23. this.nation,
  24. this.birthdate,
  25. this.address,
  26. });
  27. }
  28. class ModelDataInformation {
  29. final String ocrv4DetInfer;
  30. final String ocrv4RecInfer;
  31. final String ppocrKeysV1;
  32. final String ppocrMobileClsTrain;
  33. ModelDataInformation({
  34. required this.ocrv4DetInfer,
  35. required this.ocrv4RecInfer,
  36. required this.ppocrKeysV1,
  37. required this.ppocrMobileClsTrain,
  38. });
  39. }
  40. class IDCardRecognition {
  41. late Pointer _hIDCardRecognitionPtr;
  42. late IDCardRecognitionSoLib _idCardRecognitionSo;
  43. late EvaluateOneImageDart _evaluateOneImageDart;
  44. late ReleaseIDCardRecognitionDart _releaseIDCardRecognitionDart;
  45. IDCardRecognition() {
  46. _idCardRecognitionSo = IDCardRecognitionSoLib();
  47. }
  48. Future<bool> initiate(ModelDataInformation modelDataInfo) async {
  49. try {
  50. _hIDCardRecognitionPtr = _idCardRecognitionSo.createIDCardRecognition();
  51. var loadNetWorks = _idCardRecognitionSo.loadNetWorks();
  52. var structModelDataInfor =
  53. await _idCardRecognitionSo.loadModelDataInfor(modelDataInfo);
  54. var result = loadNetWorks(_hIDCardRecognitionPtr, structModelDataInfor);
  55. if (!result) {
  56. return false;
  57. }
  58. _evaluateOneImageDart = _idCardRecognitionSo.evaluateOneImage();
  59. _releaseIDCardRecognitionDart =
  60. _idCardRecognitionSo.releaseIDCardRecognition();
  61. } catch (e) {
  62. return false;
  63. }
  64. return true;
  65. }
  66. Future<IDCardRecogResultInfo?> evaluateOneImage(ui.Image image) async {
  67. try {
  68. int width = image.width;
  69. int height = image.height;
  70. Uint8List pixels =
  71. await image.toByteData().then((value) => value!.buffer.asUint8List());
  72. Pointer<Uint8> imageDataPtr = calloc(pixels.lengthInBytes);
  73. imageDataPtr.asTypedList(pixels.lengthInBytes).setAll(0, pixels);
  74. final structImageInfo = calloc<StructImageInfo>();
  75. structImageInfo.ref.width = width;
  76. structImageInfo.ref.colorType = 5; //默认写5
  77. structImageInfo.ref.height = height;
  78. structImageInfo.ref.dataPointer = imageDataPtr;
  79. final result =
  80. _evaluateOneImageDart(_hIDCardRecognitionPtr, structImageInfo.ref);
  81. int numerStatus = result.numerStatus;
  82. var idCardRecogResultInfo =
  83. IDCardRecogResultInfo(numerStatus: numerStatus);
  84. if (numerStatus == 1) {
  85. idCardRecogResultInfo.name = result.name.toDartString();
  86. idCardRecogResultInfo.idNumber = result.idNumber.toDartString();
  87. idCardRecogResultInfo.gender = result.gender.toDartString();
  88. idCardRecogResultInfo.nation = result.nation.toDartString();
  89. idCardRecogResultInfo.address = result.address.toDartString();
  90. idCardRecogResultInfo.birthdate = result.birthdate.toDartString();
  91. }
  92. writeDatatoFile(pixels, imageDataPtr, image);
  93. return idCardRecogResultInfo;
  94. } catch (e) {
  95. return null;
  96. }
  97. }
  98. void writeDatatoFile(
  99. Uint8List uint8List, Pointer<Uint8> imageDataPtr, ui.Image image) async {
  100. // List<int> imageDatas =
  101. // imageDataPtr.asTypedList(uint8List.lengthInBytes); //测试用。,记得删
  102. DateTime now = DateTime.now();
  103. String filePath =
  104. '${now.year}${_addLeadingZero(now.month)}${_addLeadingZero(now.day)}_${_addLeadingZero(now.hour)}${_addLeadingZero(now.minute)}';
  105. final directory = await getExternalStorageDirectory();
  106. final path = "${directory?.path}/testImage";
  107. // String fileName = '$filePath.txt';
  108. // final destFile = File('$path/$fileName');
  109. // destFile.writeAsString(imageDatas.toString());
  110. // await destFile.create(recursive: true);
  111. if (!await Directory(path).exists()) {
  112. await Directory(path).create(recursive: true);
  113. }
  114. String imageName = '$filePath.png';
  115. var bytes = await image.toByteData(format: ui.ImageByteFormat.png);
  116. final imageFile = File('$path/$imageName');
  117. imageFile.writeAsBytes(bytes!.buffer.asInt8List());
  118. // print(imageDatas);
  119. }
  120. String _addLeadingZero(int value) {
  121. return value.toString().padLeft(2, '0');
  122. }
  123. void release() {
  124. _releaseIDCardRecognitionDart(_hIDCardRecognitionPtr);
  125. }
  126. }