gpt4 book ai didi

android - 如何将 CameraView 与 Jetpack Compose 一起使用?

转载 作者:行者123 更新时间:2023-12-02 12:00:41 24 4
gpt4 key购买 nike

目前 Compose 中没有相当于 CameraView(和 PreviewView)的功能。是否可以将其包装并以组合布局显示?

最佳答案

仍然没有可组合的 CameraX。您需要使用 AndroidView创建一个。
Compose 1.0.0-beta02 的更新示例:

@Composable
fun CameraPreview(
modifier: Modifier = Modifier,
cameraSelector: CameraSelector = CameraSelector.DEFAULT_BACK_CAMERA,
scaleType: PreviewView.ScaleType = PreviewView.ScaleType.FILL_CENTER,
) {
val lifecycleOwner = LocalLifecycleOwner.current
AndroidView(
modifier = modifier,
factory = { context ->
val previewView = PreviewView(context).apply {
this.scaleType = scaleType
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
// Preview is incorrectly scaled in Compose on some devices without this
implementationMode = PreviewView.ImplementationMode.COMPATIBLE
}

val cameraProviderFuture = ProcessCameraProvider.getInstance(context)

cameraProviderFuture.addListener({
val cameraProvider = cameraProviderFuture.get()

// Preview
val preview = Preview.Builder()
.build()
.also {
it.setSurfaceProvider(previewView.surfaceProvider)
}

try {
// Must unbind the use-cases before rebinding them.
cameraProvider.unbindAll()

cameraProvider.bindToLifecycle(
lifecycleOwner, cameraSelector, preview
)
} catch (exc: Exception) {
Log.e(TAG, "Use case binding failed", exc)
}
}, ContextCompat.getMainExecutor(context))

previewView
})
}

关于android - 如何将 CameraView 与 Jetpack Compose 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61795508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com