react-native-rectangle-doc-scanner 3.207.0 → 3.208.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.
@@ -48,6 +48,7 @@ class CameraController(
48
48
 
49
49
  private var cameraId: String? = null
50
50
  private var sensorOrientation: Int = 0
51
+ private var sensorAspectRatio: Float? = null
51
52
  private var previewSize: Size? = null
52
53
  private var analysisSize: Size? = null
53
54
  private var previewChoices: Array<Size> = emptyArray()
@@ -254,6 +255,10 @@ class CameraController(
254
255
  cameraId = selected
255
256
  val characteristics = cameraManager.getCameraCharacteristics(selected)
256
257
  sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION) ?: 0
258
+ val activeArray = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE)
259
+ sensorAspectRatio = activeArray?.let { rect ->
260
+ if (rect.height() != 0) rect.width().toFloat() / rect.height().toFloat() else null
261
+ }
257
262
 
258
263
  val streamConfig = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
259
264
  previewChoices = streamConfig?.getOutputSizes(SurfaceTexture::class.java) ?: emptyArray()
@@ -267,15 +272,16 @@ class CameraController(
267
272
  null
268
273
  }
269
274
 
270
- logSizeCandidates("preview", previewChoices, targetRatio)
271
- logSizeCandidates("analysis", analysisChoices, targetRatio)
275
+ logSizeCandidates("preview", previewChoices, targetRatio, sensorAspectRatio)
276
+ logSizeCandidates("analysis", analysisChoices, targetRatio, sensorAspectRatio)
272
277
 
273
- previewSize = choosePreviewSize(previewChoices, targetRatio)
274
- analysisSize = chooseAnalysisSize(analysisChoices, targetRatio)
278
+ previewSize = choosePreviewSize(previewChoices, targetRatio, sensorAspectRatio)
279
+ analysisSize = chooseAnalysisSize(analysisChoices, targetRatio, sensorAspectRatio)
275
280
  Log.d(
276
281
  TAG,
277
282
  "[CAMERA2] chooseCamera view=${viewWidth}x${viewHeight} ratio=$targetRatio " +
278
- "sensorOrientation=$sensorOrientation preview=$previewSize analysis=$analysisSize"
283
+ "sensorOrientation=$sensorOrientation sensorRatio=$sensorAspectRatio " +
284
+ "preview=$previewSize analysis=$analysisSize"
279
285
  )
280
286
  }
281
287
 
@@ -399,8 +405,8 @@ class CameraController(
399
405
  null
400
406
  }
401
407
 
402
- val newPreview = choosePreviewSize(previewChoices, targetRatio)
403
- val newAnalysis = chooseAnalysisSize(analysisChoices, targetRatio)
408
+ val newPreview = choosePreviewSize(previewChoices, targetRatio, sensorAspectRatio)
409
+ val newAnalysis = chooseAnalysisSize(analysisChoices, targetRatio, sensorAspectRatio)
404
410
 
