1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- import 'dart:ffi';
- import 'dart:io';
- import 'package:ffi/ffi.dart';
- import 'package:flutter/foundation.dart';
- import 'package:flutter/services.dart';
- import 'package:path_provider/path_provider.dart';
- import 'dynamic_lib_singleton.dart';
- class DocCleanFilter {
- static final DynamicLibrary _nativeVision = DynamicLibSingleton().dl;
- // static final int Function(Pointer<Uint8> src, Pointer<Pointer<Uint8>> dst,
- // int width, int height) _nativeAdd =
- // _nativeVision
- // .lookup<
- // NativeFunction<
- // Int32 Function(Pointer<Uint8>, Pointer<Pointer<Uint8>>, Int32,
- // Int32)>>('native_add')
- // .asFunction();
- static final int Function(Pointer<Utf8> modelPath) _nativeInit = _nativeVision
- .lookup<NativeFunction<IntPtr Function(Pointer<Utf8>)>>('init')
- .asFunction();
- static final void Function(int docFilterPtr) _nativeRelease = _nativeVision
- .lookup<NativeFunction<Void Function(IntPtr)>>('release')
- .asFunction();
- static final int Function(
- Pointer<Utf8> imgPath, Pointer<Utf8> dstImgPath, int docFilterAdr)
- _nativeProc = _nativeVision
- .lookup<
- NativeFunction<
- Int32 Function(
- Pointer<Utf8>, Pointer<Utf8>, IntPtr)>>('process')
- .asFunction();
- static Future<String> _getModelPath() async {
- const modelName = "ssim_0.85_psnr_22.12_model_4.tflite";
- // const modelName = "ssim_0.87_psnr_23.88_model_50.tflite";
- final tempDir = await getTemporaryDirectory();
- final modelCachePath = "${tempDir.path}/$modelName";
- final file = File(modelCachePath);
- final exists = await file.exists();
- if (!exists) {
- final modelData =
- await rootBundle.load("packages/native_vision/assets/$modelName");
- final buffer = modelData.buffer;
- final result = await file.writeAsBytes(
- buffer.asUint8List(modelData.offsetInBytes, modelData.lengthInBytes));
- }
- return file.path;
- }
- static int _initDocModel(String modelPath) {
- final modelPathPtr = modelPath.toNativeUtf8();
- final docfilterPtr = _nativeInit(modelPathPtr);
- malloc.free(modelPathPtr);
- return docfilterPtr;
- }
- static Future<int> init() async {
- final modelPath = await _getModelPath();
- final docfilterPtr = await compute(_initDocModel, modelPath);
- return docfilterPtr;
- }
- static String _processAsync(args) {
- String origImgPath = args["origImgPath"];
- int docFilterPtr = args["docFilterPtr"];
- final origImgPathPtr = origImgPath.toNativeUtf8();
- final dstImgPath =
- "${File(origImgPath).parent.path}/processed_${DateTime.now().millisecondsSinceEpoch}.jpg";
- final dstImgPathPtr = dstImgPath.toNativeUtf8();
- final succ = _nativeProc(origImgPathPtr, dstImgPathPtr, docFilterPtr);
- malloc.free(origImgPathPtr);
- malloc.free(dstImgPathPtr);
- return succ == 1 ? dstImgPath : "";
- }
- static Future<String> process(String origImgPath, int docFilterPtr) async {
- Map args = {"docFilterPtr": docFilterPtr, "origImgPath": origImgPath};
- return await compute(_processAsync, args);
- }
- static Future<void> _release(int docFilterPtr) async {
- _nativeRelease(docFilterPtr);
- }
- static Future<void> release(int docFilterPtr) async {
- return await compute(_release, docFilterPtr);
- }
- }
|