rn-opencv-doc-perspective-correction 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/index.js +25 -25
  2. package/package.json +1 -1
  3. package/src/index.ts +30 -30
package/dist/index.js CHANGED
@@ -35,15 +35,15 @@ class DocumentScanner {
35
35
  let hierarchyObj = null;
36
36
  try {
37
37
  src = react_native_fast_opencv_1.OpenCV.base64ToMat(imageBase64);
38
- gray = react_native_fast_opencv_1.OpenCV.invoke('Mat');
39
- blurred = react_native_fast_opencv_1.OpenCV.invoke('Mat');
40
- edges = react_native_fast_opencv_1.OpenCV.invoke('Mat');
41
- react_native_fast_opencv_1.OpenCV.invoke('cvtColor', src, gray, 6); // 6 is BGR2GRAY
42
- const ksize = react_native_fast_opencv_1.OpenCV.createObject('Size', 5, 5);
38
+ gray = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Mat, 0, 0, react_native_fast_opencv_1.DataTypes.CV_8U);
39
+ blurred = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Mat, 0, 0, react_native_fast_opencv_1.DataTypes.CV_8U);
40
+ edges = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Mat, 0, 0, react_native_fast_opencv_1.DataTypes.CV_8U);
41
+ react_native_fast_opencv_1.OpenCV.invoke('cvtColor', src, gray, react_native_fast_opencv_1.ColorConversionCodes.COLOR_BGR2GRAY);
42
+ const ksize = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Size, 5, 5);
43
43
  react_native_fast_opencv_1.OpenCV.invoke('GaussianBlur', gray, blurred, ksize, 0);
44
44
  react_native_fast_opencv_1.OpenCV.invoke('Canny', blurred, edges, 75, 200, 3, false);
45
- contoursObj = react_native_fast_opencv_1.OpenCV.createObject('MatVector');
46
- hierarchyObj = react_native_fast_opencv_1.OpenCV.invoke('Mat');
45
+ contoursObj = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.MatVector);
46
+ hierarchyObj = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Mat, 0, 0, react_native_fast_opencv_1.DataTypes.CV_8U);
47
47
  react_native_fast_opencv_1.OpenCV.invoke('findContours', edges, contoursObj, hierarchyObj, 1, 2);
48
48
  const contoursSize = react_native_fast_opencv_1.OpenCV.invoke('size', contoursObj) || 0;
49
49
  let maxArea = 0;
@@ -53,22 +53,12 @@ class DocumentScanner {
53
53
  const area = react_native_fast_opencv_1.OpenCV.invoke('contourArea', contour);
54
54
  if (area > maxArea) {
55
55
  const peri = react_native_fast_opencv_1.OpenCV.invoke('arcLength', contour, true);
56
- const approx = react_native_fast_opencv_1.OpenCV.invoke('Mat');
56
+ const approx = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.PointVector);
57
57
  react_native_fast_opencv_1.OpenCV.invoke('approxPolyDP', contour, approx, 0.02 * peri, true);
58
- const vertices = react_native_fast_opencv_1.OpenCV.invoke('rows', approx);
59
- if (vertices === 4) {
58
+ const approxJS = react_native_fast_opencv_1.OpenCV.toJSValue(approx);
59
+ if (approxJS && approxJS.array && approxJS.array.length === 4) {
60
60
  maxArea = area;
61
- const points = [];
62
- for (let v = 0; v < 4; v++) {
63
- try {
64
- const pt = react_native_fast_opencv_1.OpenCV.invoke('row', approx, v);
65
- if (pt && typeof pt === 'object' && 'x' in pt)
66
- points.push(pt);
67
- }
68
- catch (err) { }
69
- }
70
- if (points.length === 4)
71
- largestPoly = points;
61
+ largestPoly = approxJS.array;
72
62
  }
73
63
  }
74
64
  }
