gpt4 book ai didi

android - 提供的相机选择器无法为给定用例解析相机

转载 作者:行者123 更新时间:2023-12-05 08:03:32 31 4
gpt4 key购买 nike

我在 compose 中使用相机拍照。相机预览代码^

@Composable
fun CameraPreview(
modifier: Modifier = Modifier,
scaleType: PreviewView.ScaleType = PreviewView.ScaleType.FILL_CENTER,
onUseCase: (UseCase) -> Unit = { }
) {
AndroidView(
modifier = modifier,
factory = { context ->
val previewView = PreviewView(context).apply {
this.scaleType = scaleType
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
}
onUseCase(
Preview.Builder()
.build()
.also {
it.setSurfaceProvider(previewView.surfaceProvider)
}
)
previewView
}
)
}

拍照代码

@Composable
fun CameraCapture(
modifier: Modifier = Modifier,
cameraSelector: CameraSelector = CameraSelector.DEFAULT_FRONT_CAMERA,
onPhotoCreated: (Bitmap, String) -> Unit = { bitmap, string -> bitmap; string},
onClose: () -> Unit,
onFullScreen: (Boolean) -> Unit = {},
systemUiController: SystemUiController = rememberSystemUiController()
) {
var isFullScreen by remember { mutableStateOf(true) }

onFullScreen(isFullScreen)
WindowCutoutMode(isFullScreen = isFullScreen)
SystemBarColor(
systemUiController = systemUiController,
specialStatusColor = Color.Transparent
)

Box(modifier = modifier) {
val context = LocalContext.current
val lifecycleOwner = LocalLifecycleOwner.current
var previewUseCase by remember { mutableStateOf<UseCase>(Preview.Builder().build()) }

CameraPreview(
modifier = Modifier.fillMaxSize(),
onUseCase = {
previewUseCase = it
}
)

var newSelector by remember { mutableStateOf(cameraSelector) }

val cameraProviderFuture = ProcessCameraProvider.getInstance(context)
val cameraProvider = cameraProviderFuture.get()

val cameraExecutor: ExecutorService = Executors.newSingleThreadExecutor()

val imageCaptureUseCase by remember {
mutableStateOf(
ImageCapture.Builder()
.setCaptureMode(CAPTURE_MODE_MAXIMIZE_QUALITY)
.build()
)
}
val coroutineScope = rememberCoroutineScope()

Box(
modifier = Modifier
.align(Alignment.TopStart)
.padding(top = 48.dp, start = 16.dp)
.clip(CircleShape)
.background(Colors.Background.transparent)
) {
Icon(
imageVector = Icons.Rounded.Close,
contentDescription = null,
modifier = Modifier
.clickable(
role = Role.Button
) {
isFullScreen = false
onClose()
},
)
}

Row(
modifier = Modifier
.fillMaxWidth()
.background(Colors.Background.light)
.align(Alignment.BottomCenter),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
GalleryImagePicker(onPhotoCreated)

Box(
modifier = Modifier
.size(100.dp)
.padding(16.dp)
.clip(CircleShape)
.background(Color.White),
) {
Box(
modifier = Modifier
.size(80.dp)
.padding(5.dp)
.clickable(
role = Role.Button
) {
coroutineScope.launch(Dispatchers.IO) {
val imageFile = imageCaptureUseCase.takePicture(cameraExecutor)
onPhotoCreated(rotateByOrientation(imageFile), imageFile.name)
}
}
.clip(CircleShape)
.background(Color.Black),
) {

}
}

Box(
modifier = Modifier
.size(80.dp)
.padding(16.dp)
.clip(CircleShape)
.background(Color.White),
) {
Box(
modifier = Modifier
.size(60.dp)
.padding(5.dp)
.clip(CircleShape)
.background(Color.Black),
) {
Icon(
imageVector = ImageVector.vectorResource(id = R.drawable.ic_camera_change),
contentDescription = "",
tint = Color.White,
modifier = Modifier
.align(Alignment.Center)
.clickable(
role = Role.Button
) {
newSelector =
if (newSelector == CameraSelector.DEFAULT_FRONT_CAMERA)
CameraSelector.DEFAULT_BACK_CAMERA
else
CameraSelector.DEFAULT_FRONT_CAMERA
}
)
}
}
}

LaunchedEffect(newSelector) {
try {
cameraProvider.unbindAll()
cameraProvider.bindToLifecycle(
lifecycleOwner,
newSelector,
previewUseCase,
imageCaptureUseCase
)
} catch (ex: Exception) {
Log.e("CameraCapture", "Failed to bind camera use cases", ex)
}
}
}
}

该代码一直运行良好,但突然(在本周)它开始抛出异常,例如java.lang.IllegalArgumentException:提供的相机选择器无法为给定的用例解析相机,消息为无法绑定(bind)相机用例。当 View 打开时,它只有黑屏。代码没有改变,我看不到错误。

版本:

"androidx.camera:camera-core:1.0.1""
"androidx.camera:camera-camera2:1.0.1""
"androidx.camera:camera-lifecycle:1.0.1""
"androidx.camera:camera-view:1.1.0-beta02"
"androidx.camera:camera-extensions:1.1.0-beta02"

更新

日志中的更多异常:

Camera LensFacing verification failed, existing cameras: []
CameraValidator$CameraIdListIncorrectException: Expected camera missing from device.

更新 2

设备重启后异常消失,但预览是黑屏并且每秒垃圾邮件日志消息,如

D/Camera2CameraImpl:{Camera@739281d[id=1]} 转换相机内部状态:OPENED --> CLOSING

最佳答案

必须重新授予权限。

编辑:您是否确保目前没有其他应用程序正在使用相机?如果内部状态为 OPENED,则可能与使用该资源的另一个来源发生冲突。

关于android - 提供的相机选择器无法为给定用例解析相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72217146/

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