import 'dart:async'; import 'dart:ffi'; import 'dart:ui'; import 'package:ffi/ffi.dart'; import 'package:flutter/material.dart'; import 'package:native_vision/native/doc_scanner.dart'; class Coordinate extends Struct { @Float() external double x; @Float() external double y; } class CoordinateArray extends Struct { external Pointer coordsPtr; @Uint32() external int length; } class EdgeDetectionResult { EdgeDetectionResult({ required this.topLeft, required this.topRight, required this.bottomLeft, required this.bottomRight, }); Offset topLeft; Offset topRight; Offset bottomLeft; Offset bottomRight; Pointer toNativeCoords() { final coordsPtr = CoordinateArrayExtention.createCoordsPtr(); coordsPtr.ref.coordsPtr[0].x = topLeft.dx; coordsPtr.ref.coordsPtr[0].y = topLeft.dy; coordsPtr.ref.coordsPtr[1].x = topRight.dx; coordsPtr.ref.coordsPtr[1].y = topRight.dy; coordsPtr.ref.coordsPtr[3].x = bottomRight.dx; coordsPtr.ref.coordsPtr[3].y = bottomRight.dy; coordsPtr.ref.coordsPtr[2].x = bottomLeft.dx; coordsPtr.ref.coordsPtr[2].y = bottomLeft.dy; return coordsPtr; } } extension CoordinateArrayExtention on Pointer { static Pointer createCoordsPtr() { var bytes = sizeOf(); final coordArrPtr = malloc.allocate(bytes); bytes = sizeOf(); coordArrPtr.ref.coordsPtr = malloc.allocate(bytes * 4); coordArrPtr.ref.length = 4; return coordArrPtr; } EdgeDetectionResult toEdgeDetectionResult() { return EdgeDetectionResult( topLeft: Offset(ref.coordsPtr[0].x, ref.coordsPtr[0].y), topRight: Offset(ref.coordsPtr[1].x, ref.coordsPtr[1].y), bottomLeft: Offset(ref.coordsPtr[3].x, ref.coordsPtr[3].y), bottomRight: Offset(ref.coordsPtr[2].x, ref.coordsPtr[2].y)); } void release() { malloc.free(ref.coordsPtr); malloc.free(this); } } class ROI { // Below is pixel unit int left; int top; int width; int height; //Below is logical unit double maxWidth; double maxHeight; ROI.create(this.left, this.top, this.width, this.height, this.maxWidth, this.maxHeight); Pointer toNativeROI() { final roiPtr = malloc.allocate(sizeOf()); roiPtr.ref.left = left; roiPtr.ref.top = top; roiPtr.ref.width = width; roiPtr.ref.height = height; return roiPtr; } }