如何在合成中初始化摄像机

How to initialize camera in compose(如何在合成中初始化摄像机)
本文介绍了如何在合成中初始化摄像机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过合成androidView获得相机视图,但以下代码似乎不适合我。

@Composable
fun CameraPreviewScreen() {
    val lifecycleOwner = LifecycleOwnerAmbient.current
    val context = ContextAmbient.current
    val cameraProviderFuture = remember { ProcessCameraProvider.getInstance(context) }
    val previewView = remember { PreviewView(context) }
    AndroidView(viewBlock = { previewView }) {
        cameraProviderFuture.addListener(Runnable {
            val cameraProvider = cameraProviderFuture.get()
            val preview = Preview.Builder().build()
            preview.setSurfaceProvider(it.surfaceProvider)
            val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
            try {
                cameraProvider.unbindAll()
                cameraProvider.bindToLifecycle(
                    lifecycleOwner, cameraSelector, preview)
            } catch(exc: Exception) {
                Log.e(TAG, "Use case binding failed", exc)
            }
        }, ContextCompat.getMainExecutor(context))
    }
}
下面的快照。

报告错误。

拒绝权限:无法使用摄像头
没有相机权限(代码1),无法打开相机&0&q;

推荐答案

由于您的问题似乎与摄像机权限有关,您可以使用Permissions APIs来自Accompanist库向用户请求权限。

文档显示了一个使用摄像头的示例

@Composable
private fun FeatureThatRequiresCameraPermission(
    navigateToSettingsScreen: () -> Unit
) {
    // Track if the user doesn't want to see the rationale any more.
    var doNotShowRationale by rememberSaveable { mutableStateOf(false) }

    val cameraPermissionState = rememberPermissionState(android.Manifest.permission.CAMERA)
    PermissionRequired(
        permissionState = cameraPermissionState,
        permissionNotGrantedContent = {
            if (doNotShowRationale) {
                Text("Feature not available")
            } else {
                Column {
                    Text("The camera is important for this app. Please grant the permission.")
                    Spacer(modifier = Modifier.height(8.dp))
                    Row {
                        Button(onClick = { cameraPermissionState.launchPermissionRequest() }) {
                            Text("Ok!")
                        }
                        Spacer(Modifier.width(8.dp))
                        Button(onClick = { doNotShowRationale = true }) {
                            Text("Nope")
                        }
                    }
                }
            }
        },
        permissionNotAvailableContent = {
            Column {
                Text(
                    "Camera permission denied. See this FAQ with information about why we " +
                        "need this permission. Please, grant us access on the Settings screen."
                )
                Spacer(modifier = Modifier.height(8.dp))
                Button(onClick = navigateToSettingsScreen) {
                    Text("Open Settings")
                }
            }
        }
    ) {
        Text("Camera permission Granted")
    }
}

这篇关于如何在合成中初始化摄像机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

The Google Mobile Ads SDK was initialized without AppMeasurement(Google 移动广告 SDK 在没有 AppMeasurement 的情况下进行了初始化)
No JNI_OnLoad found in ... skipping init(在 ... 中未找到 JNI_OnLoad 正在跳过初始化)
No JNI_OnLoad found skipping init gt; Application shutdown(没有找到跳过初始化的 JNI_OnLoad 应用程序关闭)
Fatal error: use of unimplemented initializer #39;init(coder:)#39; for class(致命错误:对类使用未实现的初始化程序“init(coder:))
Error in Swift class: Property not initialized at super.init call(Swift 类中的错误:在 super.init 调用时未初始化属性)
Conditional Binding: if let error – Initializer for conditional binding must have Optional type(条件绑定:if let error - 条件绑定的初始化程序必须具有可选类型)