react-native-rectangle-doc-scanner 3.245.0 → 3.246.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.
|
@@ -50,6 +50,9 @@ class DocumentScannerView(context: ThemedReactContext) : FrameLayout(context), L
|
|
|
50
50
|
private var lastDetectionTimestamp: Long = 0L
|
|
51
51
|
private var isCapturing = false
|
|
52
52
|
private var isDetaching = false
|
|
53
|
+
private var lastDetectedRectangle: Rectangle? = null
|
|
54
|
+
private var lastDetectedImageWidth = 0
|
|
55
|
+
private var lastDetectedImageHeight = 0
|
|
53
56
|
|
|
54
57
|
// Coroutine scope for async operations
|
|
55
58
|
private val scope = CoroutineScope(Dispatchers.Main + SupervisorJob())
|
|
@@ -186,6 +189,12 @@ class DocumentScannerView(context: ThemedReactContext) : FrameLayout(context), L
|
|
|
186
189
|
}
|
|
187
190
|
lastDetectionTimestamp = now
|
|
188
191
|
|
|
192
|
+
if (rectangle != null && imageWidth > 0 && imageHeight > 0) {
|
|
193
|
+
lastDetectedRectangle = rectangle
|
|
194
|
+
lastDetectedImageWidth = imageWidth
|
|
195
|
+
lastDetectedImageHeight = imageHeight
|
|
196
|
+
}
|
|
197
|
+
|
|
189
198
|
val rectangleOnScreen = if (rectangle != null && width > 0 && height > 0) {
|
|
190
199
|
DocumentDetector.transformRectangleToViewCoordinates(rectangle, imageWidth, imageHeight, width, height)
|
|
191
200
|
} else {
|
|
@@ -313,12 +322,21 @@ class DocumentScannerView(context: ThemedReactContext) : FrameLayout(context), L
|
|
|
313
322
|
// Detect rectangle in captured image
|
|
314
323
|
val bitmap = BitmapFactory.decodeFile(imageFile.absolutePath)
|
|
315
324
|
?: throw IllegalStateException("decode_failed")
|
|
316
|
-
|
|
325
|
+
var detectedRectangle = try {
|
|
317
326
|
DocumentDetector.detectRectangle(bitmap)
|
|
318
327
|
} catch (e: Exception) {
|
|
319
328
|
Log.w(TAG, "Rectangle detection failed, using original image", e)
|
|
320
329
|
null
|
|
321
330
|
}
|
|
331
|
+
if (detectedRectangle == null && lastDetectedRectangle != null && lastDetectedImageWidth > 0 && lastDetectedImageHeight > 0) {
|
|
332
|
+
detectedRectangle = scaleRectangleToBitmap(
|
|
333
|
+
lastDetectedRectangle!!,
|
|
334
|
+
lastDetectedImageWidth,
|
|
335
|
+
lastDetectedImageHeight,
|
|
336
|
+
bitmap.width,
|
|
337
|
+
bitmap.height
|
|
338
|
+
)
|
|
339
|
+
}
|
|
322
340
|
|
|
323
341
|
// Process image with detected rectangle
|
|
324
342
|
val shouldCrop = detectedRectangle != null && stableCounter > 0
|
|
@@ -580,6 +598,27 @@ class DocumentScannerView(context: ThemedReactContext) : FrameLayout(context), L
|
|
|
580
598
|
}
|
|
581
599
|
}
|
|
582
600
|
}
|
|
601
|
+
|
|
602
|
+
private fun scaleRectangleToBitmap(
|
|
603
|
+
rectangle: Rectangle,
|
|
604
|
+
srcWidth: Int,
|
|
605
|
+
srcHeight: Int,
|
|
606
|
+
dstWidth: Int,
|
|
607
|
+
dstHeight: Int
|
|
608
|
+
): Rectangle {
|
|
609
|
+
if (srcWidth == 0 || srcHeight == 0) return rectangle
|
|
610
|
+
val scaleX = dstWidth.toDouble() / srcWidth.toDouble()
|
|
611
|
+
val scaleY = dstHeight.toDouble() / srcHeight.toDouble()
|
|
612
|
+
fun mapPoint(point: Point): Point {
|
|
613
|
+
return Point(point.x * scaleX, point.y * scaleY)
|
|
614
|
+
}
|
|
615
|
+
return Rectangle(
|
|
616
|
+
mapPoint(rectangle.topLeft),
|
|
617
|
+
mapPoint(rectangle.topRight),
|
|
618
|
+
mapPoint(rectangle.bottomLeft),
|
|
619
|
+
mapPoint(rectangle.bottomRight)
|
|
620
|
+
)
|
|
621
|
+
}
|
|
583
622
|
}
|
|
584
623
|
|
|
585
624
|
/**
|
package/dist/DocScanner.js
CHANGED
|
@@ -334,7 +334,9 @@ exports.DocScanner = (0, react_1.forwardRef)(({ onCapture, overlayColor = DEFAUL
|
|
|
334
334
|
captureOriginRef.current = 'auto';
|
|
335
335
|
},
|
|
336
336
|
}), [capture]);
|
|
337
|
-
const overlayPolygon =
|
|
337
|
+
const overlayPolygon = react_native_1.Platform.OS === 'android'
|
|
338
|
+
? detectedRectangle?.rectangleOnScreen ?? null
|
|
339
|
+
: detectedRectangle?.rectangleOnScreen ?? detectedRectangle?.rectangleCoordinates ?? null;
|
|
338
340
|
const overlayIsActive = autoCapture ? isAutoCapturing : (detectedRectangle?.stableCounter ?? 0) > 0;
|
|
339
341
|
const detectionThreshold = autoCapture ? minStableFrames : 99999;
|
|
340
342
|
return (react_1.default.createElement(react_native_1.View, { style: styles.container },
|
package/package.json
CHANGED
package/src/DocScanner.tsx
CHANGED
|
@@ -452,7 +452,10 @@ export const DocScanner = forwardRef<DocScannerHandle, Props>(
|
|
|
452
452
|
[capture],
|
|
453
453
|
);
|
|
454
454
|
|
|
455
|
-
const overlayPolygon =
|
|
455
|
+
const overlayPolygon =
|
|
456
|
+
Platform.OS === 'android'
|
|
457
|
+
? detectedRectangle?.rectangleOnScreen ?? null
|
|
458
|
+
: detectedRectangle?.rectangleOnScreen ?? detectedRectangle?.rectangleCoordinates ?? null;
|
|
456
459
|
const overlayIsActive = autoCapture ? isAutoCapturing : (detectedRectangle?.stableCounter ?? 0) > 0;
|
|
457
460
|
|
|
458
461
|
const detectionThreshold = autoCapture ? minStableFrames : 99999;
|