react-native-rectangle-doc-scanner 0.15.0 → 0.17.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 +13 -22
- package/package.json +1 -1
- package/src/DocScanner.tsx +13 -23
package/dist/DocScanner.js
CHANGED
|
@@ -154,6 +154,7 @@ 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 usedBoundingRect = false;
|
|
157
158
|
let epsilonBase = 0.006 * perimeter;
|
|
158
159
|
for (let attempt = 0; attempt < 10; attempt += 1) {
|
|
159
160
|
const epsilon = epsilonBase * (1 + attempt);
|
|
@@ -177,22 +178,15 @@ const DocScanner = ({ onCapture, overlayColor = '#e7a649', autoCapture = true, m
|
|
|
177
178
|
}
|
|
178
179
|
if (approxArray.length !== 4) {
|
|
179
180
|
// fallback: boundingRect (axis-aligned) so we always have 4 points
|
|
180
|
-
if (__DEV__) {
|
|
181
|
-
console.log('[DocScanner] attempting boundingRect fallback, current length:', approxArray.length);
|
|
182
|
-
}
|
|
183
181
|
try {
|
|
184
182
|
const rect = react_native_fast_opencv_1.OpenCV.invoke('boundingRect', contour);
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
const
|
|
190
|
-
const
|
|
191
|
-
const
|
|
192
|
-
const rectH = rectValue.height ?? rectValue?.size?.height ?? 0;
|
|
193
|
-
if (__DEV__) {
|
|
194
|
-
console.log('[DocScanner] parsed rect:', { x: rectX, y: rectY, w: rectW, h: rectH });
|
|
195
|
-
}
|
|
183
|
+
// Convert the rect object to JS value to get actual coordinates
|
|
184
|
+
const rectJS = react_native_fast_opencv_1.OpenCV.toJSValue(rect);
|
|
185
|
+
const rectValue = rectJS?.value ?? rectJS;
|
|
186
|
+
const rectX = rectValue?.x ?? 0;
|
|
187
|
+
const rectY = rectValue?.y ?? 0;
|
|
188
|
+
const rectW = rectValue?.width ?? 0;
|
|
189
|
+
const rectH = rectValue?.height ?? 0;
|
|
196
190
|
// Validate that we have a valid rectangle
|
|
197
191
|
if (rectW > 0 && rectH > 0) {
|
|
198
192
|
approxArray = [
|
|
@@ -201,19 +195,15 @@ const DocScanner = ({ onCapture, overlayColor = '#e7a649', autoCapture = true, m
|
|
|
201
195
|
{ x: rectX + rectW, y: rectY + rectH },
|
|
202
196
|
{ x: rectX, y: rectY + rectH },
|
|
203
197
|
];
|
|
198
|
+
usedBoundingRect = true;
|
|
204
199
|
if (__DEV__) {
|
|
205
|
-
console.log('[DocScanner] boundingRect fallback
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
else {
|
|
209
|
-
if (__DEV__) {
|
|
210
|
-
console.warn('[DocScanner] boundingRect has invalid dimensions');
|
|
200
|
+
console.log('[DocScanner] using boundingRect fallback:', approxArray);
|
|
211
201
|
}
|
|
212
202
|
}
|
|
213
203
|
}
|
|
214
204
|
catch (err) {
|
|
215
205
|
if (__DEV__) {
|
|
216
|
-
console.warn('[DocScanner] boundingRect fallback
|
|
206
|
+
console.warn('[DocScanner] boundingRect fallback failed:', err);
|
|
217
207
|
}
|
|
218
208
|
}
|
|
219
209
|
}
|
|
@@ -238,7 +228,8 @@ const DocScanner = ({ onCapture, overlayColor = '#e7a649', autoCapture = true, m
|
|
|
238
228
|
x: pt.x / ratio,
|
|
239
229
|
y: pt.y / ratio,
|
|
240
230
|
}));
|
|
241
|
-
|
|
231
|
+
// Skip convexity check for boundingRect (always forms a valid rectangle)
|
|
232
|
+
if (!usedBoundingRect && !isConvexQuadrilateral(points)) {
|
|
242
233
|
continue;
|
|
243
234
|
}
|
|
244
235
|
if (area > maxArea) {
|
package/package.json
CHANGED
package/src/DocScanner.tsx
CHANGED
|
@@ -177,6 +177,7 @@ 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 usedBoundingRect = false;
|
|
180
181
|
let epsilonBase = 0.006 * perimeter;
|
|
181
182
|
|
|
182
183
|
for (let attempt = 0; attempt < 10; attempt += 1) {
|
|
@@ -206,25 +207,16 @@ export const DocScanner: React.FC<Props> = ({
|
|
|
206
207
|
|
|
207
208
|
if (approxArray.length !== 4) {
|
|
208
209
|
// fallback: boundingRect (axis-aligned) so we always have 4 points
|
|
209
|
-
if (__DEV__) {
|
|
210
|
-
console.log('[DocScanner] attempting boundingRect fallback, current length:', approxArray.length);
|
|
211
|
-
}
|
|
212
210
|
try {
|
|
213
211
|
const rect = OpenCV.invoke('boundingRect', contour);
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
console.log('[DocScanner] boundingRect result:', JSON.stringify(rectValue));
|
|
218
|
-
}
|
|
212
|
+
// Convert the rect object to JS value to get actual coordinates
|
|
213
|
+
const rectJS = OpenCV.toJSValue(rect);
|
|
214
|
+
const rectValue = rectJS?.value ?? rectJS;
|
|
219
215
|
|
|
220
|
-
const rectX = rectValue
|
|
221
|
-
const rectY = rectValue
|
|
222
|
-
const rectW = rectValue
|
|
223
|
-
const rectH = rectValue
|
|
224
|
-
|
|
225
|
-
if (__DEV__) {
|
|
226
|
-
console.log('[DocScanner] parsed rect:', { x: rectX, y: rectY, w: rectW, h: rectH });
|
|
227
|
-
}
|
|
216
|
+
const rectX = rectValue?.x ?? 0;
|
|
217
|
+
const rectY = rectValue?.y ?? 0;
|
|
218
|
+
const rectW = rectValue?.width ?? 0;
|
|
219
|
+
const rectH = rectValue?.height ?? 0;
|
|
228
220
|
|
|
229
221
|
// Validate that we have a valid rectangle
|
|
230
222
|
if (rectW > 0 && rectH > 0) {
|
|
@@ -234,18 +226,15 @@ export const DocScanner: React.FC<Props> = ({
|
|
|
234
226
|
{ x: rectX + rectW, y: rectY + rectH },
|
|
235
227
|
{ x: rectX, y: rectY + rectH },
|
|
236
228
|
];
|
|
229
|
+
usedBoundingRect = true;
|
|
237
230
|
|
|
238
231
|
if (__DEV__) {
|
|
239
|
-
console.log('[DocScanner] boundingRect fallback
|
|
240
|
-
}
|
|
241
|
-
} else {
|
|
242
|
-
if (__DEV__) {
|
|
243
|
-
console.warn('[DocScanner] boundingRect has invalid dimensions');
|
|
232
|
+
console.log('[DocScanner] using boundingRect fallback:', approxArray);
|
|
244
233
|
}
|
|
245
234
|
}
|
|
246
235
|
} catch (err) {
|
|
247
236
|
if (__DEV__) {
|
|
248
|
-
console.warn('[DocScanner] boundingRect fallback
|
|
237
|
+
console.warn('[DocScanner] boundingRect fallback failed:', err);
|
|
249
238
|
}
|
|
250
239
|
}
|
|
251
240
|
}
|
|
@@ -276,7 +265,8 @@ export const DocScanner: React.FC<Props> = ({
|
|
|
276
265
|
y: pt.y / ratio,
|
|
277
266
|
}));
|
|
278
267
|
|
|
279
|
-
|
|
268
|
+
// Skip convexity check for boundingRect (always forms a valid rectangle)
|
|
269
|
+
if (!usedBoundingRect && !isConvexQuadrilateral(points)) {
|
|
280
270
|
continue;
|
|
281
271
|
}
|
|
282
272
|
|