react-native-rectangle-doc-scanner 3.199.0 → 3.201.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.
@@ -22,6 +22,7 @@ import android.os.Handler
22
22
  import android.os.HandlerThread
23
23
  import android.util.Log
24
24
  import android.util.Size
25
+ import android.view.Gravity
25
26
  import android.view.Surface
26
27
  import android.view.TextureView
27
28
  import androidx.core.content.ContextCompat
@@ -359,6 +360,8 @@ class CameraController(
359
360
 
360
361
  private fun updatePreviewTransform() {
361
362
  val previewSize = previewSize ?: return
363
+ adjustPreviewLayout(previewSize)
364
+
362
365
  val viewWidth = previewView.width
363
366
  val viewHeight = previewView.height
364
367
  if (viewWidth == 0 || viewHeight == 0) {
@@ -366,29 +369,75 @@ class CameraController(
366
369
  }
367
370
 
368
371
  val rotationDegrees = getRotationDegrees()
369
- val matrix = Matrix()
370
- val viewRect = RectF(0f, 0f, viewWidth.toFloat(), viewHeight.toFloat())
371
- val bufferRect = if (rotationDegrees == 90 || rotationDegrees == 270) {
372
- RectF(0f, 0f, previewSize.height.toFloat(), previewSize.width.toFloat())
372
+ val bufferWidth = previewSize.width.toFloat()
373
+ val bufferHeight = previewSize.height.toFloat()
374
+ val rotatedBufferWidth = if (rotationDegrees == 90 || rotationDegrees == 270) {
375
+ bufferHeight
376
+ } else {
377
+ bufferWidth
378
+ }
379
+ val rotatedBufferHeight = if (rotationDegrees == 90 || rotationDegrees == 270) {
380
+ bufferWidth
373
381
  } else {
374
- RectF(0f, 0f, previewSize.width.toFloat(), previewSize.height.toFloat())
382
+ bufferHeight
375
383
  }
376
- val centerX = viewRect.centerX()
377
- val centerY = viewRect.centerY()
378
384
 
379
- bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY())
380
- // Fit entire buffer into view (no crop).
381
- matrix.setRectToRect(bufferRect, viewRect, Matrix.ScaleToFit.CENTER)
385
+ val scale = kotlin.math.min(
386
+ viewWidth.toFloat() / rotatedBufferWidth,
387
+ viewHeight.toFloat() / rotatedBufferHeight
388
+ )
382
389
 
383
- when (rotationDegrees) {
384
- 90 -> matrix.postRotate(90f, centerX, centerY)
385
- 180 -> matrix.postRotate(180f, centerX, centerY)
386
- 270 -> matrix.postRotate(270f, centerX, centerY)
390
+ val matrix = Matrix()
391
+ // Center buffer at origin, rotate, scale to fit, then move to view center.
392
+ matrix.postTranslate(-bufferWidth / 2f, -bufferHeight / 2f)
393
+ if (rotationDegrees != 0) {
394
+ matrix.postRotate(rotationDegrees.toFloat())
387
395
  }
396
+ matrix.postScale(scale, scale)
397
+ matrix.postTranslate(viewWidth / 2f, viewHeight / 2f)
388
398
 
389
399
  previewView.setTransform(matrix)
390
400
  }
391
401
 
402
+ private fun adjustPreviewLayout(previewSize: Size) {
403
+ val parentView = previewView.parent as? android.view.View ?: return
404
+ val parentWidth = parentView.width
405
+ val parentHeight = parentView.height
406
+ if (parentWidth == 0 || parentHeight == 0) {
407
+ return
408
+ }
409
+
410
+ val rotationDegrees = getRotationDegrees()
411
+ val bufferWidth = previewSize.width.toFloat()
412
+ val bufferHeight = previewSize.height.toFloat()
413
+ val bufferAspect = if (rotationDegrees == 90 || rotationDegrees == 270) {
414
+ bufferHeight / bufferWidth
415
+ } else {
416
+ bufferWidth / bufferHeight
417
+ }
418
+
419
+ val parentAspect = parentWidth.toFloat() / parentHeight.toFloat()
420
+ val targetWidth: Int
421
+ val targetHeight: Int
422
+
423
+ if (parentAspect > bufferAspect) {
424
+ targetHeight = parentHeight
425
+ targetWidth = (parentHeight * bufferAspect).toInt()
426
+ } else {
427
+ targetWidth = parentWidth
428
+ targetHeight = (parentWidth / bufferAspect).toInt()
429
+ }
430
+
431
+ val layoutParams = (previewView.layoutParams as? android.widget.FrameLayout.LayoutParams)
432
+ ?: android.widget.FrameLayout.LayoutParams(targetWidth, targetHeight)
433
+ if (layoutParams.width != targetWidth || layoutParams.height != targetHeight) {
434
+ layoutParams.width = targetWidth
435
+ layoutParams.height = targetHeight
436
+ layoutParams.gravity = Gravity.CENTER
437
+ previewView.layoutParams = layoutParams
438
+ }
439
+ }
440
+
392
441
  private fun handleImage(image: Image) {
393
442
  try {
394
443
  val rotationDegrees = getRotationDegrees()
@@ -517,7 +566,11 @@ class CameraController(
517
566
 
518
567
  val ratioFiltered = if (targetRatio != null) {
519
568
  candidates.filter { size ->
520
- val ratio = size.width.toFloat() / size.height.toFloat()
569
+ val ratio = if (targetRatio < 1f) {
570
+ size.height.toFloat() / size.width.toFloat()
571
+ } else {
572
+ size.width.toFloat() / size.height.toFloat()
573
+ }
521
574
  kotlin.math.abs(ratio - targetRatio) <= 0.05f
522
575
  }
523
576
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-rectangle-doc-scanner",
3
- "version": "3.199.0",
3
+ "version": "3.201.0",
4
4
  "description": "Native-backed document scanner for React Native with customizable overlays.",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",