react-native-rectangle-doc-scanner 10.12.0 → 10.13.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,50 @@ 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
478
+ // Sensor orientation 90° = needs 270° rotation (or -90°)
479
+ // Sensor orientation 270° = needs 90° rotation
480
+ val rotationDegrees = when (sensorOrientation) {
481
+ 0 -> 0f
482
+ 90 -> 270f // Most common for back camera
483
+ 180 -> 180f
484
+ 270 -> 90f // Some devices
485
+ else -> 270f
486
+ }
487
+
488
+ Log.d(TAG, "[TRANSFORM] Applying rotation: ${rotationDegrees}°")
489
+
490
+ if (rotationDegrees != 0f) {
491
+ matrix.postRotate(rotationDegrees, centerX, centerY)
492
+ }
461
493
 
462
- // After rotation, the buffer dimensions are swapped
463
- val rotatedBufferWidth = bufferHeight // 1088
464
- val rotatedBufferHeight = bufferWidth // 1440
494
+ // After rotation, determine effective buffer size
495
+ val rotatedBufferWidth = if (rotationDegrees == 90f || rotationDegrees == 270f) {
496
+ bufferHeight
497
+ } else {
498
+ bufferWidth
499
+ }
500
+ val rotatedBufferHeight = if (rotationDegrees == 90f || rotationDegrees == 270f) {
501
+ bufferWidth
502
+ } else {
503
+ bufferHeight
504
+ }
465
505
 
466
506
  // Scale to fill the view while maintaining aspect ratio
467
507
  val scaleX = viewWidth.toFloat() / rotatedBufferWidth.toFloat()
468
508
  val scaleY = viewHeight.toFloat() / rotatedBufferHeight.toFloat()
469
509
  val scale = scaleX.coerceAtLeast(scaleY) // Use max to fill
470
510
 
471
- Log.d(TAG, "[TRANSFORM] Rotation: 270°, ScaleX: $scaleX, ScaleY: $scaleY, Using: $scale")
511
+ Log.d(TAG, "[TRANSFORM] Rotated buffer: ${rotatedBufferWidth}x${rotatedBufferHeight}, ScaleX: $scaleX, ScaleY: $scaleY, Using: $scale")
472
512
 
473
513
  matrix.postScale(scale, scale, centerX, centerY)
474
514
 
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.13.0",
4
4
  "description": "Native-backed document scanner for React Native with customizable overlays.",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",