import 'dart:ffi'; import 'dart:io'; import 'dart:typed_data'; import 'package:ffi/ffi.dart'; import 'package:flutter/services.dart'; import 'package:path_provider/path_provider.dart'; import 'dart:ui' as ui; import 'native_so.dart'; // 定义 IDCardRecogResult 结构体 class IDCardRecogResultInfo { int numerStatus; String? name; String? idNumber; String? gender; String? nation; String? birthdate; String? address; IDCardRecogResultInfo({ this.numerStatus = 0, this.name, this.idNumber, this.gender, this.nation, this.birthdate, this.address, }); } class ModelDataInformation { final String ocrv4DetInfer; final String ocrv4RecInfer; final String ppocrKeysV1; final String ppocrMobileClsTrain; ModelDataInformation({ required this.ocrv4DetInfer, required this.ocrv4RecInfer, required this.ppocrKeysV1, required this.ppocrMobileClsTrain, }); } class IDCardRecognition { late Pointer _hIDCardRecognitionPtr; late IDCardRecognitionSoLib _idCardRecognitionSo; late EvaluateOneImageDart _evaluateOneImageDart; late ReleaseIDCardRecognitionDart _releaseIDCardRecognitionDart; IDCardRecognition() { _idCardRecognitionSo = IDCardRecognitionSoLib(); } Future initiate(ModelDataInformation modelDataInfo) async { try { _hIDCardRecognitionPtr = _idCardRecognitionSo.createIDCardRecognition(); var loadNetWorks = _idCardRecognitionSo.loadNetWorks(); var structModelDataInfor = await _idCardRecognitionSo.loadModelDataInfor(modelDataInfo); var result = loadNetWorks(_hIDCardRecognitionPtr, structModelDataInfor); if (!result) { return false; } _evaluateOneImageDart = _idCardRecognitionSo.evaluateOneImage(); _releaseIDCardRecognitionDart = _idCardRecognitionSo.releaseIDCardRecognition(); } catch (e) { return false; } return true; } Future evaluateOneImage(ui.Image image) async { try { int width = image.width; int height = image.height; Uint8List pixels = await image.toByteData().then((value) => value!.buffer.asUint8List()); Pointer imageDataPtr = calloc(pixels.lengthInBytes); imageDataPtr.asTypedList(pixels.lengthInBytes).setAll(0, pixels); final structImageInfo = calloc(); structImageInfo.ref.width = width; structImageInfo.ref.colorType = 5; //默认写5 structImageInfo.ref.height = height; structImageInfo.ref.dataPointer = imageDataPtr; final result = _evaluateOneImageDart(_hIDCardRecognitionPtr, structImageInfo.ref); int numerStatus = result.numerStatus; var idCardRecogResultInfo = IDCardRecogResultInfo(numerStatus: numerStatus); if (numerStatus == 1) { idCardRecogResultInfo.name = result.name.toDartString(); idCardRecogResultInfo.idNumber = result.idNumber.toDartString(); idCardRecogResultInfo.gender = result.gender.toDartString(); idCardRecogResultInfo.nation = result.nation.toDartString(); idCardRecogResultInfo.address = result.address.toDartString(); idCardRecogResultInfo.birthdate = result.birthdate.toDartString(); } writeDatatoFile(pixels, imageDataPtr, image); return idCardRecogResultInfo; } catch (e) { return null; } } void writeDatatoFile( Uint8List uint8List, Pointer imageDataPtr, ui.Image image) async { // List imageDatas = // imageDataPtr.asTypedList(uint8List.lengthInBytes); //测试用。,记得删 DateTime now = DateTime.now(); String filePath = '${now.year}${_addLeadingZero(now.month)}${_addLeadingZero(now.day)}_${_addLeadingZero(now.hour)}${_addLeadingZero(now.minute)}'; final directory = await getExternalStorageDirectory(); final path = "${directory?.path}/testImage"; // String fileName = '$filePath.txt'; // final destFile = File('$path/$fileName'); // destFile.writeAsString(imageDatas.toString()); // await destFile.create(recursive: true); if (!await Directory(path).exists()) { await Directory(path).create(recursive: true); } String imageName = '$filePath.png'; var bytes = await image.toByteData(format: ui.ImageByteFormat.png); final imageFile = File('$path/$imageName'); imageFile.writeAsBytes(bytes!.buffer.asInt8List()); // print(imageDatas); } String _addLeadingZero(int value) { return value.toString().padLeft(2, '0'); } void release() { _releaseIDCardRecognitionDart(_hIDCardRecognitionPtr); } }