react-native-rectangle-doc-scanner 3.203.0 → 3.205.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.
@@ -65,8 +65,8 @@ class CameraController(
65
65
 
66
66
  companion object {
67
67
  private const val TAG = "CameraController"
68
- private const val MAX_PREVIEW_WIDTH = 2560
69
- private const val MAX_PREVIEW_HEIGHT = 1440
68
+ private const val MAX_ANALYSIS_WIDTH = 1280
69
+ private const val MAX_ANALYSIS_HEIGHT = 720
70
70
  }
71
71
 
72
72
  private data class LastFrame(
@@ -267,8 +267,8 @@ class CameraController(
267
267
  null
268
268
  }
269
269
 
270
- previewSize = chooseSize(previewChoices, MAX_PREVIEW_WIDTH, MAX_PREVIEW_HEIGHT, targetRatio)
271
- analysisSize = chooseSize(analysisChoices, MAX_PREVIEW_WIDTH, MAX_PREVIEW_HEIGHT, targetRatio)
270
+ previewSize = choosePreviewSize(previewChoices, targetRatio)
271
+ analysisSize = chooseAnalysisSize(analysisChoices, targetRatio)
272
272
  Log.d(TAG, "[CAMERA2] Selected sizes - preview: $previewSize, analysis: $analysisSize")
273
273
  }
274
274
 
@@ -386,8 +386,8 @@ class CameraController(
386
386
  null
387
387
  }
388
388
 
389
- val newPreview = chooseSize(previewChoices, MAX_PREVIEW_WIDTH, MAX_PREVIEW_HEIGHT, targetRatio)
390
- val newAnalysis = chooseSize(analysisChoices, MAX_PREVIEW_WIDTH, MAX_PREVIEW_HEIGHT, targetRatio)
389
+ val newPreview = choosePreviewSize(previewChoices, targetRatio)
390
+ val newAnalysis = chooseAnalysisSize(analysisChoices, targetRatio)
391
391
 
392
392
  if (newPreview != null && newPreview != previewSize) {
393
393
  previewSize = newPreview
@@ -401,7 +401,7 @@ class CameraController(
401
401
 
402
402
  private fun updatePreviewTransform() {
403
403
  val previewSize = previewSize ?: return
404
- adjustPreviewLayout(previewSize)
404
+ ensureMatchParent()
405
405
 
406
406
  val viewWidth = previewView.width
407
407
  val viewHeight = previewView.height
@@ -423,7 +423,8 @@ class CameraController(
423
423
  bufferHeight
424
424
  }
425
425
 
426
- val scale = kotlin.math.min(
426
+ // Fill the view like the default camera preview (center-crop).
427
+ val scale = kotlin.math.max(
427
428
  viewWidth.toFloat() / rotatedBufferWidth,
428
429
  viewHeight.toFloat() / rotatedBufferHeight
429
430
  )
@@ -440,7 +441,7 @@ class CameraController(
440
441
  previewView.setTransform(matrix)
441
442
  }
442
443
 
443
- private fun adjustPreviewLayout(previewSize: Size) {
444
+ private fun ensureMatchParent() {
444
445
  val parentView = previewView.parent as? android.view.View ?: return
445
446
  val parentWidth = parentView.width
446
447
  val parentHeight = parentView.height
@@ -448,32 +449,16 @@ class CameraController(
448
449
  return
449
450
  }
450
451
 
451
- val rotationDegrees = getRotationDegrees()
452
- val bufferWidth = previewSize.width.toFloat()
453
- val bufferHeight = previewSize.height.toFloat()
454
- val bufferAspect = if (rotationDegrees == 90 || rotationDegrees == 270) {
455
- bufferHeight / bufferWidth
456
- } else {
457
- bufferWidth / bufferHeight
458
- }
459
-
460
- val parentAspect = parentWidth.toFloat() / parentHeight.toFloat()
461
- val targetWidth: Int
462
- val targetHeight: Int
463
-
464
- if (parentAspect > bufferAspect) {
465
- targetHeight = parentHeight
466
- targetWidth = (parentHeight * bufferAspect).toInt()
467
- } else {
468
- targetWidth = parentWidth
469
- targetHeight = (parentWidth / bufferAspect).toInt()
470
- }
471
-
472
452
  val layoutParams = (previewView.layoutParams as? android.widget.FrameLayout.LayoutParams)
473
- ?: android.widget.FrameLayout.LayoutParams(targetWidth, targetHeight)
474
- if (layoutParams.width != targetWidth || layoutParams.height != targetHeight) {
475
- layoutParams.width = targetWidth
476
- layoutParams.height = targetHeight
453
+ ?: android.widget.FrameLayout.LayoutParams(
454
+ android.widget.FrameLayout.LayoutParams.MATCH_PARENT,
455
+ android.widget.FrameLayout.LayoutParams.MATCH_PARENT
456
+ )
457
+ if (layoutParams.width != android.widget.FrameLayout.LayoutParams.MATCH_PARENT ||
458
+ layoutParams.height != android.widget.FrameLayout.LayoutParams.MATCH_PARENT
459
+ ) {
460
+ layoutParams.width = android.widget.FrameLayout.LayoutParams.MATCH_PARENT
461
+ layoutParams.height = android.widget.FrameLayout.LayoutParams.MATCH_PARENT
477
462
  layoutParams.gravity = Gravity.CENTER
478
463
  previewView.layoutParams = layoutParams
479
464
  }
@@ -593,20 +578,48 @@ class CameraController(
593
578
  }
594
579
  }
595
580
 
596
- private fun chooseSize(
581
+ private fun choosePreviewSize(
582
+ choices: Array<Size>,
583
+ targetRatio: Float?
584
+ ): Size? {
585
+ if (choices.isEmpty()) {
586
+ return null
587
+ }
588
+ val candidates = choices.toList()
589
+
590
+ if (targetRatio == null) {
591
+ return candidates.maxByOrNull { it.width * it.height }
592
+ }
593
+
594
+ val normalizedTarget = targetRatio
595
+ val sorted = candidates.sortedWith(
596
+ compareBy<Size> { size ->
597
+ val ratio = if (normalizedTarget < 1f) {
598
+ size.height.toFloat() / size.width.toFloat()
599
+ } else {
600
+ size.width.toFloat() / size.height.toFloat()
601
+ }
602
+ kotlin.math.abs(ratio - normalizedTarget)
603
+ }.thenByDescending { size ->
604
+ size.width * size.height
605
+ }
606
+ )
607
+ return sorted.first()
608
+ }
609
+
610
+ private fun chooseAnalysisSize(
597
611
  choices: Array<Size>,
598
- maxWidth: Int,
599
- maxHeight: Int,
600
612
  targetRatio: Float?
601
613
  ): Size? {
602
614
  if (choices.isEmpty()) {
603
615
  return null
604
616
  }
605
- val maxCandidates = choices.filter { it.width <= maxWidth && it.height <= maxHeight }
606
- val candidates = if (maxCandidates.isNotEmpty()) maxCandidates else choices.toList()
617
+
618
+ val capped = choices.filter { it.width <= MAX_ANALYSIS_WIDTH && it.height <= MAX_ANALYSIS_HEIGHT }
619
+ val candidates = if (capped.isNotEmpty()) capped else choices.toList()
607
620
 
608
621
  if (targetRatio == null) {
609
- return candidates.sortedBy { it.width * it.height }.last()
622
+ return candidates.maxByOrNull { it.width * it.height }
610
623
  }
611
624
 
612
625
  val normalizedTarget = targetRatio
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-rectangle-doc-scanner",
3
- "version": "3.203.0",
3
+ "version": "3.205.0",
4
4
  "description": "Native-backed document scanner for React Native with customizable overlays.",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",