@@ -105,11 +95,21 @@ class DocumentScanner {
105
95
  if (maxWidth === 0 || maxHeight === 0)
106
96
  return undefined;
107
97
  src = react_native_fast_opencv_1.OpenCV.base64ToMat(imageBase64);
108
- dst = react_native_fast_opencv_1.OpenCV.invoke('Mat');
109
- const srcPoints = react_native_fast_opencv_1.OpenCV.createObject('Point2fVector', tl.x, tl.y, tr.x, tr.y, br.x, br.y, bl.x, bl.y);
110
- const dstPoints = react_native_fast_opencv_1.OpenCV.createObject('Point2fVector', 0, 0, maxWidth - 1, 0, maxWidth - 1, maxHeight - 1, 0, maxHeight - 1);
98
+ dst = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Mat, 0, 0, react_native_fast_opencv_1.DataTypes.CV_8UC3);
99
+ const srcPoints = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2fVector, [
100
+ react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, tl.x, tl.y),
101
+ react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, tr.x, tr.y),
102
+ react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, br.x, br.y),
103
+ react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, bl.x, bl.y)
104
+ ]);
105
+ const dstPoints = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2fVector, [
106
+ react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, 0, 0),
107
+ react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, maxWidth - 1, 0),
108
+ react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, maxWidth - 1, maxHeight - 1),
109
+ react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, 0, maxHeight - 1)
110
+ ]);
111
111
  const perspectiveMatrix = react_native_fast_opencv_1.OpenCV.invoke('getPerspectiveTransform', srcPoints, dstPoints);
112
- const size = react_native_fast_opencv_1.OpenCV.createObject('Size', maxWidth, maxHeight);
112
+ const size = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Size, maxWidth, maxHeight);
113
113
  react_native_fast_opencv_1.OpenCV.invoke('warpPerspective', src, dst, perspectiveMatrix, size);
114
114
  return react_native_fast_opencv_1.OpenCV.invoke('toBase64', dst);
115
115
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rn-opencv-doc-perspective-correction",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "A React Native library for document corner detection and perspective correction using react-native-fast-opencv",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  // @ts-nocheck
2
- import { OpenCV, OpenCVMat } from 'react-native-fast-opencv';
2
+ import { OpenCV, OpenCVMat, ObjectType, DataTypes, ColorConversionCodes } from 'react-native-fast-opencv';
3
3
 
4
4
  export type Point = { x: number; y: number };
5
5
 
