react-native-rectangle-doc-scanner 0.8.0 → 0.10.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 +26 -8
- package/package.json +1 -1
- package/src/DocScanner.tsx +31 -8
package/dist/DocScanner.js
CHANGED
|
@@ -143,20 +143,38 @@ const DocScanner = ({ onCapture, overlayColor = '#e7a649', autoCapture = true, m
|
|
|
143
143
|
step = `contour_${i}_area`;
|
|
144
144
|
reportStage(step);
|
|
145
145
|
const { value: area } = react_native_fast_opencv_1.OpenCV.invoke('contourArea', contour, false);
|
|
146
|
-
if (area < width * height * 0.
|
|
146
|
+
if (area < width * height * 0.02) {
|
|
147
147
|
continue;
|
|
148
148
|
}
|
|
149
149
|
step = `contour_${i}_arcLength`;
|
|
150
150
|
reportStage(step);
|
|
151
151
|
const { value: perimeter } = react_native_fast_opencv_1.OpenCV.invoke('arcLength', contour, true);
|
|
152
152
|
const approx = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.PointVector);
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
153
|
+
let approxArray = [];
|
|
154
|
+
let epsilonBase = 0.01 * perimeter;
|
|
155
|
+
for (let attempt = 0; attempt < 5; attempt += 1) {
|
|
156
|
+
const epsilon = epsilonBase * (1 + attempt * 0.5);
|
|
157
|
+
step = `contour_${i}_approxPolyDP_attempt_${attempt}`;
|
|
158
|
+
reportStage(step);
|
|
159
|
+
react_native_fast_opencv_1.OpenCV.invoke('approxPolyDP', contour, approx, epsilon, true);
|
|
160
|
+
step = `contour_${i}_toJS_attempt_${attempt}`;
|
|
161
|
+
reportStage(step);
|
|
162
|
+
const approxValue = react_native_fast_opencv_1.OpenCV.toJSValue(approx);
|
|
163
|
+
const candidate = Array.isArray(approxValue?.array) ? approxValue.array : [];
|
|
164
|
+
if (__DEV__) {
|
|
165
|
+
console.log('[DocScanner] approx length', candidate.length, 'epsilon', epsilon);
|
|
166
|
+
}
|
|
167
|
+
if (candidate.length === 4) {
|
|
168
|
+
approxArray = candidate;
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
if (approxArray.length === 0 || Math.abs(candidate.length - 4) < Math.abs(approxArray.length - 4)) {
|
|
172
|
+
approxArray = candidate;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (approxArray.length !== 4) {
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
160
178
|
if (approxArray.length !== 4) {
|
|
161
179
|
continue;
|
|
162
180
|
}
|
package/package.json
CHANGED
package/src/DocScanner.tsx
CHANGED
|
@@ -163,7 +163,7 @@ export const DocScanner: React.FC<Props> = ({
|
|
|
163
163
|
reportStage(step);
|
|
164
164
|
const { value: area } = OpenCV.invoke('contourArea', contour, false);
|
|
165
165
|
|
|
166
|
-
if (area < width * height * 0.
|
|
166
|
+
if (area < width * height * 0.02) {
|
|
167
167
|
continue;
|
|
168
168
|
}
|
|
169
169
|
|
|
@@ -172,14 +172,37 @@ export const DocScanner: React.FC<Props> = ({
|
|
|
172
172
|
const { value: perimeter } = OpenCV.invoke('arcLength', contour, true);
|
|
173
173
|
const approx = OpenCV.createObject(ObjectType.PointVector);
|
|
174
174
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
OpenCV.invoke('approxPolyDP', contour, approx, 0.015 * perimeter, true);
|
|
175
|
+
let approxArray: Array<{ x: number; y: number }> = [];
|
|
176
|
+
let epsilonBase = 0.01 * perimeter;
|
|
178
177
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
178
|
+
for (let attempt = 0; attempt < 5; attempt += 1) {
|
|
179
|
+
const epsilon = epsilonBase * (1 + attempt * 0.5);
|
|
180
|
+
step = `contour_${i}_approxPolyDP_attempt_${attempt}`;
|
|
181
|
+
reportStage(step);
|
|
182
|
+
OpenCV.invoke('approxPolyDP', contour, approx, epsilon, true);
|
|
183
|
+
|
|
184
|
+
step = `contour_${i}_toJS_attempt_${attempt}`;
|
|
185
|
+
reportStage(step);
|
|
186
|
+
const approxValue = OpenCV.toJSValue(approx);
|
|
187
|
+
const candidate = Array.isArray(approxValue?.array) ? approxValue.array : [];
|
|
188
|
+
|
|
189
|
+
if (__DEV__) {
|
|
190
|
+
console.log('[DocScanner] approx length', candidate.length, 'epsilon', epsilon);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (candidate.length === 4) {
|
|
194
|
+
approxArray = candidate as Array<{ x: number; y: number }>;
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (approxArray.length === 0 || Math.abs(candidate.length - 4) < Math.abs(approxArray.length - 4)) {
|
|
199
|
+
approxArray = candidate as Array<{ x: number; y: number }>;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (approxArray.length !== 4) {
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
183
206
|
|
|
184
207
|
if (approxArray.length !== 4) {
|
|
185
208
|
continue;
|