react-native-rectangle-doc-scanner 0.11.0 → 0.12.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.008 * perimeter;
158
+ for (let attempt = 0; attempt < 8; attempt += 1) {
159
+ const epsilon = epsilonBase * (1 + attempt * 0.75);
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,19 @@ 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;
188
- }
181
+ const { x: rectX, y: rectY, width: rectW, height: rectH } = react_native_fast_opencv_1.OpenCV.invoke('boundingRect', contour);
182
+ approxArray = [
183
+ { x: rectX, y: rectY },
184
+ { x: rectX + rectW, y: rectY },
185
+ { x: rectX + rectW, y: rectY + rectH },
186
+ { x: rectX, y: rectY + rectH },
187
+ ];
189
188
  }
190
189
  catch (err) {
191
190
  if (__DEV__) {
192
- console.warn('[DocScanner] minAreaRect fallback failed', err);
191
+ console.warn('[DocScanner] boundingRect fallback failed', err);
193
192
  }
194
193
  }
195
194
  }
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.12.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.008 * 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 < 8; attempt += 1) {
183
+ const epsilon = epsilonBase * (1 + attempt * 0.75);
184
184
  step = `contour_${i}_approxPolyDP_attempt_${attempt}`;
185
185
  reportStage(step);
186
186
  OpenCV.invoke('approxPolyDP', contour, approx, epsilon, true);
@@ -205,20 +205,18 @@ 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 }>;
218
- }
210
+ const { x: rectX, y: rectY, width: rectW, height: rectH } = OpenCV.invoke('boundingRect', contour);
211
+ approxArray = [
212
+ { x: rectX, y: rectY },
213
+ { x: rectX + rectW, y: rectY },
214
+ { x: rectX + rectW, y: rectY + rectH },
215
+ { x: rectX, y: rectY + rectH },
216
+ ];
219
217
  } catch (err) {
220
218
  if (__DEV__) {
221
- console.warn('[DocScanner] minAreaRect fallback failed', err);
219
+ console.warn('[DocScanner] boundingRect fallback failed', err);
222
220
  }
223
221
  }
224
222
  }