react-native-rectangle-doc-scanner 3.192.0 → 3.194.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.
|
@@ -11,6 +11,8 @@ import androidx.camera.view.PreviewView
|
|
|
11
11
|
import androidx.core.content.ContextCompat
|
|
12
12
|
import androidx.lifecycle.Lifecycle
|
|
13
13
|
import androidx.lifecycle.LifecycleOwner
|
|
14
|
+
import androidx.lifecycle.LiveData
|
|
15
|
+
import androidx.lifecycle.Observer
|
|
14
16
|
import java.io.File
|
|
15
17
|
import java.util.concurrent.ExecutorService
|
|
16
18
|
import java.util.concurrent.Executors
|
|
@@ -30,6 +32,9 @@ class CameraController(
|
|
|
30
32
|
private var torchEnabled = false
|
|
31
33
|
private var detectionEnabled = true
|
|
32
34
|
private var isCaptureSession = false
|
|
35
|
+
private var hasFallbackAttempted = false
|
|
36
|
+
private var cameraStateLiveData: LiveData<CameraState>? = null
|
|
37
|
+
private var cameraStateObserver: Observer<CameraState>? = null
|
|
33
38
|
|
|
34
39
|
var onFrameAnalyzed: ((Rectangle?, Int, Int) -> Unit)? = null
|
|
35
40
|
|
|
@@ -114,11 +119,9 @@ class CameraController(
|
|
|
114
119
|
|
|
115
120
|
val targetRotation = previewView.display?.rotation ?: Surface.ROTATION_0
|
|
116
121
|
|
|
117
|
-
// Preview use case
|
|
122
|
+
// Preview use case (avoid forcing a size to let CameraX pick a compatible stream)
|
|
118
123
|
Log.d(TAG, "[BIND] Creating Preview use case...")
|
|
119
124
|
val preview = Preview.Builder()
|
|
120
|
-
// Use a modest fixed size to avoid stream configuration timeouts on some devices.
|
|
121
|
-
.setTargetResolution(Size(960, 720))
|
|
122
125
|
.setTargetRotation(targetRotation)
|
|
123
126
|
.build()
|
|
124
127
|
Log.d(TAG, "[BIND] Preview created: $preview")
|
|
@@ -192,6 +195,7 @@ class CameraController(
|
|
|
192
195
|
*useCases.toTypedArray()
|
|
193
196
|
)
|
|
194
197
|
Log.d(TAG, "[BIND] Bound to lifecycle successfully, camera: $camera")
|
|
198
|
+
registerCameraStateObserver(camera)
|
|
195
199
|
|
|
196
200
|
// Restore torch state if it was enabled
|
|
197
201
|
if (torchEnabled) {
|
|
@@ -210,6 +214,31 @@ class CameraController(
|
|
|
210
214
|
}
|
|
211
215
|
}
|
|
212
216
|
|
|
217
|
+
private fun registerCameraStateObserver(camera: Camera?) {
|
|
218
|
+
val cam = camera ?: return
|
|
219
|
+
cameraStateLiveData?.let { liveData ->
|
|
220
|
+
cameraStateObserver?.let { liveData.removeObserver(it) }
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
val observer = Observer<CameraState> { state ->
|
|
224
|
+
val error = state.error
|
|
225
|
+
if (error != null && !hasFallbackAttempted && !isCaptureSession) {
|
|
226
|
+
hasFallbackAttempted = true
|
|
227
|
+
Log.e(TAG, "[STATE] Camera error detected (${error.code}), falling back to preview-only")
|
|
228
|
+
try {
|
|
229
|
+
cameraProvider?.unbindAll()
|
|
230
|
+
bindCameraUseCases(enableDetection = false, useImageCapture = false)
|
|
231
|
+
} catch (e: Exception) {
|
|
232
|
+
Log.e(TAG, "[STATE] Fallback bind failed", e)
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
cameraStateObserver = observer
|
|
238
|
+
cameraStateLiveData = cam.cameraInfo.cameraState
|
|
239
|
+
cam.cameraInfo.cameraState.observe(lifecycleOwner, observer)
|
|
240
|
+
}
|
|
241
|
+
|
|
213
242
|
/**
|
|
214
243
|
* Analyze frame for rectangle detection
|
|
215
244
|
*/
|
|
@@ -77,8 +77,8 @@ class DocumentScannerView(context: ThemedReactContext) : FrameLayout(context), L
|
|
|
77
77
|
previewView = PreviewView(context).apply {
|
|
78
78
|
layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
|
|
79
79
|
scaleType = PreviewView.ScaleType.FILL_CENTER
|
|
80
|
-
// Use
|
|
81
|
-
implementationMode = PreviewView.ImplementationMode.
|
|
80
|
+
// Use COMPATIBLE (TextureView) to avoid SurfaceView black frames on some devices.
|
|
81
|
+
implementationMode = PreviewView.ImplementationMode.COMPATIBLE
|
|
82
82
|
visibility = View.VISIBLE
|
|
83
83
|
keepScreenOn = true
|
|
84
84
|
// Force view to be drawn
|
package/package.json
CHANGED