react-native-rectangle-doc-scanner 0.33.0 → 0.34.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 +7 -42
- package/package.json +1 -1
- package/src/DocScanner.tsx +7 -47
package/dist/DocScanner.js
CHANGED
|
@@ -196,25 +196,23 @@ const DocScanner = ({ onCapture, overlayColor = '#e7a649', autoCapture = true, m
|
|
|
196
196
|
if (__DEV__) {
|
|
197
197
|
console.log('[DocScanner] area', area, 'ratio', areaRatio);
|
|
198
198
|
}
|
|
199
|
-
if
|
|
199
|
+
// Skip if area ratio is too small or too large
|
|
200
|
+
if (areaRatio < 0.01 || areaRatio > 0.95) {
|
|
200
201
|
continue;
|
|
201
202
|
}
|
|
202
|
-
// Use convex hull to simplify contour before polygon approximation
|
|
203
|
-
const hull = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.PointVector);
|
|
204
|
-
react_native_fast_opencv_1.OpenCV.invoke('convexHull', contour, hull);
|
|
205
203
|
step = `contour_${i}_arcLength`;
|
|
206
204
|
reportStage(step);
|
|
207
|
-
const { value: perimeter } = react_native_fast_opencv_1.OpenCV.invoke('arcLength',
|
|
205
|
+
const { value: perimeter } = react_native_fast_opencv_1.OpenCV.invoke('arcLength', contour, true);
|
|
208
206
|
const approx = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.PointVector);
|
|
209
207
|
let approxArray = [];
|
|
210
208
|
// Start with smaller epsilon for more accurate corner detection
|
|
211
|
-
// Try epsilon values from 0.
|
|
212
|
-
const epsilonValues = [0.
|
|
209
|
+
// Try epsilon values from 0.5% to 5% of perimeter
|
|
210
|
+
const epsilonValues = [0.005, 0.01, 0.015, 0.02, 0.025, 0.03, 0.035, 0.04, 0.045, 0.05];
|
|
213
211
|
for (let attempt = 0; attempt < epsilonValues.length; attempt += 1) {
|
|
214
212
|
const epsilon = epsilonValues[attempt] * perimeter;
|
|
215
213
|
step = `contour_${i}_approxPolyDP_attempt_${attempt}`;
|
|
216
214
|
reportStage(step);
|
|
217
|
-
react_native_fast_opencv_1.OpenCV.invoke('approxPolyDP',
|
|
215
|
+
react_native_fast_opencv_1.OpenCV.invoke('approxPolyDP', contour, approx, epsilon, true);
|
|
218
216
|
step = `contour_${i}_toJS_attempt_${attempt}`;
|
|
219
217
|
reportStage(step);
|
|
220
218
|
const approxValue = react_native_fast_opencv_1.OpenCV.toJSValue(approx);
|
|
@@ -227,40 +225,7 @@ const DocScanner = ({ onCapture, overlayColor = '#e7a649', autoCapture = true, m
|
|
|
227
225
|
break;
|
|
228
226
|
}
|
|
229
227
|
}
|
|
230
|
-
if
|
|
231
|
-
// fallback: rotated rectangle using minAreaRect
|
|
232
|
-
try {
|
|
233
|
-
const rect = react_native_fast_opencv_1.OpenCV.invoke('minAreaRect', contour);
|
|
234
|
-
const rectValue = rect?.value ?? rect;
|
|
235
|
-
const centerX = rectValue.centerX ?? rectValue.center?.x ?? 0;
|
|
236
|
-
const centerY = rectValue.centerY ?? rectValue.center?.y ?? 0;
|
|
237
|
-
const rectW = rectValue.width ?? rectValue.size?.width ?? 0;
|
|
238
|
-
const rectH = rectValue.height ?? rectValue.size?.height ?? 0;
|
|
239
|
-
const angleDeg = rectValue.angle ?? rectValue.rotation ?? 0;
|
|
240
|
-
if (rectW > 0 && rectH > 0) {
|
|
241
|
-
const angleRad = (angleDeg * Math.PI) / 180;
|
|
242
|
-
const cosA = Math.cos(angleRad);
|
|
243
|
-
const sinA = Math.sin(angleRad);
|
|
244
|
-
const halfW = rectW / 2;
|
|
245
|
-
const halfH = rectH / 2;
|
|
246
|
-
approxArray = [
|
|
247
|
-
{ x: centerX - halfW * cosA + halfH * sinA, y: centerY - halfW * sinA - halfH * cosA },
|
|
248
|
-
{ x: centerX + halfW * cosA + halfH * sinA, y: centerY + halfW * sinA - halfH * cosA },
|
|
249
|
-
{ x: centerX + halfW * cosA - halfH * sinA, y: centerY + halfW * sinA + halfH * cosA },
|
|
250
|
-
{ x: centerX - halfW * cosA - halfH * sinA, y: centerY - halfW * sinA + halfH * cosA },
|
|
251
|
-
];
|
|
252
|
-
if (__DEV__) {
|
|
253
|
-
console.log('[DocScanner] minAreaRect fallback', rectValue, approxArray);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
catch (err) {
|
|
258
|
-
if (__DEV__) {
|
|
259
|
-
console.warn('[DocScanner] minAreaRect fallback failed', err);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
// Only proceed if we found exactly 4 corners after fallback
|
|
228
|
+
// Only proceed if we found exactly 4 corners
|
|
264
229
|
if (approxArray.length !== 4) {
|
|
265
230
|
continue;
|
|
266
231
|
}
|
package/package.json
CHANGED
package/src/DocScanner.tsx
CHANGED
|
@@ -224,30 +224,27 @@ export const DocScanner: React.FC<Props> = ({
|
|
|
224
224
|
console.log('[DocScanner] area', area, 'ratio', areaRatio);
|
|
225
225
|
}
|
|
226
226
|
|
|
227
|
-
if
|
|
227
|
+
// Skip if area ratio is too small or too large
|
|
228
|
+
if (areaRatio < 0.01 || areaRatio > 0.95) {
|
|
228
229
|
continue;
|
|
229
230
|
}
|
|
230
231
|
|
|
231
|
-
// Use convex hull to simplify contour before polygon approximation
|
|
232
|
-
const hull = OpenCV.createObject(ObjectType.PointVector);
|
|
233
|
-
OpenCV.invoke('convexHull', contour, hull);
|
|
234
|
-
|
|
235
232
|
step = `contour_${i}_arcLength`;
|
|
236
233
|
reportStage(step);
|
|
237
|
-
const { value: perimeter } = OpenCV.invoke('arcLength',
|
|
234
|
+
const { value: perimeter } = OpenCV.invoke('arcLength', contour, true);
|
|
238
235
|
const approx = OpenCV.createObject(ObjectType.PointVector);
|
|
239
236
|
|
|
240
237
|
let approxArray: Array<{ x: number; y: number }> = [];
|
|
241
238
|
|
|
242
239
|
// Start with smaller epsilon for more accurate corner detection
|
|
243
|
-
// Try epsilon values from 0.
|
|
244
|
-
const epsilonValues = [0.
|
|
240
|
+
// Try epsilon values from 0.5% to 5% of perimeter
|
|
241
|
+
const epsilonValues = [0.005, 0.01, 0.015, 0.02, 0.025, 0.03, 0.035, 0.04, 0.045, 0.05];
|
|
245
242
|
|
|
246
243
|
for (let attempt = 0; attempt < epsilonValues.length; attempt += 1) {
|
|
247
244
|
const epsilon = epsilonValues[attempt] * perimeter;
|
|
248
245
|
step = `contour_${i}_approxPolyDP_attempt_${attempt}`;
|
|
249
246
|
reportStage(step);
|
|
250
|
-
OpenCV.invoke('approxPolyDP',
|
|
247
|
+
OpenCV.invoke('approxPolyDP', contour, approx, epsilon, true);
|
|
251
248
|
|
|
252
249
|
step = `contour_${i}_toJS_attempt_${attempt}`;
|
|
253
250
|
reportStage(step);
|
|
@@ -264,44 +261,7 @@ export const DocScanner: React.FC<Props> = ({
|
|
|
264
261
|
}
|
|
265
262
|
}
|
|
266
263
|
|
|
267
|
-
if
|
|
268
|
-
// fallback: rotated rectangle using minAreaRect
|
|
269
|
-
try {
|
|
270
|
-
const rect = OpenCV.invoke('minAreaRect', contour);
|
|
271
|
-
const rectValue = rect?.value ?? rect;
|
|
272
|
-
|
|
273
|
-
const centerX = rectValue.centerX ?? rectValue.center?.x ?? 0;
|
|
274
|
-
const centerY = rectValue.centerY ?? rectValue.center?.y ?? 0;
|
|
275
|
-
const rectW = rectValue.width ?? rectValue.size?.width ?? 0;
|
|
276
|
-
const rectH = rectValue.height ?? rectValue.size?.height ?? 0;
|
|
277
|
-
const angleDeg = rectValue.angle ?? rectValue.rotation ?? 0;
|
|
278
|
-
|
|
279
|
-
if (rectW > 0 && rectH > 0) {
|
|
280
|
-
const angleRad = (angleDeg * Math.PI) / 180;
|
|
281
|
-
const cosA = Math.cos(angleRad);
|
|
282
|
-
const sinA = Math.sin(angleRad);
|
|
283
|
-
const halfW = rectW / 2;
|
|
284
|
-
const halfH = rectH / 2;
|
|
285
|
-
|
|
286
|
-
approxArray = [
|
|
287
|
-
{ x: centerX - halfW * cosA + halfH * sinA, y: centerY - halfW * sinA - halfH * cosA },
|
|
288
|
-
{ x: centerX + halfW * cosA + halfH * sinA, y: centerY + halfW * sinA - halfH * cosA },
|
|
289
|
-
{ x: centerX + halfW * cosA - halfH * sinA, y: centerY + halfW * sinA + halfH * cosA },
|
|
290
|
-
{ x: centerX - halfW * cosA - halfH * sinA, y: centerY - halfW * sinA + halfH * cosA },
|
|
291
|
-
];
|
|
292
|
-
|
|
293
|
-
if (__DEV__) {
|
|
294
|
-
console.log('[DocScanner] minAreaRect fallback', rectValue, approxArray);
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
} catch (err) {
|
|
298
|
-
if (__DEV__) {
|
|
299
|
-
console.warn('[DocScanner] minAreaRect fallback failed', err);
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
// Only proceed if we found exactly 4 corners after fallback
|
|
264
|
+
// Only proceed if we found exactly 4 corners
|
|
305
265
|
if (approxArray.length !== 4) {
|
|
306
266
|
continue;
|
|
307
267
|
}
|