android - Kotlin CameraX 无法捕获图像
问题描述
我想使用 CameraX 库捕获图像并保存到文件中。我捕获了图像并保存。图像文件的大小为 0B。我不知道我哪里出错了。日志说这个错误:
androidx.camera.core.ImageCaptureException: Not bound to a valid Camera [ImageCapture:androidx.camera.core.ImageCapture-52180692-0099-40c3-8d17-508e08019b84]
这是我的捕获代码:
fun bindPreview(
lifecycleOwner: LifecycleOwner,
previewView: PreviewView,
cameraProvider: ProcessCameraProvider,
){
val preview = Preview.Builder()
.build().also {
it.setSurfaceProvider(previewView.surfaceProvider)
}
imageCapture = ImageCapture.Builder().build()
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
try {
cameraProvider.unbindAll()
cameraProvider.bindToLifecycle(
lifecycleOwner, cameraSelector, preview, imageCapture)
}catch(exception: Exception) {
Log.e(TAG, "Use case binding failed", exception)
}
}
fun onImageCaptureClicked(context: Context){
outputDirectory = getOutputDirectory(context)
val photoFile = File(outputDirectory, SimpleDateFormat(FILENAME_FORMAT, Locale.US
).format(System.currentTimeMillis()) + ".jpg")
val outputOptions = ImageCapture.OutputFileOptions.Builder(photoFile).build()
imageCapture.takePicture(
outputOptions, ContextCompat.getMainExecutor(context), object :ImageCapture.OnImageSavedCallback{
override fun onError(exception: ImageCaptureException) {
Log.e( TAG, "Photo capture failed: ${exception.message}", exception)
}
override fun onImageSaved(output: ImageCapture.OutputFileResults) {
val savedUri = Uri.fromFile(photoFile)
val msg = "Photo capture succeeded: $savedUri"
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
Log.d(TAG, msg)
}
}
)
}
我应该怎么办?
解决方案
推荐阅读
- php - Wordpress CF7 从短代码属性中获取下拉列表的所有值
- reactjs - React Native:您是否必须手动升级“project.pbxproj”?
- applescript - 如何使用 Applescript 在 BBEdit 窗口的项目窗格中选择特定文件?解决了
- java - 如何通过java在bash中传递像$(date +“%Y”)这样的命令参数?
- docker - 我应该使用哪些服务来自动构建计算密集型 docker?
- excel - VBA - xlsheetvisible = true 后闪烁
- javascript - JavaScript:有没有办法从字符串创建一个命名的 JSON 对象?
- reactjs - “任务 ':app:packageDebug' 执行失败。” 对于全新的博览会裸工作流应用程序
- redis - 优化一个redis appendonly 文件
- javascript - 如何保存添加到本地存储的连续数据?