123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- 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';
- 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<bool> 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<IDCardRecogResultInfo?> 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<Uint8> imageDataPtr = calloc(pixels.lengthInBytes);
- imageDataPtr.asTypedList(pixels.lengthInBytes).setAll(0, pixels);
- final structImageInfo = calloc<StructImageInfo>();
- structImageInfo.ref.width = width;
- structImageInfo.ref.colorType = 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<Uint8> imageDataPtr, ui.Image image) async {
-
-
- 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";
-
-
-
-
- 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());
-
- }
- String _addLeadingZero(int value) {
- return value.toString().padLeft(2, '0');
- }
- void release() {
- _releaseIDCardRecognitionDart(_hIDCardRecognitionPtr);
- }
- }
|