405
411
  if (newPreview != null && newPreview != previewSize) {
406
412
  previewSize = newPreview
@@ -604,25 +610,23 @@ class CameraController(
604
610
 
605
611
  private fun choosePreviewSize(
606
612
  choices: Array<Size>,
607
- targetRatio: Float?
613
+ targetRatio: Float?,
614
+ sensorRatio: Float?
608
615
  ): Size? {
609
616
  if (choices.isEmpty()) {
610
617
  return null
611
618
  }
612
619
  val candidates = choices.toList()
613
620
 
614
- if (targetRatio == null) {
621
+ val ratioBase = sensorRatio ?: targetRatio
622
+ if (ratioBase == null) {
615
623
  return candidates.maxByOrNull { it.width * it.height }
616
624
  }
617
625
 
618
- val normalizedTarget = targetRatio
626
+ val normalizedTarget = ratioBase
619
627
  val sorted = candidates.sortedWith(
620
628
  compareBy<Size> { size ->
621
- val ratio = if (normalizedTarget < 1f) {
622
- size.height.toFloat() / size.width.toFloat()
623
- } else {
624
- size.width.toFloat() / size.height.toFloat()
625
- }
629
+ val ratio = size.width.toFloat() / size.height.toFloat()
626
630
  kotlin.math.abs(ratio - normalizedTarget)
627
631
  }.thenByDescending { size ->
628
632
  size.width * size.height
@@ -633,7 +637,8 @@ class CameraController(
633
637
 
634
638
  private fun chooseAnalysisSize(
635
639
  choices: Array<Size>,
636
- targetRatio: Float?
640
+ targetRatio: Float?,
641
+ sensorRatio: Float?
637
642
  ): Size? {
638
643
  if (choices.isEmpty()) {
639
644
  return null
@@ -642,18 +647,15 @@ class CameraController(
642
647
  val capped = choices.filter { it.width <= MAX_ANALYSIS_WIDTH && it.height <= MAX_ANALYSIS_HEIGHT }
643
648
  val candidates = if (capped.isNotEmpty()) capped else choices.toList()
644
649
 
645
- if (targetRatio == null) {
650
+ val ratioBase = sensorRatio ?: targetRatio
651
+ if (ratioBase == null) {
646
652
  return candidates.maxByOrNull { it.width * it.height }
647
653
  }
648
654
 
649
- val normalizedTarget = targetRatio
655
+ val normalizedTarget = ratioBase
650
656
  val sorted = candidates.sortedWith(
651
657
  compareBy<Size> { size ->
652
- val ratio = if (normalizedTarget < 1f) {
653
- size.height.toFloat() / size.width.toFloat()
654
- } else {
655
- size.width.toFloat() / size.height.toFloat()
656
- }
658
+ val ratio = size.width.toFloat() / size.height.toFloat()
657
659
  kotlin.math.abs(ratio - normalizedTarget)
658
660
  }.thenByDescending { size ->
659
661
  size.width * size.height
@@ -665,26 +667,24 @@ class CameraController(
665
667
  private fun logSizeCandidates(
666
668
  label: String,
667
669
  choices: Array<Size>,
668
- targetRatio: Float?
670
+ targetRatio: Float?,
671
+ sensorRatio: Float?
669
672
  ) {
670
673
  if (choices.isEmpty()) {
671
674
  Log.d(TAG, "[CAMERA2] $label sizes: none")
672
675
  return
673
676
  }
674
677
 
675
- if (targetRatio == null) {
676
- Log.d(TAG, "[CAMERA2] $label sizes: ${choices.size}, targetRatio=null")
678
+ val ratioBase = sensorRatio ?: targetRatio
679
+ if (ratioBase == null) {
680
+ Log.d(TAG, "[CAMERA2] $label sizes: ${choices.size}, ratioBase=null")
677
681
  return
678
682
  }
679
683
 
680
- val normalizedTarget = targetRatio
684
+ val normalizedTarget = ratioBase
681
685
  val sorted = choices.sortedWith(
682
686
  compareBy<Size> { size ->
683
- val ratio = if (normalizedTarget < 1f) {
684
- size.height.toFloat() / size.width.toFloat()
685
- } else {
686
- size.width.toFloat() / size.height.toFloat()
687
- }
687
+ val ratio = size.width.toFloat() / size.height.toFloat()
688
688
  kotlin.math.abs(ratio - normalizedTarget)
689
689
  }.thenByDescending { size ->
690
690
  size.width * size.height
@@ -692,16 +692,16 @@ class CameraController(
692
692
  )
693
693
 
694
694
  val top = sorted.take(5).joinToString { size ->
695
- val ratio = if (normalizedTarget < 1f) {
696
- size.height.toFloat() / size.width.toFloat()
697
- } else {
698
- size.width.toFloat() / size.height.toFloat()
699
- }
695
+ val ratio = size.width.toFloat() / size.height.toFloat()
700
696
  val diff = kotlin.math.abs(ratio - normalizedTarget)
701
697
  "${size.width}x${size.height}(r=${"%.3f".format(ratio)},d=${"%.3f".format(diff)})"
702
698
  }
703
699
 
704
- Log.d(TAG, "[CAMERA2] $label sizes: ${choices.size}, target=${"%.3f".format(normalizedTarget)} top=$top")
700
+ Log.d(
701
+ TAG,
702
+ "[CAMERA2] $label sizes: ${choices.size}, ratioBase=${"%.3f".format(normalizedTarget)} " +
703
+ "sensor=${sensorRatio?.let { "%.3f".format(it) }} target=${targetRatio?.let { "%.3f".format(it) }} top=$top"
704
+ )
705
705
  }
706
706
 
707
707
  private fun startBackgroundThread() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-rectangle-doc-scanner",
3
- "version": "3.207.0",
3
+ "version": "3.208.0",
4
4
  "description": "Native-backed document scanner for React Native with customizable overlays.",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",