react-native-rectangle-doc-scanner 3.245.0 → 3.247.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.
@@ -7,6 +7,7 @@ import android.graphics.Color
7
7
  import android.graphics.Paint
8
8
  import android.graphics.PorterDuff
9
9
  import android.graphics.PorterDuffXfermode
10
+ import org.opencv.core.Point
10
11
  import android.util.Log
11
12
  import android.view.TextureView
12
13
  import android.view.View
@@ -50,6 +51,9 @@ class DocumentScannerView(context: ThemedReactContext) : FrameLayout(context), L
50
51
  private var lastDetectionTimestamp: Long = 0L
51
52
  private var isCapturing = false
52
53
  private var isDetaching = false
54
+ private var lastDetectedRectangle: Rectangle? = null
55
+ private var lastDetectedImageWidth = 0
56
+ private var lastDetectedImageHeight = 0
53
57
 
54
58
  // Coroutine scope for async operations
55
59
  private val scope = CoroutineScope(Dispatchers.Main + SupervisorJob())
@@ -186,6 +190,12 @@ class DocumentScannerView(context: ThemedReactContext) : FrameLayout(context), L
186
190
  }
187
191
  lastDetectionTimestamp = now
188
192
 
193
+ if (rectangle != null && imageWidth > 0 && imageHeight > 0) {
194
+ lastDetectedRectangle = rectangle
195
+ lastDetectedImageWidth = imageWidth
196
+ lastDetectedImageHeight = imageHeight
197
+ }
198
+
189
199
  val rectangleOnScreen = if (rectangle != null && width > 0 && height > 0) {
190
200
  DocumentDetector.transformRectangleToViewCoordinates(rectangle, imageWidth, imageHeight, width, height)
191
201
  } else {
@@ -313,12 +323,21 @@ class DocumentScannerView(context: ThemedReactContext) : FrameLayout(context), L
313
323
  // Detect rectangle in captured image
314
324
  val bitmap = BitmapFactory.decodeFile(imageFile.absolutePath)
315
325
  ?: throw IllegalStateException("decode_failed")
316
- val detectedRectangle = try {
326
+ var detectedRectangle = try {
317
327
  DocumentDetector.detectRectangle(bitmap)
318
328
  } catch (e: Exception) {
319
329
  Log.w(TAG, "Rectangle detection failed, using original image", e)
320
330
  null
321
331
  }
332
+ if (detectedRectangle == null && lastDetectedRectangle != null && lastDetectedImageWidth > 0 && lastDetectedImageHeight > 0) {
333
+ detectedRectangle = scaleRectangleToBitmap(
334
+ lastDetectedRectangle!!,
335
+ lastDetectedImageWidth,
336
+ lastDetectedImageHeight,
337
+ bitmap.width,
338
+ bitmap.height
339
+ )
340
+ }
322
341
 
323
342
  // Process image with detected rectangle
324
343
  val shouldCrop = detectedRectangle != null && stableCounter > 0
@@ -580,6 +599,27 @@ class DocumentScannerView(context: ThemedReactContext) : FrameLayout(context), L
580
599
  }
581
600
  }
582
601
  }
602
+
603
+ private fun scaleRectangleToBitmap(
604
+ rectangle: Rectangle,
605
+ srcWidth: Int,
606
+ srcHeight: Int,
607
+ dstWidth: Int,
608
+ dstHeight: Int
609
+ ): Rectangle {
610
+ if (srcWidth == 0 || srcHeight == 0) return rectangle
611
+ val scaleX = dstWidth.toDouble() / srcWidth.toDouble()
612
+ val scaleY = dstHeight.toDouble() / srcHeight.toDouble()
613
+ fun mapPoint(point: Point): Point {
614
+ return Point(point.x * scaleX, point.y * scaleY)
615
+ }
616
+ return Rectangle(
617
+ mapPoint(rectangle.topLeft),
618
+ mapPoint(rectangle.topRight),
619
+ mapPoint(rectangle.bottomLeft),
620
+ mapPoint(rectangle.bottomRight)
621
+ )
622
+ }
583
623
  }
584
624
 
585
625
  /**
@@ -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 = detectedRectangle?.rectangleOnScreen ?? detectedRectangle?.rectangleCoordinates ?? null;
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-rectangle-doc-scanner",
3
- "version": "3.245.0",
3
+ "version": "3.247.0",
4
4
  "description": "Native-backed document scanner for React Native with customizable overlays.",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
@@ -452,7 +452,10 @@ export const DocScanner = forwardRef<DocScannerHandle, Props>(
452
452
  [capture],
453
453
  );
454
454
 
455
- const overlayPolygon = detectedRectangle?.rectangleOnScreen ?? detectedRectangle?.rectangleCoordinates ?? null;
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;