react-native-rectangle-doc-scanner 0.9.0 → 0.11.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.
@@ -143,22 +143,55 @@ const DocScanner = ({ onCapture, overlayColor = '#e7a649', autoCapture = true, m
143
143
  step = `contour_${i}_area`;
144
144
  reportStage(step);
145
145
  const { value: area } = react_native_fast_opencv_1.OpenCV.invoke('contourArea', contour, false);
146
- if (area < width * height * 0.02) {
146
+ if (__DEV__) {
147
+ console.log('[DocScanner] area ratio', area / (width * height));
148
+ }
149
+ if (area < width * height * 0.005) {
147
150
  continue;
148
151
  }
149
152
  step = `contour_${i}_arcLength`;
150
153
  reportStage(step);
151
154
  const { value: perimeter } = react_native_fast_opencv_1.OpenCV.invoke('arcLength', contour, true);
152
155
  const approx = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.PointVector);
153
- step = `contour_${i}_approxPolyDP`;
154
- reportStage(step);
155
- react_native_fast_opencv_1.OpenCV.invoke('approxPolyDP', contour, approx, 0.012 * perimeter, true);
156
- step = `contour_${i}_toJS`;
157
- reportStage(step);
158
- const approxValue = react_native_fast_opencv_1.OpenCV.toJSValue(approx);
159
- const approxArray = Array.isArray(approxValue?.array) ? approxValue.array : [];
160
- if (__DEV__) {
161
- reportStage(`${step}_length_${approxArray.length}`);
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);
160
+ step = `contour_${i}_approxPolyDP_attempt_${attempt}`;
161
+ reportStage(step);
162
+ react_native_fast_opencv_1.OpenCV.invoke('approxPolyDP', contour, approx, epsilon, true);
163
+ step = `contour_${i}_toJS_attempt_${attempt}`;
164
+ reportStage(step);
165
+ const approxValue = react_native_fast_opencv_1.OpenCV.toJSValue(approx);
166
+ const candidate = Array.isArray(approxValue?.array) ? approxValue.array : [];
167
+ if (__DEV__) {
168
+ console.log('[DocScanner] approx length', candidate.length, 'epsilon', epsilon);
169
+ }
170
+ if (candidate.length === 4) {
171
+ approxArray = candidate;
172
+ break;
173
+ }
174
+ if (approxArray.length === 0 || Math.abs(candidate.length - 4) < Math.abs(approxArray.length - 4)) {
175
+ approxArray = candidate;
176
+ }
177
+ }
178
+ if (approxArray.length !== 4) {
179
+ // fallback to minAreaRect -> boxPoints
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
+ }
189
+ }
190
+ catch (err) {
191
+ if (__DEV__) {
192
+ console.warn('[DocScanner] minAreaRect fallback failed', err);
193
+ }
194
+ }
162
195
  }
163
196
  if (approxArray.length !== 4) {
164
197
  continue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-rectangle-doc-scanner",
3
- "version": "0.9.0",
3
+ "version": "0.11.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "repository": {
@@ -163,7 +163,11 @@ export const DocScanner: React.FC<Props> = ({
163
163
  reportStage(step);
164
164
  const { value: area } = OpenCV.invoke('contourArea', contour, false);
165
165
 
166
- if (area < width * height * 0.02) {
166
+ if (__DEV__) {
167
+ console.log('[DocScanner] area ratio', area / (width * height));
168
+ }
169
+
170
+ if (area < width * height * 0.005) {
167
171
  continue;
168
172
  }
169
173
 
@@ -172,17 +176,51 @@ export const DocScanner: React.FC<Props> = ({
172
176
  const { value: perimeter } = OpenCV.invoke('arcLength', contour, true);
173
177
  const approx = OpenCV.createObject(ObjectType.PointVector);
174
178
 
175
- step = `contour_${i}_approxPolyDP`;
176
- reportStage(step);
177
- OpenCV.invoke('approxPolyDP', contour, approx, 0.012 * perimeter, true);
179
+ let approxArray: Array<{ x: number; y: number }> = [];
180
+ let epsilonBase = 0.01 * perimeter;
178
181
 
179
- step = `contour_${i}_toJS`;
180
- reportStage(step);
181
- const approxValue = OpenCV.toJSValue(approx);
182
- const approxArray = Array.isArray(approxValue?.array) ? approxValue.array : [];
182
+ for (let attempt = 0; attempt < 5; attempt += 1) {
183
+ const epsilon = epsilonBase * (1 + attempt * 0.5);
184
+ step = `contour_${i}_approxPolyDP_attempt_${attempt}`;
185
+ reportStage(step);
186
+ OpenCV.invoke('approxPolyDP', contour, approx, epsilon, true);
183
187
 
184
- if (__DEV__) {
185
- reportStage(`${step}_length_${approxArray.length}`);
188
+ step = `contour_${i}_toJS_attempt_${attempt}`;
189
+ reportStage(step);
190
+ const approxValue = OpenCV.toJSValue(approx);
191
+ const candidate = Array.isArray(approxValue?.array) ? approxValue.array : [];
192
+
193
+ if (__DEV__) {
194
+ console.log('[DocScanner] approx length', candidate.length, 'epsilon', epsilon);
195
+ }
196
+
197
+ if (candidate.length === 4) {
198
+ approxArray = candidate as Array<{ x: number; y: number }>;
199
+ break;
200
+ }
201
+
202
+ if (approxArray.length === 0 || Math.abs(candidate.length - 4) < Math.abs(approxArray.length - 4)) {
203
+ approxArray = candidate as Array<{ x: number; y: number }>;
204
+ }
205
+ }
206
+
207
+ if (approxArray.length !== 4) {
208
+ // fallback to minAreaRect -> boxPoints
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
+ }
219
+ } catch (err) {
220
+ if (__DEV__) {
221
+ console.warn('[DocScanner] minAreaRect fallback failed', err);
222
+ }
223
+ }
186
224
  }
187
225
 
188
226
  if (approxArray.length !== 4) {