edge_detection.dart 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import 'dart:async';
  2. import 'dart:ffi';
  3. import 'dart:ui';
  4. import 'package:ffi/ffi.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:native_vision/native/doc_scanner.dart';
  7. class Coordinate extends Struct {
  8. @Float()
  9. external double x;
  10. @Float()
  11. external double y;
  12. }
  13. class CoordinateArray extends Struct {
  14. external Pointer<Coordinate> coordsPtr;
  15. @Uint32()
  16. external int length;
  17. }
  18. class EdgeDetectionResult {
  19. EdgeDetectionResult({
  20. required this.topLeft,
  21. required this.topRight,
  22. required this.bottomLeft,
  23. required this.bottomRight,
  24. });
  25. Offset topLeft;
  26. Offset topRight;
  27. Offset bottomLeft;
  28. Offset bottomRight;
  29. Pointer<CoordinateArray> toNativeCoords() {
  30. final coordsPtr = CoordinateArrayExtention.createCoordsPtr();
  31. coordsPtr.ref.coordsPtr[0].x = topLeft.dx;
  32. coordsPtr.ref.coordsPtr[0].y = topLeft.dy;
  33. coordsPtr.ref.coordsPtr[1].x = topRight.dx;
  34. coordsPtr.ref.coordsPtr[1].y = topRight.dy;
  35. coordsPtr.ref.coordsPtr[3].x = bottomRight.dx;
  36. coordsPtr.ref.coordsPtr[3].y = bottomRight.dy;
  37. coordsPtr.ref.coordsPtr[2].x = bottomLeft.dx;
  38. coordsPtr.ref.coordsPtr[2].y = bottomLeft.dy;
  39. return coordsPtr;
  40. }
  41. }
  42. extension CoordinateArrayExtention on Pointer<CoordinateArray> {
  43. static Pointer<CoordinateArray> createCoordsPtr() {
  44. var bytes = sizeOf<CoordinateArray>();
  45. final coordArrPtr = malloc.allocate<CoordinateArray>(bytes);
  46. bytes = sizeOf<Coordinate>();
  47. coordArrPtr.ref.coordsPtr = malloc.allocate(bytes * 4);
  48. coordArrPtr.ref.length = 4;
  49. return coordArrPtr;
  50. }
  51. EdgeDetectionResult toEdgeDetectionResult() {
  52. return EdgeDetectionResult(
  53. topLeft: Offset(ref.coordsPtr[0].x, ref.coordsPtr[0].y),
  54. topRight: Offset(ref.coordsPtr[1].x, ref.coordsPtr[1].y),
  55. bottomLeft: Offset(ref.coordsPtr[3].x, ref.coordsPtr[3].y),
  56. bottomRight: Offset(ref.coordsPtr[2].x, ref.coordsPtr[2].y));
  57. }
  58. void release() {
  59. malloc.free(ref.coordsPtr);
  60. malloc.free(this);
  61. }
  62. }
  63. class ROI {
  64. // Below is pixel unit
  65. int left;
  66. int top;
  67. int width;
  68. int height;
  69. //Below is logical unit
  70. double maxWidth;
  71. double maxHeight;
  72. ROI.create(this.left, this.top, this.width, this.height, this.maxWidth,
  73. this.maxHeight);
  74. Pointer<NativeROI> toNativeROI() {
  75. final roiPtr = malloc.allocate<NativeROI>(sizeOf<NativeROI>());
  76. roiPtr.ref.left = left;
  77. roiPtr.ref.top = top;
  78. roiPtr.ref.width = width;
  79. roiPtr.ref.height = height;
  80. return roiPtr;
  81. }
  82. }