123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- 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<Coordinate> 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<CoordinateArray> 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<CoordinateArray> {
- static Pointer<CoordinateArray> createCoordsPtr() {
- var bytes = sizeOf<CoordinateArray>();
- final coordArrPtr = malloc.allocate<CoordinateArray>(bytes);
- bytes = sizeOf<Coordinate>();
- 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<NativeROI> toNativeROI() {
- final roiPtr = malloc.allocate<NativeROI>(sizeOf<NativeROI>());
- roiPtr.ref.left = left;
- roiPtr.ref.top = top;
- roiPtr.ref.width = width;
- roiPtr.ref.height = height;
- return roiPtr;
- }
- }
|