react-native-rectangle-doc-scanner 10.12.0 → 10.14.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.
@@ -2,6 +2,8 @@ package com.reactnativerectangledocscanner
2
2
 
3
3
  import android.content.Context
4
4
  import android.graphics.SurfaceTexture
5
+ import android.hardware.camera2.CameraCharacteristics
6
+ import android.hardware.camera2.CameraManager
5
7
  import android.util.Log
6
8
  import android.util.Size
7
9
  import android.view.Surface
@@ -53,6 +55,20 @@ class CameraController(
53
55
  private const val ANALYSIS_TARGET_RESOLUTION = 1280 // Max dimension for analysis
54
56
  }
55
57
 
58
+ private fun getCameraSensorOrientation(): Int {
59
+ return try {
60
+ val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
61
+ val cameraId = if (useFrontCamera) "1" else "0"
62
+ val characteristics = cameraManager.getCameraCharacteristics(cameraId)
63
+ val sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION) ?: 0
64
+ Log.d(TAG, "[SENSOR] Camera $cameraId sensor orientation: $sensorOrientation")
65
+ sensorOrientation
66
+ } catch (e: Exception) {
67
+ Log.e(TAG, "[SENSOR] Failed to get sensor orientation", e)
68
+ 90 // Default to 90 for most devices
69
+ }
70
+ }
71
+
56
72
  private data class PendingCapture(
57
73
  val outputDirectory: File,
58
74
  val onImageCaptured: (File) -> Unit,
@@ -449,26 +465,52 @@ class CameraController(
449
465
  return
450
466
  }
451
467
 
452
- Log.d(TAG, "[TRANSFORM] View: ${viewWidth}x${viewHeight}, Buffer: ${bufferWidth}x${bufferHeight}")
468
+ // Get sensor orientation to determine correct rotation
469
+ val sensorOrientation = getCameraSensorOrientation()
470
+
471
+ Log.d(TAG, "[TRANSFORM] View: ${viewWidth}x${viewHeight}, Buffer: ${bufferWidth}x${bufferHeight}, Sensor: ${sensorOrientation}°")
453
472
 
454
473
  val matrix = android.graphics.Matrix()
455
474
  val centerX = viewWidth / 2f
456
475
  val centerY = viewHeight / 2f
457
476
 
458
- // Camera sensor is landscape (1440x1088), but we want portrait display
459
- // Rotate 270 degrees (or -90 degrees) to make it portrait with correct orientation
460
- matrix.postRotate(270f, centerX, centerY)
477
+ // Calculate rotation needed for portrait display
478
+ // The rotation is inverse of sensor orientation
479
+ // Sensor 0° = device held naturally shows landscape, need 270° to portrait
480
+ // Sensor 90° = device held naturally shows portrait, need 90° correction
481
+ // Sensor 270° = need 270° correction
482
+ val rotationDegrees = when (sensorOrientation) {
483
+ 0 -> 270f // Tablet case - sensor is landscape, rotate to portrait
484
+ 90 -> 90f // Most phones - sensor already portrait-ish, small correction
485
+ 180 -> 180f
486
+ 270 -> 270f
487
+ else -> 90f
488
+ }
489
+
490
+ Log.d(TAG, "[TRANSFORM] Applying rotation: ${rotationDegrees}°")
491
+
492
+ if (rotationDegrees != 0f) {
493
+ matrix.postRotate(rotationDegrees, centerX, centerY)
494
+ }
461
495
 
462
- // After rotation, the buffer dimensions are swapped
463
- val rotatedBufferWidth = bufferHeight // 1088
464
- val rotatedBufferHeight = bufferWidth // 1440
496
+ // After rotation, determine effective buffer size
497
+ val rotatedBufferWidth = if (rotationDegrees == 90f || rotationDegrees == 270f) {
498
+ bufferHeight
499
+ } else {
500
+ bufferWidth
501
+ }
502
+ val rotatedBufferHeight = if (rotationDegrees == 90f || rotationDegrees == 270f) {
503
+ bufferWidth
504
+ } else {
505
+ bufferHeight
506
+ }
465
507
 
466
508
  // Scale to fill the view while maintaining aspect ratio
467
509
  val scaleX = viewWidth.toFloat() / rotatedBufferWidth.toFloat()
468
510
  val scaleY = viewHeight.toFloat() / rotatedBufferHeight.toFloat()
469
511
  val scale = scaleX.coerceAtLeast(scaleY) // Use max to fill
470
512
 
471
- Log.d(TAG, "[TRANSFORM] Rotation: 270°, ScaleX: $scaleX, ScaleY: $scaleY, Using: $scale")
513
+ Log.d(TAG, "[TRANSFORM] Rotated buffer: ${rotatedBufferWidth}x${rotatedBufferHeight}, ScaleX: $scaleX, ScaleY: $scaleY, Using: $scale")
472
514
 
473
515
  matrix.postScale(scale, scale, centerX, centerY)
474
516
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-rectangle-doc-scanner",
3
- "version": "10.12.0",
3
+ "version": "10.14.0",
4
4
  "description": "Native-backed document scanner for React Native with customizable overlays.",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",