Browse Source

ImageViewer, Android camera (#3487)

* The camera should stop working after leaving CameraView on Android.
* Update examples/imageviewer/shared/src/androidMain/kotlin/example/imageviewer/view/CameraView.android.kt
pull/3567/head
Rob X 1 year ago committed by GitHub
parent
commit
494c686293
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      examples/imageviewer/shared/src/androidMain/kotlin/example/imageviewer/view/CameraView.android.kt

13
examples/imageviewer/shared/src/androidMain/kotlin/example/imageviewer/view/CameraView.android.kt

@ -79,6 +79,7 @@ private fun CameraWithGrantedPermission(
val context = LocalContext.current
val lifecycleOwner = LocalLifecycleOwner.current
val viewScope = rememberCoroutineScope()
var cameraProvider: ProcessCameraProvider? by remember { mutableStateOf(null) }
val preview = Preview.Builder().build()
val previewView = remember { PreviewView(context) }
@ -96,16 +97,22 @@ private fun CameraWithGrantedPermission(
.build()
}
DisposableEffect(Unit) {
onDispose {
cameraProvider?.unbindAll()
}
}
LaunchedEffect(isFrontCamera) {
val cameraProvider = suspendCoroutine<ProcessCameraProvider> { continuation ->
cameraProvider = suspendCoroutine<ProcessCameraProvider> { continuation ->
ProcessCameraProvider.getInstance(context).also { cameraProvider ->
cameraProvider.addListener({
continuation.resume(cameraProvider.get())
}, executor)
}
}
cameraProvider.unbindAll()
cameraProvider.bindToLifecycle(
cameraProvider?.unbindAll()
cameraProvider?.bindToLifecycle(
lifecycleOwner,
cameraSelector,
preview,

Loading…
Cancel
Save