react-native-rectangle-doc-scanner 0.9.0 → 0.10.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.
@@ -150,15 +150,30 @@ const DocScanner = ({ onCapture, overlayColor = '#e7a649', autoCapture = true, m
150
150
  reportStage(step);
151
151
  const { value: perimeter } = react_native_fast_opencv_1.OpenCV.invoke('arcLength', contour, true);
152
152
  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}`);
153
+ let approxArray = [];
154
+ let epsilonBase = 0.01 * perimeter;
155
+ for (let attempt = 0; attempt < 5; attempt += 1) {
156
+ const epsilon = epsilonBase * (1 + attempt * 0.5);
157
+ step = `contour_${i}_approxPolyDP_attempt_${attempt}`;
158
+ reportStage(step);
159
+ react_native_fast_opencv_1.OpenCV.invoke('approxPolyDP', contour, approx, epsilon, true);
160
+ step = `contour_${i}_toJS_attempt_${attempt}`;
161
+ reportStage(step);
162
+ const approxValue = react_native_fast_opencv_1.OpenCV.toJSValue(approx);
163
+ const candidate = Array.isArray(approxValue?.array) ? approxValue.array : [];
164
+ if (__DEV__) {
165
+ console.log('[DocScanner] approx length', candidate.length, 'epsilon', epsilon);
166
+ }
167
+ if (candidate.length === 4) {
168
+ approxArray = candidate;
169
+ break;
170
+ }
171
+ if (approxArray.length === 0 || Math.abs(candidate.length - 4) < Math.abs(approxArray.length - 4)) {
172
+ approxArray = candidate;
173
+ }
174
+ }
175
+ if (approxArray.length !== 4) {
176
+ continue;
162
177
  }
163
178
  if (approxArray.length !== 4) {
164
179
  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.10.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "repository": {
@@ -172,17 +172,36 @@ export const DocScanner: React.FC<Props> = ({
172
172
  const { value: perimeter } = OpenCV.invoke('arcLength', contour, true);
173
173
  const approx = OpenCV.createObject(ObjectType.PointVector);
174
174
 
175
- step = `contour_${i}_approxPolyDP`;
176
- reportStage(step);
177
- OpenCV.invoke('approxPolyDP', contour, approx, 0.012 * perimeter, true);
178
-
179
- step = `contour_${i}_toJS`;
180
- reportStage(step);
181
- const approxValue = OpenCV.toJSValue(approx);
182
- const approxArray = Array.isArray(approxValue?.array) ? approxValue.array : [];
175
+ let approxArray: Array<{ x: number; y: number }> = [];
176
+ let epsilonBase = 0.01 * perimeter;
177
+
178
+ for (let attempt = 0; attempt < 5; attempt += 1) {
179
+ const epsilon = epsilonBase * (1 + attempt * 0.5);
180
+ step = `contour_${i}_approxPolyDP_attempt_${attempt}`;
181
+ reportStage(step);
182
+ OpenCV.invoke('approxPolyDP', contour, approx, epsilon, true);
183
+
184
+ step = `contour_${i}_toJS_attempt_${attempt}`;
185
+ reportStage(step);
186
+ const approxValue = OpenCV.toJSValue(approx);
187
+ const candidate = Array.isArray(approxValue?.array) ? approxValue.array : [];
188
+
189
+ if (__DEV__) {
190
+ console.log('[DocScanner] approx length', candidate.length, 'epsilon', epsilon);
191
+ }
192
+
193
+ if (candidate.length === 4) {
194
+ approxArray = candidate as Array<{ x: number; y: number }>;
195
+ break;
196
+ }
197
+
198
+ if (approxArray.length === 0 || Math.abs(candidate.length - 4) < Math.abs(approxArray.length - 4)) {
199
+ approxArray = candidate as Array<{ x: number; y: number }>;
200
+ }
201
+ }
183
202
 
184
- if (__DEV__) {
185
- reportStage(`${step}_length_${approxArray.length}`);
203
+ if (approxArray.length !== 4) {
204
+ continue;
186
205
  }
187
206
 
188
207
  if (approxArray.length !== 4) {