react-native-rectangle-doc-scanner 3.138.0 → 3.140.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.
@@ -6,6 +6,7 @@ import org.opencv.android.Utils
6
6
  import org.opencv.core.*
7
7
  import org.opencv.imgproc.Imgproc
8
8
  import kotlin.math.abs
9
+ import kotlin.math.max
9
10
  import kotlin.math.sqrt
10
11
 
11
12
  data class Rectangle(
@@ -249,14 +250,34 @@ class DocumentDetector {
249
250
  viewWidth: Int,
250
251
  viewHeight: Int
251
252
  ): Rectangle {
252
- val scaleX = viewWidth.toDouble() / imageWidth
253
- val scaleY = viewHeight.toDouble() / imageHeight
253
+ if (imageWidth == 0 || imageHeight == 0 || viewWidth == 0 || viewHeight == 0) {
254
+ return rectangle
255
+ }
256
+
257
+ val scale = max(
258
+ viewWidth.toDouble() / imageWidth.toDouble(),
259
+ viewHeight.toDouble() / imageHeight.toDouble()
260
+ )
261
+
262
+ val scaledImageWidth = imageWidth * scale
263
+ val scaledImageHeight = imageHeight * scale
264
+ val offsetX = (scaledImageWidth - viewWidth) / 2.0
265
+ val offsetY = (scaledImageHeight - viewHeight) / 2.0
266
+
267
+ fun mapPoint(point: Point): Point {
268
+ val x = (point.x * scale) - offsetX
269
+ val y = (point.y * scale) - offsetY
270
+ return Point(
271
+ x.coerceIn(0.0, viewWidth.toDouble()),
272
+ y.coerceIn(0.0, viewHeight.toDouble())
273
+ )
274
+ }
254
275
 
255
276
  return Rectangle(
256
- Point(rectangle.topLeft.x * scaleX, rectangle.topLeft.y * scaleY),
257
- Point(rectangle.topRight.x * scaleX, rectangle.topRight.y * scaleY),
258
- Point(rectangle.bottomLeft.x * scaleX, rectangle.bottomLeft.y * scaleY),
259
- Point(rectangle.bottomRight.x * scaleX, rectangle.bottomRight.y * scaleY)
277
+ mapPoint(rectangle.topLeft),
278
+ mapPoint(rectangle.topRight),
279
+ mapPoint(rectangle.bottomLeft),
280
+ mapPoint(rectangle.bottomRight)
260
281
  )
261
282
  }
262
283
  }
@@ -28,8 +28,8 @@ class DocumentScannerView(context: ThemedReactContext) : FrameLayout(context) {
28
28
 
29
29
  // Props (matching iOS)
30
30
  var overlayColor: Int = Color.parseColor("#80FFFFFF")
31
- var enableTorch: Boolean = false
32
- var useFrontCam: Boolean = false
31
+ private var isTorchEnabled: Boolean = false
32
+ private var isUsingFrontCamera: Boolean = false
33
33
  var useBase64: Boolean = false
34
34
  var saveInAppDocument: Boolean = false
35
35
  var captureMultiple: Boolean = false
@@ -83,7 +83,10 @@ class DocumentScannerView(context: ThemedReactContext) : FrameLayout(context) {
83
83
  handleDetectionResult(rectangle, imageWidth, imageHeight)
84
84
  }
85
85
  lastDetectionTimestamp = 0L
86
- cameraController?.startCamera(useFrontCam, true)
86
+ cameraController?.startCamera(isUsingFrontCamera, true)
87
+ if (isTorchEnabled) {
88
+ cameraController?.setTorchEnabled(true)
89
+ }
87
90
 
88
91
  Log.d(TAG, "Camera setup completed")
89
92
  } catch (e: Exception) {
@@ -317,13 +320,13 @@ class DocumentScannerView(context: ThemedReactContext) : FrameLayout(context) {
317
320
  }
318
321
 
319
322
  fun setEnableTorch(enabled: Boolean) {
320
- this.enableTorch = enabled
323
+ isTorchEnabled = enabled
321
324
  cameraController?.setTorchEnabled(enabled)
322
325
  }
323
326
 
324
327
  fun setUseFrontCam(enabled: Boolean) {
325
- if (this.useFrontCam != enabled) {
326
- this.useFrontCam = enabled
328
+ if (isUsingFrontCamera != enabled) {
329
+ isUsingFrontCamera = enabled
327
330
  cameraController?.stopCamera()
328
331
  setupCamera()
329
332
  }
@@ -334,7 +337,10 @@ class DocumentScannerView(context: ThemedReactContext) : FrameLayout(context) {
334
337
  cameraController?.onFrameAnalyzed = { rectangle, imageWidth, imageHeight ->
335
338
  handleDetectionResult(rectangle, imageWidth, imageHeight)
336
339
  }
337
- cameraController?.startCamera(useFrontCam, true)
340
+ cameraController?.startCamera(isUsingFrontCamera, true)
341
+ if (isTorchEnabled) {
342
+ cameraController?.setTorchEnabled(true)
343
+ }
338
344
  }
339
345
 
340
346
  fun stopCamera() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-rectangle-doc-scanner",
3
- "version": "3.138.0",
3
+ "version": "3.140.0",
4
4
  "description": "Native-backed document scanner for React Native with customizable overlays.",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",