react-native-rectangle-doc-scanner 3.244.0 → 3.245.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
  private var previewSize: Size? = null
49
49
  private var analysisSize: Size? = null
50
50
  private var captureSize: Size? = null
51
+ private var sensorOrientation: Int = 0
51
52
 
52
53
  private var yuvReader: ImageReader? = null
53
54
  private var jpegReader: ImageReader? = null
@@ -207,6 +208,7 @@ class CameraController(
207
208
  val characteristics = cameraManager.getCameraCharacteristics(cameraId)
208
209
  val streamConfigMap = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
209
210
  ?: return
211
+ sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION) ?: 0
210
212
 
211
213
  val viewAspect = if (previewView.height == 0) {
212
214
  1.0
@@ -370,11 +372,35 @@ class CameraController(
370
372
  val rotationDegrees = computeRotationDegrees()
371
373
  val imageWidth = image.width
372
374
  val imageHeight = image.height
375
+ val nv21 = try {
376
+ imageToNv21(image)
377
+ } catch (e: Exception) {
378
+ Log.e(TAG, "[CAMERA2] Failed to read image buffer", e)
379
+ try {
380
+ image.close()
381
+ } catch (closeError: Exception) {
382
+ Log.w(TAG, "[CAMERA2] Failed to close image", closeError)
383
+ }
384
+ analysisInFlight.set(false)
385
+ return
386
+ } finally {
387
+ try {
388
+ image.close()
389
+ } catch (e: Exception) {
390
+ Log.w(TAG, "[CAMERA2] Failed to close image", e)
391
+ }
392
+ }
393
+
373
394
  val inputImage = try {
374
- InputImage.fromMediaImage(image, rotationDegrees)
395
+ InputImage.fromByteArray(
396
+ nv21,
397
+ imageWidth,
398
+ imageHeight,
399
+ rotationDegrees,
400
+ InputImage.IMAGE_FORMAT_NV21
401
+ )
375
402
  } catch (e: Exception) {
376
403
  Log.e(TAG, "[CAMERA2] Failed to create InputImage", e)
377
- image.close()
378
404
  analysisInFlight.set(false)
379
405
  return
380
406
  }
@@ -388,7 +414,9 @@ class CameraController(
388
414
  val mlBox = best?.boundingBox
389
415
  val rectangle = when {
390
416
  mlBox == null -> null
391
- shouldRefineWithOpenCv() -> refineWithOpenCv(image, rotationDegrees, mlBox) ?: boxToRectangle(mlBox)
417
+ shouldRefineWithOpenCv() ->
418
+ refineWithOpenCv(nv21, imageWidth, imageHeight, rotationDegrees, mlBox)
419
+ ?: boxToRectangle(mlBox)
392
420
  else -> boxToRectangle(mlBox)
393
421
  }
394
422
 
@@ -400,11 +428,6 @@ class CameraController(
400
428
  Log.e(TAG, "[CAMERA2] ML Kit detection failed", e)
401
429
  }
402
430
  .addOnCompleteListener {
403
- try {
404
- image.close()
405
- } catch (e: Exception) {
406
- Log.w(TAG, "[CAMERA2] Failed to close image", e)
407
- }
408
431
  analysisInFlight.set(false)
409
432
  }
410
433
  }
@@ -454,9 +477,6 @@ class CameraController(
454
477
  }
455
478
 
456
479
  private fun computeRotationDegrees(): Int {
457
- val cameraId = selectCameraId() ?: return 0
458
- val characteristics = cameraManager.getCameraCharacteristics(cameraId)
459
- val sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION) ?: 0
460
480
  val displayRotation = displayRotationDegrees()
461
481
  return if (useFrontCamera) {
462
482
  (sensorOrientation + displayRotation) % 360
@@ -482,7 +502,7 @@ class CameraController(
482
502
  val preview = previewSize ?: return
483
503
  if (viewWidth == 0f || viewHeight == 0f) return
484
504
 
485
- val rotation = displayRotationDegrees()
505
+ val rotation = computeRotationDegrees()
486
506
  val bufferWidth = if (rotation == 90 || rotation == 270) preview.height.toFloat() else preview.width.toFloat()
487
507
  val bufferHeight = if (rotation == 90 || rotation == 270) preview.width.toFloat() else preview.height.toFloat()
488
508
 
@@ -557,16 +577,21 @@ class CameraController(
557
577
  return true
558
578
  }
559
579
 
560
- private fun refineWithOpenCv(image: Image, rotationDegrees: Int, mlBox: Rect): Rectangle? {
580
+ private fun refineWithOpenCv(
581
+ nv21: ByteArray,
582
+ imageWidth: Int,
583
+ imageHeight: Int,
584
+ rotationDegrees: Int,
585
+ mlBox: Rect
586
+ ): Rectangle? {
561
587
  return try {
562
- val nv21 = imageToNv21(image)
563
- val uprightWidth = if (rotationDegrees == 90 || rotationDegrees == 270) image.height else image.width
564
- val uprightHeight = if (rotationDegrees == 90 || rotationDegrees == 270) image.width else image.height
588
+ val uprightWidth = if (rotationDegrees == 90 || rotationDegrees == 270) imageHeight else imageWidth
589
+ val uprightHeight = if (rotationDegrees == 90 || rotationDegrees == 270) imageWidth else imageHeight
565
590
  val expanded = expandRect(mlBox, uprightWidth, uprightHeight, 0.2f)
566
591
  val openCvRect = DocumentDetector.detectRectangleInYUVWithRoi(
567
592
  nv21,
568
- image.width,
569
- image.height,
593
+ imageWidth,
594
+ imageHeight,
570
595
  rotationDegrees,
571
596
  expanded
572
597
  )
@@ -389,9 +389,7 @@ class DocumentScannerView(context: ThemedReactContext) : FrameLayout(context), L
389
389
  }
390
390
 
391
391
  private fun sendPictureTakenEvent(data: WritableMap) {
392
- val event = Arguments.createMap().apply {
393
- merge(data)
394
- }
392
+ val event = data.toHashMap().toWritableMap()
395
393
  themedContext.getJSModule(RCTEventEmitter::class.java)
396
394
  .receiveEvent(id, "onPictureTaken", event)
397
395
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-rectangle-doc-scanner",
3
- "version": "3.244.0",
3
+ "version": "3.245.0",
4
4
  "description": "Native-backed document scanner for React Native with customizable overlays.",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",