react-native-rectangle-doc-scanner 0.12.0 → 0.14.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 +33 -10
- package/package.json +1 -1
- package/src/DocScanner.tsx +39 -10
package/dist/DocScanner.js
CHANGED
|
@@ -154,9 +154,9 @@ const DocScanner = ({ onCapture, overlayColor = '#e7a649', autoCapture = true, m
|
|
|
154
154
|
const { value: perimeter } = react_native_fast_opencv_1.OpenCV.invoke('arcLength', contour, true);
|
|
155
155
|
const approx = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.PointVector);
|
|
156
156
|
let approxArray = [];
|
|
157
|
-
let epsilonBase = 0.
|
|
158
|
-
for (let attempt = 0; attempt <
|
|
159
|
-
const epsilon = epsilonBase * (1 + attempt
|
|
157
|
+
let epsilonBase = 0.006 * perimeter;
|
|
158
|
+
for (let attempt = 0; attempt < 10; attempt += 1) {
|
|
159
|
+
const epsilon = epsilonBase * (1 + attempt);
|
|
160
160
|
step = `contour_${i}_approxPolyDP_attempt_${attempt}`;
|
|
161
161
|
reportStage(step);
|
|
162
162
|
react_native_fast_opencv_1.OpenCV.invoke('approxPolyDP', contour, approx, epsilon, true);
|
|
@@ -178,13 +178,24 @@ const DocScanner = ({ onCapture, overlayColor = '#e7a649', autoCapture = true, m
|
|
|
178
178
|
if (approxArray.length !== 4) {
|
|
179
179
|
// fallback: boundingRect (axis-aligned) so we always have 4 points
|
|
180
180
|
try {
|
|
181
|
-
const
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
181
|
+
const rect = react_native_fast_opencv_1.OpenCV.invoke('boundingRect', contour);
|
|
182
|
+
const rectValue = rect?.value ?? rect;
|
|
183
|
+
const rectX = rectValue.x ?? rectValue?.topLeft?.x ?? 0;
|
|
184
|
+
const rectY = rectValue.y ?? rectValue?.topLeft?.y ?? 0;
|
|
185
|
+
const rectW = rectValue.width ?? rectValue?.size?.width ?? 0;
|
|
186
|
+
const rectH = rectValue.height ?? rectValue?.size?.height ?? 0;
|
|
187
|
+
// Validate that we have a valid rectangle
|
|
188
|
+
if (rectW > 0 && rectH > 0) {
|
|
189
|
+
approxArray = [
|
|
190
|
+
{ x: rectX, y: rectY },
|
|
191
|
+
{ x: rectX + rectW, y: rectY },
|
|
192
|
+
{ x: rectX + rectW, y: rectY + rectH },
|
|
193
|
+
{ x: rectX, y: rectY + rectH },
|
|
194
|
+
];
|
|
195
|
+
if (__DEV__) {
|
|
196
|
+
console.log('[DocScanner] boundingRect fallback', approxArray);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
188
199
|
}
|
|
189
200
|
catch (err) {
|
|
190
201
|
if (__DEV__) {
|
|
@@ -197,6 +208,18 @@ const DocScanner = ({ onCapture, overlayColor = '#e7a649', autoCapture = true, m
|
|
|
197
208
|
}
|
|
198
209
|
step = `contour_${i}_convex`;
|
|
199
210
|
reportStage(step);
|
|
211
|
+
// Validate points before processing
|
|
212
|
+
const isValidPoint = (pt) => {
|
|
213
|
+
return typeof pt.x === 'number' && typeof pt.y === 'number' &&
|
|
214
|
+
!isNaN(pt.x) && !isNaN(pt.y) &&
|
|
215
|
+
isFinite(pt.x) && isFinite(pt.y);
|
|
216
|
+
};
|
|
217
|
+
if (!approxArray.every(isValidPoint)) {
|
|
218
|
+
if (__DEV__) {
|
|
219
|
+
console.warn('[DocScanner] invalid points in approxArray', approxArray);
|
|
220
|
+
}
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
200
223
|
const points = approxArray.map((pt) => ({
|
|
201
224
|
x: pt.x / ratio,
|
|
202
225
|
y: pt.y / ratio,
|
package/package.json
CHANGED
package/src/DocScanner.tsx
CHANGED
|
@@ -177,10 +177,10 @@ export const DocScanner: React.FC<Props> = ({
|
|
|
177
177
|
const approx = OpenCV.createObject(ObjectType.PointVector);
|
|
178
178
|
|
|
179
179
|
let approxArray: Array<{ x: number; y: number }> = [];
|
|
180
|
-
let epsilonBase = 0.
|
|
180
|
+
let epsilonBase = 0.006 * perimeter;
|
|
181
181
|
|
|
182
|
-
for (let attempt = 0; attempt <
|
|
183
|
-
const epsilon = epsilonBase * (1 + attempt
|
|
182
|
+
for (let attempt = 0; attempt < 10; attempt += 1) {
|
|
183
|
+
const epsilon = epsilonBase * (1 + attempt);
|
|
184
184
|
step = `contour_${i}_approxPolyDP_attempt_${attempt}`;
|
|
185
185
|
reportStage(step);
|
|
186
186
|
OpenCV.invoke('approxPolyDP', contour, approx, epsilon, true);
|
|
@@ -207,13 +207,27 @@ export const DocScanner: React.FC<Props> = ({
|
|
|
207
207
|
if (approxArray.length !== 4) {
|
|
208
208
|
// fallback: boundingRect (axis-aligned) so we always have 4 points
|
|
209
209
|
try {
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
210
|
+
const rect = OpenCV.invoke('boundingRect', contour);
|
|
211
|
+
const rectValue = rect?.value ?? rect;
|
|
212
|
+
|
|
213
|
+
const rectX = rectValue.x ?? rectValue?.topLeft?.x ?? 0;
|
|
214
|
+
const rectY = rectValue.y ?? rectValue?.topLeft?.y ?? 0;
|
|
215
|
+
const rectW = rectValue.width ?? rectValue?.size?.width ?? 0;
|
|
216
|
+
const rectH = rectValue.height ?? rectValue?.size?.height ?? 0;
|
|
217
|
+
|
|
218
|
+
// Validate that we have a valid rectangle
|
|
219
|
+
if (rectW > 0 && rectH > 0) {
|
|
220
|
+
approxArray = [
|
|
221
|
+
{ x: rectX, y: rectY },
|
|
222
|
+
{ x: rectX + rectW, y: rectY },
|
|
223
|
+
{ x: rectX + rectW, y: rectY + rectH },
|
|
224
|
+
{ x: rectX, y: rectY + rectH },
|
|
225
|
+
];
|
|
226
|
+
|
|
227
|
+
if (__DEV__) {
|
|
228
|
+
console.log('[DocScanner] boundingRect fallback', approxArray);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
217
231
|
} catch (err) {
|
|
218
232
|
if (__DEV__) {
|
|
219
233
|
console.warn('[DocScanner] boundingRect fallback failed', err);
|
|
@@ -227,6 +241,21 @@ export const DocScanner: React.FC<Props> = ({
|
|
|
227
241
|
|
|
228
242
|
step = `contour_${i}_convex`;
|
|
229
243
|
reportStage(step);
|
|
244
|
+
|
|
245
|
+
// Validate points before processing
|
|
246
|
+
const isValidPoint = (pt: { x: number; y: number }) => {
|
|
247
|
+
return typeof pt.x === 'number' && typeof pt.y === 'number' &&
|
|
248
|
+
!isNaN(pt.x) && !isNaN(pt.y) &&
|
|
249
|
+
isFinite(pt.x) && isFinite(pt.y);
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
if (!approxArray.every(isValidPoint)) {
|
|
253
|
+
if (__DEV__) {
|
|
254
|
+
console.warn('[DocScanner] invalid points in approxArray', approxArray);
|
|
255
|
+
}
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
|
|
230
259
|
const points: Point[] = approxArray.map((pt: { x: number; y: number }) => ({
|
|
231
260
|
x: pt.x / ratio,
|
|
232
261
|
y: pt.y / ratio,
|