react-native-rectangle-doc-scanner 3.243.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
@@ -368,11 +370,37 @@ class CameraController(
368
370
 
369
371
  private fun analyzeImage(image: Image) {
370
372
  val rotationDegrees = computeRotationDegrees()
373
+ val imageWidth = image.width
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
+
371
394
  val inputImage = try {
372
- InputImage.fromMediaImage(image, rotationDegrees)
395
+ InputImage.fromByteArray(
396
+ nv21,
397
+ imageWidth,
398
+ imageHeight,
399
+ rotationDegrees,
400
+ InputImage.IMAGE_FORMAT_NV21
401
+ )
373
402
  } catch (e: Exception) {
374
403
  Log.e(TAG, "[CAMERA2] Failed to create InputImage", e)
375
- image.close()
376
404
  analysisInFlight.set(false)
377
405
  return
378
406
  }
@@ -386,19 +414,20 @@ class CameraController(
386
414
  val mlBox = best?.boundingBox
387
415
  val rectangle = when {
388
416
  mlBox == null -> null
389
- shouldRefineWithOpenCv() -> refineWithOpenCv(image, rotationDegrees, mlBox) ?: boxToRectangle(mlBox)
417
+ shouldRefineWithOpenCv() ->
418
+ refineWithOpenCv(nv21, imageWidth, imageHeight, rotationDegrees, mlBox)
419
+ ?: boxToRectangle(mlBox)
390
420
  else -> boxToRectangle(mlBox)
391
421
  }
392
422
 
393
- val frameWidth = if (rotationDegrees == 90 || rotationDegrees == 270) image.height else image.width
394
- val frameHeight = if (rotationDegrees == 90 || rotationDegrees == 270) image.width else image.height
423
+ val frameWidth = if (rotationDegrees == 90 || rotationDegrees == 270) imageHeight else imageWidth
424
+ val frameHeight = if (rotationDegrees == 90 || rotationDegrees == 270) imageWidth else imageHeight
395
425
  onFrameAnalyzed?.invoke(smoothRectangle(rectangle), frameWidth, frameHeight)
396
426
  }
397
427
  .addOnFailureListener { e ->
398
428
  Log.e(TAG, "[CAMERA2] ML Kit detection failed", e)
399
429
  }
400
430
  .addOnCompleteListener {
401
- image.close()
402
431
  analysisInFlight.set(false)
403
432
  }
404
433
  }
@@ -448,9 +477,6 @@ class CameraController(
448
477
  }
449
478
 
450
479
  private fun computeRotationDegrees(): Int {
451
- val cameraId = selectCameraId() ?: return 0
452
- val characteristics = cameraManager.getCameraCharacteristics(cameraId)
453
- val sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION) ?: 0
454
480
  val displayRotation = displayRotationDegrees()
455
481
  return if (useFrontCamera) {
456
482
  (sensorOrientation + displayRotation) % 360
@@ -476,7 +502,7 @@ class CameraController(
476
502
  val preview = previewSize ?: return
477
503
  if (viewWidth == 0f || viewHeight == 0f) return
478
504
 
479
- val rotation = displayRotationDegrees()
505
+ val rotation = computeRotationDegrees()
480
506
  val bufferWidth = if (rotation == 90 || rotation == 270) preview.height.toFloat() else preview.width.toFloat()
481
507
  val bufferHeight = if (rotation == 90 || rotation == 270) preview.width.toFloat() else preview.height.toFloat()
482
508
 
@@ -551,16 +577,21 @@ class CameraController(
551
577
  return true
552
578
  }
553
579
 
554
- 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? {
555
587
  return try {
556
- val nv21 = imageToNv21(image)
557
- val uprightWidth = if (rotationDegrees == 90 || rotationDegrees == 270) image.height else image.width
558
- 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
559
590
  val expanded = expandRect(mlBox, uprightWidth, uprightHeight, 0.2f)
560
591
  val openCvRect = DocumentDetector.detectRectangleInYUVWithRoi(
561
592
  nv21,
562
- image.width,
563
- image.height,
593
+ imageWidth,
594
+ imageHeight,
564
595
  rotationDegrees,
565
596
  expanded
566
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.243.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",