@@ -42,19 +42,19 @@ export class DocumentScanner {
42
42
 
43
43
  try {
44
44
  src = OpenCV.base64ToMat(imageBase64);
45
- gray = OpenCV.invoke('Mat');
46
- blurred = OpenCV.invoke('Mat');
47
- edges = OpenCV.invoke('Mat');
45
+ gray = OpenCV.createObject(ObjectType.Mat, 0, 0, DataTypes.CV_8U);
46
+ blurred = OpenCV.createObject(ObjectType.Mat, 0, 0, DataTypes.CV_8U);
47
+ edges = OpenCV.createObject(ObjectType.Mat, 0, 0, DataTypes.CV_8U);
48
48
 
49
- OpenCV.invoke('cvtColor', src, gray, 6); // 6 is BGR2GRAY
49
+ OpenCV.invoke('cvtColor', src, gray, ColorConversionCodes.COLOR_BGR2GRAY);
50
50
 
51
- const ksize = OpenCV.createObject('Size', 5, 5);
51
+ const ksize = OpenCV.createObject(ObjectType.Size, 5, 5);
52
52
  OpenCV.invoke('GaussianBlur', gray, blurred, ksize, 0);
53
53
 
54
54
  OpenCV.invoke('Canny', blurred, edges, 75, 200, 3, false);
55
55
 
56
- contoursObj = OpenCV.createObject('MatVector');
57
- hierarchyObj = OpenCV.invoke('Mat');
56
+ contoursObj = OpenCV.createObject(ObjectType.MatVector);
57
+ hierarchyObj = OpenCV.createObject(ObjectType.Mat, 0, 0, DataTypes.CV_8U);
58
58
 
59
59
  OpenCV.invoke('findContours', edges, contoursObj, hierarchyObj, 1, 2);
60
60
 
@@ -68,21 +68,13 @@ export class DocumentScanner {
68
68
 
69
69
  if (area > maxArea) {
70
70
  const peri = OpenCV.invoke('arcLength', contour, true);
71
- const approx = OpenCV.invoke('Mat');
71
+ const approx = OpenCV.createObject(ObjectType.PointVector);
72
72
  OpenCV.invoke('approxPolyDP', contour, approx, 0.02 * peri, true);
73
73
 
74
- const vertices = OpenCV.invoke('rows', approx);
75
- if (vertices === 4) {
74
+ const approxJS = OpenCV.toJSValue(approx);
75
+ if (approxJS && approxJS.array && approxJS.array.length === 4) {
76
76
  maxArea = area;
77
-
78
- const points: Point[] = [];
79
- for (let v = 0; v < 4; v++) {
80
- try {
81
- const pt = OpenCV.invoke('row', approx, v);
82
- if (pt && typeof pt === 'object' && 'x' in pt) points.push(pt as Point);
83
- } catch (err) { }
84
- }
85
- if (points.length === 4) largestPoly = points;
77
+ largestPoly = approxJS.array as Point[];
86
78
  }
87
79
  }
88
80
  }
@@ -123,20 +115,28 @@ export class DocumentScanner {
123
115
  if (maxWidth === 0 || maxHeight === 0) return undefined;
124
116
 
125
117
  src = OpenCV.base64ToMat(imageBase64);
126
- dst = OpenCV.invoke('Mat');
127
-
128
- const srcPoints = OpenCV.createObject('Point2fVector', tl.x, tl.y, tr.x, tr.y, br.x, br.y, bl.x, bl.y);
129
- const dstPoints = OpenCV.createObject(
130
- 'Point2fVector',
131
- 0, 0,
132
- maxWidth - 1, 0,
133
- maxWidth - 1, maxHeight - 1,
134
- 0, maxHeight - 1
118
+ dst = OpenCV.createObject(ObjectType.Mat, 0, 0, DataTypes.CV_8UC3);
119
+
120
+ const srcPoints = OpenCV.createObject(ObjectType.Point2fVector,
121
+ [
122
+ OpenCV.createObject(ObjectType.Point2f, tl.x, tl.y),
123
+ OpenCV.createObject(ObjectType.Point2f, tr.x, tr.y),
124
+ OpenCV.createObject(ObjectType.Point2f, br.x, br.y),
125
+ OpenCV.createObject(ObjectType.Point2f, bl.x, bl.y)
126
+ ]
127
+ );
128
+ const dstPoints = OpenCV.createObject(ObjectType.Point2fVector,
129
+ [
130
+ OpenCV.createObject(ObjectType.Point2f, 0, 0),
131
+ OpenCV.createObject(ObjectType.Point2f, maxWidth - 1, 0),
132
+ OpenCV.createObject(ObjectType.Point2f, maxWidth - 1, maxHeight - 1),
133
+ OpenCV.createObject(ObjectType.Point2f, 0, maxHeight - 1)
134
+ ]
135
135
  );
136
136
 
137
137
  const perspectiveMatrix = OpenCV.invoke('getPerspectiveTransform', srcPoints, dstPoints);
138
138
 
139
- const size = OpenCV.createObject('Size', maxWidth, maxHeight);
139
+ const size = OpenCV.createObject(ObjectType.Size, maxWidth, maxHeight);
140
140
  OpenCV.invoke('warpPerspective', src, dst, perspectiveMatrix, size);
141
141
 
142
142
  return OpenCV.invoke('toBase64', dst);