react-native-rectangle-doc-scanner 0.11.0 → 0.13.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.
@@ -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.01 * perimeter;
158
- for (let attempt = 0; attempt < 5; attempt += 1) {
159
- const epsilon = epsilonBase * (1 + attempt * 0.5);
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);
@@ -176,20 +176,27 @@ const DocScanner = ({ onCapture, overlayColor = '#e7a649', autoCapture = true, m
176
176
  }
177
177
  }
178
178
  if (approxArray.length !== 4) {
179
- // fallback to minAreaRect -> boxPoints
179
+ // fallback: boundingRect (axis-aligned) so we always have 4 points
180
180
  try {
181
- const minRect = react_native_fast_opencv_1.OpenCV.invoke('minAreaRect', contour);
182
- const rectPoints = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.PointVector);
183
- react_native_fast_opencv_1.OpenCV.invoke('boxPoints', minRect, rectPoints);
184
- const rectValue = react_native_fast_opencv_1.OpenCV.toJSValue(rectPoints);
185
- const rectArray = Array.isArray(rectValue?.array) ? rectValue.array : [];
186
- if (rectArray.length === 4) {
187
- approxArray = rectArray;
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
+ approxArray = [
188
+ { x: rectX, y: rectY },
189
+ { x: rectX + rectW, y: rectY },
190
+ { x: rectX + rectW, y: rectY + rectH },
191
+ { x: rectX, y: rectY + rectH },
192
+ ];
193
+ if (__DEV__) {
194
+ console.log('[DocScanner] boundingRect fallback', approxArray);
188
195
  }
189
196
  }
190
197
  catch (err) {
191
198
  if (__DEV__) {
192
- console.warn('[DocScanner] minAreaRect fallback failed', err);
199
+ console.warn('[DocScanner] boundingRect fallback failed', err);
193
200
  }
194
201
  }
195
202
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-rectangle-doc-scanner",
3
- "version": "0.11.0",
3
+ "version": "0.13.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "repository": {
@@ -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.01 * perimeter;
180
+ let epsilonBase = 0.006 * perimeter;
181
181
 
182
- for (let attempt = 0; attempt < 5; attempt += 1) {
183
- const epsilon = epsilonBase * (1 + attempt * 0.5);
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);
@@ -205,20 +205,29 @@ export const DocScanner: React.FC<Props> = ({
205
205
  }
206
206
 
207
207
  if (approxArray.length !== 4) {
208
- // fallback to minAreaRect -> boxPoints
208
+ // fallback: boundingRect (axis-aligned) so we always have 4 points
209
209
  try {
210
- const minRect = OpenCV.invoke('minAreaRect', contour);
211
- const rectPoints = OpenCV.createObject(ObjectType.PointVector);
212
- OpenCV.invoke('boxPoints', minRect, rectPoints);
213
- const rectValue = OpenCV.toJSValue(rectPoints);
214
- const rectArray = Array.isArray(rectValue?.array) ? rectValue.array : [];
215
-
216
- if (rectArray.length === 4) {
217
- approxArray = rectArray as Array<{ x: number; y: number }>;
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
+ approxArray = [
219
+ { x: rectX, y: rectY },
220
+ { x: rectX + rectW, y: rectY },
221
+ { x: rectX + rectW, y: rectY + rectH },
222
+ { x: rectX, y: rectY + rectH },
223
+ ];
224
+
225
+ if (__DEV__) {
226
+ console.log('[DocScanner] boundingRect fallback', approxArray);
218
227
  }
219
228
  } catch (err) {
220
229
  if (__DEV__) {
221
- console.warn('[DocScanner] minAreaRect fallback failed', err);
230
+ console.warn('[DocScanner] boundingRect fallback failed', err);
222
231
  }
223
232
  }
224
233
  }