react-native-rectangle-doc-scanner 0.4.0 → 0.5.0
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/DocScanner.js +80 -48
- package/package.json +4 -1
- package/src/DocScanner.tsx +99 -61
- package/src/worklets-core.d.ts +8 -0
package/dist/DocScanner.js
CHANGED
|
@@ -38,7 +38,7 @@ const react_1 = __importStar(require("react"));
|
|
|
38
38
|
const react_native_1 = require("react-native");
|
|
39
39
|
const react_native_vision_camera_1 = require("react-native-vision-camera");
|
|
40
40
|
const vision_camera_resize_plugin_1 = require("vision-camera-resize-plugin");
|
|
41
|
-
const
|
|
41
|
+
const react_native_worklets_core_1 = require("react-native-worklets-core");
|
|
42
42
|
const react_native_fast_opencv_1 = require("react-native-fast-opencv");
|
|
43
43
|
const overlay_1 = require("./utils/overlay");
|
|
44
44
|
const stability_1 = require("./utils/stability");
|
|
@@ -77,57 +77,89 @@ const DocScanner = ({ onCapture, overlayColor = '#e7a649', autoCapture = true, m
|
|
|
77
77
|
(0, react_1.useEffect)(() => {
|
|
78
78
|
requestPermission();
|
|
79
79
|
}, [requestPermission]);
|
|
80
|
+
const updateQuad = (0, react_native_worklets_core_1.useRunOnJS)((value) => {
|
|
81
|
+
setQuad(value);
|
|
82
|
+
}, []);
|
|
83
|
+
const reportError = (0, react_native_worklets_core_1.useRunOnJS)((step, error) => {
|
|
84
|
+
const message = error instanceof Error ? error.message : `${error}`;
|
|
85
|
+
console.warn(`[DocScanner] frame error at ${step}: ${message}`);
|
|
86
|
+
}, []);
|
|
80
87
|
const frameProcessor = (0, react_native_vision_camera_1.useFrameProcessor)((frame) => {
|
|
81
88
|
'worklet';
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
89
|
+
let step = 'start';
|
|
90
|
+
try {
|
|
91
|
+
const ratio = 480 / frame.width;
|
|
92
|
+
const width = Math.floor(frame.width * ratio);
|
|
93
|
+
const height = Math.floor(frame.height * ratio);
|
|
94
|
+
step = 'resize';
|
|
95
|
+
const resized = resize(frame, {
|
|
96
|
+
dataType: 'uint8',
|
|
97
|
+
pixelFormat: 'bgr',
|
|
98
|
+
scale: { width: width, height: height },
|
|
99
|
+
});
|
|
100
|
+
step = 'frameBufferToMat';
|
|
101
|
+
const mat = react_native_fast_opencv_1.OpenCV.frameBufferToMat(height, width, 3, resized);
|
|
102
|
+
step = 'cvtColor';
|
|
103
|
+
react_native_fast_opencv_1.OpenCV.invoke('cvtColor', mat, mat, react_native_fast_opencv_1.ColorConversionCodes.COLOR_BGR2GRAY);
|
|
104
|
+
const morphologyKernel = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Size, 5, 5);
|
|
105
|
+
step = 'getStructuringElement';
|
|
106
|
+
const element = react_native_fast_opencv_1.OpenCV.invoke('getStructuringElement', react_native_fast_opencv_1.MorphShapes.MORPH_RECT, morphologyKernel);
|
|
107
|
+
step = 'morphologyEx';
|
|
108
|
+
react_native_fast_opencv_1.OpenCV.invoke('morphologyEx', mat, mat, react_native_fast_opencv_1.MorphTypes.MORPH_OPEN, element);
|
|
109
|
+
const gaussianKernel = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Size, 5, 5);
|
|
110
|
+
step = 'GaussianBlur';
|
|
111
|
+
react_native_fast_opencv_1.OpenCV.invoke('GaussianBlur', mat, mat, gaussianKernel, 0);
|
|
112
|
+
step = 'Canny';
|
|
113
|
+
react_native_fast_opencv_1.OpenCV.invoke('Canny', mat, mat, 75, 100);
|
|
114
|
+
step = 'createContours';
|
|
115
|
+
const contours = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.PointVectorOfVectors);
|
|
116
|
+
react_native_fast_opencv_1.OpenCV.invoke('findContours', mat, contours, react_native_fast_opencv_1.RetrievalModes.RETR_LIST, react_native_fast_opencv_1.ContourApproximationModes.CHAIN_APPROX_SIMPLE);
|
|
117
|
+
let best = null;
|
|
118
|
+
let maxArea = 0;
|
|
119
|
+
step = 'toJSValue';
|
|
120
|
+
const contourVector = react_native_fast_opencv_1.OpenCV.toJSValue(contours);
|
|
121
|
+
const contourArray = Array.isArray(contourVector?.array) ? contourVector.array : [];
|
|
122
|
+
for (let i = 0; i < contourArray.length; i += 1) {
|
|
123
|
+
step = `contour_${i}_copy`;
|
|
124
|
+
const contour = react_native_fast_opencv_1.OpenCV.copyObjectFromVector(contours, i);
|
|
125
|
+
step = `contour_${i}_area`;
|
|
126
|
+
const { value: area } = react_native_fast_opencv_1.OpenCV.invoke('contourArea', contour, false);
|
|
127
|
+
if (area < width * height * 0.1) {
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
step = `contour_${i}_arcLength`;
|
|
131
|
+
const { value: perimeter } = react_native_fast_opencv_1.OpenCV.invoke('arcLength', contour, true);
|
|
132
|
+
const approx = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.PointVector);
|
|
133
|
+
step = `contour_${i}_approxPolyDP`;
|
|
134
|
+
react_native_fast_opencv_1.OpenCV.invoke('approxPolyDP', contour, approx, 0.02 * perimeter, true);
|
|
135
|
+
step = `contour_${i}_toJS`;
|
|
136
|
+
const approxValue = react_native_fast_opencv_1.OpenCV.toJSValue(approx);
|
|
137
|
+
const approxArray = Array.isArray(approxValue?.array) ? approxValue.array : [];
|
|
138
|
+
if (approxArray.length !== 4) {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
step = `contour_${i}_convex`;
|
|
142
|
+
const points = approxArray.map((pt) => ({
|
|
143
|
+
x: pt.x / ratio,
|
|
144
|
+
y: pt.y / ratio,
|
|
145
|
+
}));
|
|
146
|
+
if (!isConvexQuadrilateral(points)) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (area > maxArea) {
|
|
150
|
+
best = points;
|
|
151
|
+
maxArea = area;
|
|
152
|
+
}
|
|
126
153
|
}
|
|
154
|
+
step = 'clearBuffers';
|
|
155
|
+
react_native_fast_opencv_1.OpenCV.clearBuffers();
|
|
156
|
+
step = 'updateQuad';
|
|
157
|
+
updateQuad(best);
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
reportError(step, error);
|
|
127
161
|
}
|
|
128
|
-
|
|
129
|
-
(0, react_native_reanimated_1.runOnJS)(setQuad)(best);
|
|
130
|
-
}, [resize]);
|
|
162
|
+
}, [resize, reportError, updateQuad]);
|
|
131
163
|
(0, react_1.useEffect)(() => {
|
|
132
164
|
const s = (0, stability_1.checkStability)(quad);
|
|
133
165
|
setStable(s);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-rectangle-doc-scanner",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"repository": {
|
|
@@ -29,5 +29,8 @@
|
|
|
29
29
|
"@types/react": "^18.2.41",
|
|
30
30
|
"@types/react-native": "0.73.0",
|
|
31
31
|
"typescript": "^5.3.3"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"react-native-worklets-core": "^1.6.2"
|
|
32
35
|
}
|
|
33
36
|
}
|
package/src/DocScanner.tsx
CHANGED
|
@@ -2,7 +2,7 @@ import React, { ReactNode, useCallback, useEffect, useRef, useState } from 'reac
|
|
|
2
2
|
import { View, TouchableOpacity, StyleSheet } from 'react-native';
|
|
3
3
|
import { Camera, useCameraDevice, useCameraPermission, useFrameProcessor } from 'react-native-vision-camera';
|
|
4
4
|
import { useResizePlugin } from 'vision-camera-resize-plugin';
|
|
5
|
-
import {
|
|
5
|
+
import { useRunOnJS } from 'react-native-worklets-core';
|
|
6
6
|
import {
|
|
7
7
|
OpenCV,
|
|
8
8
|
ColorConversionCodes,
|
|
@@ -82,72 +82,110 @@ export const DocScanner: React.FC<Props> = ({
|
|
|
82
82
|
requestPermission();
|
|
83
83
|
}, [requestPermission]);
|
|
84
84
|
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const ratio = 480 / frame.width;
|
|
89
|
-
const w = Math.floor(frame.width * ratio);
|
|
90
|
-
const h = Math.floor(frame.height * ratio);
|
|
91
|
-
const resized = resize(frame, {
|
|
92
|
-
dataType: 'uint8',
|
|
93
|
-
pixelFormat: 'bgr',
|
|
94
|
-
scale: { width: w, height: h },
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
const mat = OpenCV.frameBufferToMat(h, w, 3, resized);
|
|
98
|
-
|
|
99
|
-
OpenCV.invoke('cvtColor', mat, mat, ColorConversionCodes.COLOR_BGR2GRAY);
|
|
100
|
-
|
|
101
|
-
const kernel = OpenCV.createObject(ObjectType.Size, 4, 4);
|
|
102
|
-
const element = OpenCV.invoke('getStructuringElement', MorphShapes.MORPH_RECT, kernel);
|
|
103
|
-
OpenCV.invoke('morphologyEx', mat, mat, MorphTypes.MORPH_OPEN, element);
|
|
104
|
-
|
|
105
|
-
OpenCV.invoke('GaussianBlur', mat, mat, kernel, 0);
|
|
106
|
-
OpenCV.invoke('Canny', mat, mat, 75, 100);
|
|
107
|
-
|
|
108
|
-
const contours = OpenCV.createObject(ObjectType.PointVectorOfVectors);
|
|
109
|
-
OpenCV.invoke('findContours', mat, contours, RetrievalModes.RETR_LIST, ContourApproximationModes.CHAIN_APPROX_SIMPLE);
|
|
110
|
-
|
|
111
|
-
let best: Point[] | null = null;
|
|
112
|
-
let maxArea = 0;
|
|
113
|
-
|
|
114
|
-
const arr = OpenCV.toJSValue(contours).array;
|
|
115
|
-
|
|
116
|
-
for (let i = 0; i < arr.length; i++) {
|
|
117
|
-
const c = OpenCV.copyObjectFromVector(contours, i);
|
|
118
|
-
const { value: area } = OpenCV.invoke('contourArea', c, false);
|
|
119
|
-
|
|
120
|
-
if (area < w * h * 0.1) {
|
|
121
|
-
continue;
|
|
122
|
-
}
|
|
85
|
+
const updateQuad = useRunOnJS((value: Point[] | null) => {
|
|
86
|
+
setQuad(value);
|
|
87
|
+
}, []);
|
|
123
88
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
if (size !== 4) {
|
|
129
|
-
continue;
|
|
130
|
-
}
|
|
89
|
+
const reportError = useRunOnJS((step: string, error: unknown) => {
|
|
90
|
+
const message = error instanceof Error ? error.message : `${error}`;
|
|
91
|
+
console.warn(`[DocScanner] frame error at ${step}: ${message}`);
|
|
92
|
+
}, []);
|
|
131
93
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const p = OpenCV.invoke('atPoint', approx, j, 0);
|
|
135
|
-
pts.push({ x: p.x / ratio, y: p.y / ratio });
|
|
136
|
-
}
|
|
94
|
+
const frameProcessor = useFrameProcessor((frame) => {
|
|
95
|
+
'worklet';
|
|
137
96
|
|
|
138
|
-
|
|
139
|
-
|
|
97
|
+
let step = 'start';
|
|
98
|
+
|
|
99
|
+
try {
|
|
100
|
+
const ratio = 480 / frame.width;
|
|
101
|
+
const width = Math.floor(frame.width * ratio);
|
|
102
|
+
const height = Math.floor(frame.height * ratio);
|
|
103
|
+
step = 'resize';
|
|
104
|
+
const resized = resize(frame, {
|
|
105
|
+
dataType: 'uint8',
|
|
106
|
+
pixelFormat: 'bgr',
|
|
107
|
+
scale: { width: width, height: height },
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
step = 'frameBufferToMat';
|
|
111
|
+
const mat = OpenCV.frameBufferToMat(height, width, 3, resized);
|
|
112
|
+
|
|
113
|
+
step = 'cvtColor';
|
|
114
|
+
OpenCV.invoke('cvtColor', mat, mat, ColorConversionCodes.COLOR_BGR2GRAY);
|
|
115
|
+
|
|
116
|
+
const morphologyKernel = OpenCV.createObject(ObjectType.Size, 5, 5);
|
|
117
|
+
step = 'getStructuringElement';
|
|
118
|
+
const element = OpenCV.invoke('getStructuringElement', MorphShapes.MORPH_RECT, morphologyKernel);
|
|
119
|
+
step = 'morphologyEx';
|
|
120
|
+
OpenCV.invoke('morphologyEx', mat, mat, MorphTypes.MORPH_OPEN, element);
|
|
121
|
+
|
|
122
|
+
const gaussianKernel = OpenCV.createObject(ObjectType.Size, 5, 5);
|
|
123
|
+
step = 'GaussianBlur';
|
|
124
|
+
OpenCV.invoke('GaussianBlur', mat, mat, gaussianKernel, 0);
|
|
125
|
+
step = 'Canny';
|
|
126
|
+
OpenCV.invoke('Canny', mat, mat, 75, 100);
|
|
127
|
+
|
|
128
|
+
step = 'createContours';
|
|
129
|
+
const contours = OpenCV.createObject(ObjectType.PointVectorOfVectors);
|
|
130
|
+
OpenCV.invoke('findContours', mat, contours, RetrievalModes.RETR_LIST, ContourApproximationModes.CHAIN_APPROX_SIMPLE);
|
|
131
|
+
|
|
132
|
+
let best: Point[] | null = null;
|
|
133
|
+
let maxArea = 0;
|
|
134
|
+
|
|
135
|
+
step = 'toJSValue';
|
|
136
|
+
const contourVector = OpenCV.toJSValue(contours);
|
|
137
|
+
const contourArray = Array.isArray(contourVector?.array) ? contourVector.array : [];
|
|
138
|
+
|
|
139
|
+
for (let i = 0; i < contourArray.length; i += 1) {
|
|
140
|
+
step = `contour_${i}_copy`;
|
|
141
|
+
const contour = OpenCV.copyObjectFromVector(contours, i);
|
|
142
|
+
|
|
143
|
+
step = `contour_${i}_area`;
|
|
144
|
+
const { value: area } = OpenCV.invoke('contourArea', contour, false);
|
|
145
|
+
|
|
146
|
+
if (area < width * height * 0.1) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
step = `contour_${i}_arcLength`;
|
|
151
|
+
const { value: perimeter } = OpenCV.invoke('arcLength', contour, true);
|
|
152
|
+
const approx = OpenCV.createObject(ObjectType.PointVector);
|
|
153
|
+
|
|
154
|
+
step = `contour_${i}_approxPolyDP`;
|
|
155
|
+
OpenCV.invoke('approxPolyDP', contour, approx, 0.02 * perimeter, true);
|
|
156
|
+
|
|
157
|
+
step = `contour_${i}_toJS`;
|
|
158
|
+
const approxValue = OpenCV.toJSValue(approx);
|
|
159
|
+
const approxArray = Array.isArray(approxValue?.array) ? approxValue.array : [];
|
|
160
|
+
|
|
161
|
+
if (approxArray.length !== 4) {
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
step = `contour_${i}_convex`;
|
|
166
|
+
const points: Point[] = approxArray.map((pt: { x: number; y: number }) => ({
|
|
167
|
+
x: pt.x / ratio,
|
|
168
|
+
y: pt.y / ratio,
|
|
169
|
+
}));
|
|
170
|
+
|
|
171
|
+
if (!isConvexQuadrilateral(points)) {
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (area > maxArea) {
|
|
176
|
+
best = points;
|
|
177
|
+
maxArea = area;
|
|
178
|
+
}
|
|
140
179
|
}
|
|
141
180
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
181
|
+
step = 'clearBuffers';
|
|
182
|
+
OpenCV.clearBuffers();
|
|
183
|
+
step = 'updateQuad';
|
|
184
|
+
updateQuad(best);
|
|
185
|
+
} catch (error) {
|
|
186
|
+
reportError(step, error);
|
|
146
187
|
}
|
|
147
|
-
|
|
148
|
-
OpenCV.clearBuffers();
|
|
149
|
-
runOnJS(setQuad)(best);
|
|
150
|
-
}, [resize]);
|
|
188
|
+
}, [resize, reportError, updateQuad]);
|
|
151
189
|
|
|
152
190
|
useEffect(() => {
|
|
153
191
|
const s = checkStability(quad);
|