Move usage of LocalInspectionMode.current into the deeper block.
This commit is contained in:
parent
6100d4944f
commit
06c20e3abf
1 changed files with 67 additions and 67 deletions
|
|
@ -48,78 +48,78 @@ fun QrCodeCameraView(
|
||||||
isScanning: Boolean,
|
isScanning: Boolean,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
) {
|
) {
|
||||||
if (LocalInspectionMode.current) {
|
val coroutineScope = rememberCoroutineScope()
|
||||||
Box(
|
val localContext = LocalContext.current
|
||||||
modifier = modifier
|
val lifecycleOwner = LocalLifecycleOwner.current
|
||||||
.background(color = ElementTheme.colors.bgSubtlePrimary),
|
var cameraProvider by remember { mutableStateOf<ProcessCameraProvider?>(null) }
|
||||||
contentAlignment = Alignment.Center,
|
val previewUseCase = remember { Preview.Builder().build() }
|
||||||
) {
|
var lastFrame by remember { mutableStateOf<Bitmap?>(null) }
|
||||||
Text("CameraView")
|
val imageAnalysis = remember {
|
||||||
}
|
ImageAnalysis.Builder()
|
||||||
} else {
|
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
|
||||||
val coroutineScope = rememberCoroutineScope()
|
.build()
|
||||||
val localContext = LocalContext.current
|
}
|
||||||
val lifecycleOwner = LocalLifecycleOwner.current
|
|
||||||
var cameraProvider by remember { mutableStateOf<ProcessCameraProvider?>(null) }
|
|
||||||
val previewUseCase = remember { Preview.Builder().build() }
|
|
||||||
var lastFrame by remember { mutableStateOf<Bitmap?>(null) }
|
|
||||||
val imageAnalysis = remember {
|
|
||||||
ImageAnalysis.Builder()
|
|
||||||
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
|
|
||||||
.build()
|
|
||||||
}
|
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
LaunchedEffect(Unit) {
|
||||||
cameraProvider = localContext.getCameraProvider()
|
cameraProvider = localContext.getCameraProvider()
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun startQRCodeAnalysis(cameraProvider: ProcessCameraProvider, attempt: Int = 1) {
|
suspend fun startQRCodeAnalysis(cameraProvider: ProcessCameraProvider, attempt: Int = 1) {
|
||||||
lastFrame = null
|
lastFrame = null
|
||||||
val cameraSelector = CameraSelector.Builder()
|
val cameraSelector = CameraSelector.Builder()
|
||||||
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
|
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
|
||||||
.build()
|
.build()
|
||||||
imageAnalysis.setAnalyzer(
|
imageAnalysis.setAnalyzer(
|
||||||
ContextCompat.getMainExecutor(localContext),
|
ContextCompat.getMainExecutor(localContext),
|
||||||
QRCodeAnalyzer(onScanQrCode)
|
QRCodeAnalyzer(onScanQrCode)
|
||||||
|
)
|
||||||
|
try {
|
||||||
|
// Make sure we unbind all use cases before binding them again
|
||||||
|
cameraProvider.unbindAll()
|
||||||
|
|
||||||
|
cameraProvider.bindToLifecycle(
|
||||||
|
lifecycleOwner,
|
||||||
|
cameraSelector,
|
||||||
|
previewUseCase,
|
||||||
|
imageAnalysis,
|
||||||
)
|
)
|
||||||
try {
|
lastFrame = null
|
||||||
// Make sure we unbind all use cases before binding them again
|
} catch (e: Exception) {
|
||||||
cameraProvider.unbindAll()
|
val maxAttempts = 3
|
||||||
|
if (attempt > maxAttempts) {
|
||||||
cameraProvider.bindToLifecycle(
|
Timber.e(e, "Use case binding failed after $maxAttempts attempts. Giving up.")
|
||||||
lifecycleOwner,
|
} else {
|
||||||
cameraSelector,
|
Timber.e(e, "Use case binding failed (attempt #$attempt). Retrying after a delay...")
|
||||||
previewUseCase,
|
delay(100)
|
||||||
imageAnalysis
|
startQRCodeAnalysis(cameraProvider, attempt + 1)
|
||||||
)
|
|
||||||
lastFrame = null
|
|
||||||
} catch (e: Exception) {
|
|
||||||
val maxAttempts = 3
|
|
||||||
if (attempt > maxAttempts) {
|
|
||||||
Timber.e(e, "Use case binding failed after $maxAttempts attempts. Giving up.")
|
|
||||||
} else {
|
|
||||||
Timber.e(e, "Use case binding failed (attempt #$attempt). Retrying after a delay...")
|
|
||||||
delay(100)
|
|
||||||
startQRCodeAnalysis(cameraProvider, attempt + 1)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun stopQRCodeAnalysis(previewView: PreviewView) {
|
fun stopQRCodeAnalysis(previewView: PreviewView) {
|
||||||
// Stop analyzer
|
// Stop analyzer
|
||||||
imageAnalysis.clearAnalyzer()
|
imageAnalysis.clearAnalyzer()
|
||||||
|
|
||||||
// Save last frame to display it as the 'frozen' preview
|
// Save last frame to display it as the 'frozen' preview
|
||||||
if (lastFrame == null) {
|
if (lastFrame == null) {
|
||||||
lastFrame = previewView.bitmap
|
lastFrame = previewView.bitmap
|
||||||
Timber.d("Saving last frame for frozen preview.")
|
Timber.d("Saving last frame for frozen preview.")
|
||||||
}
|
|
||||||
|
|
||||||
// Unbind preview use case
|
|
||||||
cameraProvider?.unbindAll()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Box(modifier.clipToBounds()) {
|
// Unbind preview use case
|
||||||
|
cameraProvider?.unbindAll()
|
||||||
|
}
|
||||||
|
|
||||||
|
Box(modifier.clipToBounds()) {
|
||||||
|
if (LocalInspectionMode.current) {
|
||||||
|
Box(
|
||||||
|
modifier = modifier
|
||||||
|
.background(color = ElementTheme.colors.bgSubtlePrimary),
|
||||||
|
contentAlignment = Alignment.Center,
|
||||||
|
) {
|
||||||
|
Text("CameraView")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
AndroidView(
|
AndroidView(
|
||||||
factory = { context ->
|
factory = { context ->
|
||||||
val previewView = PreviewView(context)
|
val previewView = PreviewView(context)
|
||||||
|
|
@ -145,9 +145,9 @@ fun QrCodeCameraView(
|
||||||
cameraProvider = null
|
cameraProvider = null
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
lastFrame?.let {
|
}
|
||||||
Image(bitmap = it.asImageBitmap(), contentDescription = null)
|
lastFrame?.let {
|
||||||
}
|
Image(bitmap = it.asImageBitmap(), contentDescription = null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -159,7 +159,7 @@ private suspend fun Context.getCameraProvider(): ProcessCameraProvider =
|
||||||
{
|
{
|
||||||
continuation.resume(cameraProvider.get())
|
continuation.resume(cameraProvider.get())
|
||||||
},
|
},
|
||||||
ContextCompat.getMainExecutor(this)
|
ContextCompat.getMainExecutor(this),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue