react-native-rectangle-doc-scanner 0.10.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.
- package/dist/DocScanner.js +22 -5
- package/package.json +1 -1
- package/src/DocScanner.tsx +22 -5
package/dist/DocScanner.js
CHANGED
|
@@ -143,7 +143,10 @@ 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 (
|
|
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`;
|
|
@@ -151,9 +154,9 @@ const DocScanner = ({ onCapture, overlayColor = '#e7a649', autoCapture = true, m
|
|
|
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
156
|
let approxArray = [];
|
|
154
|
-
let epsilonBase = 0.
|
|
155
|
-
for (let attempt = 0; attempt <
|
|
156
|
-
const epsilon = epsilonBase * (1 + attempt * 0.
|
|
157
|
+
let epsilonBase = 0.008 * perimeter;
|
|
158
|
+
for (let attempt = 0; attempt < 8; attempt += 1) {
|
|
159
|
+
const epsilon = epsilonBase * (1 + attempt * 0.75);
|
|
157
160
|
step = `contour_${i}_approxPolyDP_attempt_${attempt}`;
|
|
158
161
|
reportStage(step);
|
|
159
162
|
react_native_fast_opencv_1.OpenCV.invoke('approxPolyDP', contour, approx, epsilon, true);
|
|
@@ -173,7 +176,21 @@ const DocScanner = ({ onCapture, overlayColor = '#e7a649', autoCapture = true, m
|
|
|
173
176
|
}
|
|
174
177
|
}
|
|
175
178
|
if (approxArray.length !== 4) {
|
|
176
|
-
|
|
179
|
+
// fallback: boundingRect (axis-aligned) so we always have 4 points
|
|
180
|
+
try {
|
|
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
|
+
];
|
|
188
|
+
}
|
|
189
|
+
catch (err) {
|
|
190
|
+
if (__DEV__) {
|
|
191
|
+
console.warn('[DocScanner] boundingRect fallback failed', err);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
177
194
|
}
|
|
178
195
|
if (approxArray.length !== 4) {
|
|
179
196
|
continue;
|
package/package.json
CHANGED
package/src/DocScanner.tsx
CHANGED
|
@@ -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 (
|
|
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
|
|
|
@@ -173,10 +177,10 @@ export const DocScanner: React.FC<Props> = ({
|
|
|
173
177
|
const approx = OpenCV.createObject(ObjectType.PointVector);
|
|
174
178
|
|
|
175
179
|
let approxArray: Array<{ x: number; y: number }> = [];
|
|
176
|
-
let epsilonBase = 0.
|
|
180
|
+
let epsilonBase = 0.008 * perimeter;
|
|
177
181
|
|
|
178
|
-
for (let attempt = 0; attempt <
|
|
179
|
-
const epsilon = epsilonBase * (1 + attempt * 0.
|
|
182
|
+
for (let attempt = 0; attempt < 8; attempt += 1) {
|
|
183
|
+
const epsilon = epsilonBase * (1 + attempt * 0.75);
|
|
180
184
|
step = `contour_${i}_approxPolyDP_attempt_${attempt}`;
|
|
181
185
|
reportStage(step);
|
|
182
186
|
OpenCV.invoke('approxPolyDP', contour, approx, epsilon, true);
|
|
@@ -201,7 +205,20 @@ export const DocScanner: React.FC<Props> = ({
|
|
|
201
205
|
}
|
|
202
206
|
|
|
203
207
|
if (approxArray.length !== 4) {
|
|
204
|
-
|
|
208
|
+
// fallback: boundingRect (axis-aligned) so we always have 4 points
|
|
209
|
+
try {
|
|
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
|
+
];
|
|
217
|
+
} catch (err) {
|
|
218
|
+
if (__DEV__) {
|
|
219
|
+
console.warn('[DocScanner] boundingRect fallback failed', err);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
205
222
|
}
|
|
206
223
|
|
|
207
224
|
if (approxArray.length !== 4) {
|