Skip to content

Instantly share code, notes, and snippets.

@Gnzlt
Last active August 21, 2023 22:18
Show Gist options
  • Save Gnzlt/a2b6cc4fdbe99724e7e69a34534f59fd to your computer and use it in GitHub Desktop.
Save Gnzlt/a2b6cc4fdbe99724e7e69a34534f59fd to your computer and use it in GitHub Desktop.
Android Jetpack Compose Camera Preview Composable
@Composable
fun CameraPreview(
modifier: Modifier = Modifier,
scaleType: PreviewView.ScaleType = PreviewView.ScaleType.FILL_CENTER,
cameraSelector: CameraSelector = CameraSelector.DEFAULT_FRONT_CAMERA
) {
val lifecycleOwner = LocalLifecycleOwner.current
AndroidView(
modifier = modifier,
factory = { context ->
PreviewView(context).apply {
this.scaleType = scaleType
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
controller = LifecycleCameraController(context).apply {
this.bindToLifecycle(lifecycleOwner)
this.cameraSelector = cameraSelector
}
}
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment