rn-opencv-doc-perspective-correction 1.0.2 → 1.0.3
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.
- package/dist/index.js +22 -12
- package/package.json +1 -1
- package/src/index.ts +27 -19
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.
|
|
39
|
-
blurred = react_native_fast_opencv_1.OpenCV.
|
|
40
|
-
edges = react_native_fast_opencv_1.OpenCV.
|
|
41
|
-
react_native_fast_opencv_1.OpenCV.invoke('cvtColor', src, gray,
|
|
42
|
-
const ksize = react_native_fast_opencv_1.OpenCV.createObject(
|
|
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(
|
|
46
|
-
hierarchyObj = react_native_fast_opencv_1.OpenCV.
|
|
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,7 +53,7 @@ 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.
|
|
56
|
+
const approx = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Mat, 0, 0, react_native_fast_opencv_1.DataTypes.CV_8U);
|
|
57
57
|
react_native_fast_opencv_1.OpenCV.invoke('approxPolyDP', contour, approx, 0.02 * peri, true);
|
|
58
58
|
const vertices = react_native_fast_opencv_1.OpenCV.invoke('rows', approx);
|
|
59
59
|
if (vertices === 4) {
|
|
@@ -105,11 +105,21 @@ class DocumentScanner {
|
|
|
105
105
|
if (maxWidth === 0 || maxHeight === 0)
|
|
106
106
|
return undefined;
|
|
107
107
|
src = react_native_fast_opencv_1.OpenCV.base64ToMat(imageBase64);
|
|
108
|
-
dst = react_native_fast_opencv_1.OpenCV.
|
|
109
|
-
const srcPoints = react_native_fast_opencv_1.OpenCV.createObject(
|
|
110
|
-
|
|
108
|
+
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);
|
|
109
|
+
const srcPoints = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2fVector, [
|
|
110
|
+
react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, tl.x, tl.y),
|
|
111
|
+
react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, tr.x, tr.y),
|
|
112
|
+
react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, br.x, br.y),
|
|
113
|
+
react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, bl.x, bl.y)
|
|
114
|
+
]);
|
|
115
|
+
const dstPoints = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2fVector, [
|
|
116
|
+
react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, 0, 0),
|
|
117
|
+
react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, maxWidth - 1, 0),
|
|
118
|
+
react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, maxWidth - 1, maxHeight - 1),
|
|
119
|
+
react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, 0, maxHeight - 1)
|
|
120
|
+
]);
|
|
111
121
|
const perspectiveMatrix = react_native_fast_opencv_1.OpenCV.invoke('getPerspectiveTransform', srcPoints, dstPoints);
|
|
112
|
-
const size = react_native_fast_opencv_1.OpenCV.createObject(
|
|
122
|
+
const size = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Size, maxWidth, maxHeight);
|
|
113
123
|
react_native_fast_opencv_1.OpenCV.invoke('warpPerspective', src, dst, perspectiveMatrix, size);
|
|
114
124
|
return react_native_fast_opencv_1.OpenCV.invoke('toBase64', dst);
|
|
115
125
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rn-opencv-doc-perspective-correction",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
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.
|
|
46
|
-
blurred = OpenCV.
|
|
47
|
-
edges = OpenCV.
|
|
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,
|
|
49
|
+
OpenCV.invoke('cvtColor', src, gray, ColorConversionCodes.COLOR_BGR2GRAY);
|
|
50
50
|
|
|
51
|
-
const ksize = OpenCV.createObject(
|
|
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(
|
|
57
|
-
hierarchyObj = OpenCV.
|
|
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,7 +68,7 @@ export class DocumentScanner {
|
|
|
68
68
|
|
|
69
69
|
if (area > maxArea) {
|
|
70
70
|
const peri = OpenCV.invoke('arcLength', contour, true);
|
|
71
|
-
const approx = OpenCV.
|
|
71
|
+
const approx = OpenCV.createObject(ObjectType.Mat, 0, 0, DataTypes.CV_8U);
|
|
72
72
|
OpenCV.invoke('approxPolyDP', contour, approx, 0.02 * peri, true);
|
|
73
73
|
|
|
74
74
|
const vertices = OpenCV.invoke('rows', approx);
|
|
@@ -123,20 +123,28 @@ export class DocumentScanner {
|
|
|
123
123
|
if (maxWidth === 0 || maxHeight === 0) return undefined;
|
|
124
124
|
|
|
125
125
|
src = OpenCV.base64ToMat(imageBase64);
|
|
126
|
-
dst = OpenCV.
|
|
127
|
-
|
|
128
|
-
const srcPoints = OpenCV.createObject(
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
126
|
+
dst = OpenCV.createObject(ObjectType.Mat, 0, 0, DataTypes.CV_8UC3);
|
|
127
|
+
|
|
128
|
+
const srcPoints = OpenCV.createObject(ObjectType.Point2fVector,
|
|
129
|
+
[
|
|
130
|
+
OpenCV.createObject(ObjectType.Point2f, tl.x, tl.y) as any,
|
|
131
|
+
OpenCV.createObject(ObjectType.Point2f, tr.x, tr.y) as any,
|
|
132
|
+
OpenCV.createObject(ObjectType.Point2f, br.x, br.y) as any,
|
|
133
|
+
OpenCV.createObject(ObjectType.Point2f, bl.x, bl.y) as any
|
|
134
|
+
]
|
|
135
|
+
);
|
|
136
|
+
const dstPoints = OpenCV.createObject(ObjectType.Point2fVector,
|
|
137
|
+
[
|
|
138
|
+
OpenCV.createObject(ObjectType.Point2f, 0, 0) as any,
|
|
139
|
+
OpenCV.createObject(ObjectType.Point2f, maxWidth - 1, 0) as any,
|
|
140
|
+
OpenCV.createObject(ObjectType.Point2f, maxWidth - 1, maxHeight - 1) as any,
|
|
141
|
+
OpenCV.createObject(ObjectType.Point2f, 0, maxHeight - 1) as any
|
|
142
|
+
]
|
|
135
143
|
);
|
|
136
144
|
|
|
137
145
|
const perspectiveMatrix = OpenCV.invoke('getPerspectiveTransform', srcPoints, dstPoints);
|
|
138
146
|
|
|
139
|
-
const size = OpenCV.createObject(
|
|
147
|
+
const size = OpenCV.createObject(ObjectType.Size, maxWidth, maxHeight);
|
|
140
148
|
OpenCV.invoke('warpPerspective', src, dst, perspectiveMatrix, size);
|
|
141
149
|
|
|
142
150
|
return OpenCV.invoke('toBase64', dst);
|