英文:
"Not Bound To A Valid Camera" CameraX Error
问题
我试图按照谷歌的“使用 CameraX 入门”代码实验进行操作,我尝试用 Java 而不是 Kotlin 来实现,但是当我运行它并尝试拍照时,它给了我一个错误,说未绑定到有效的相机。我找不到代码中的错误所在。我检查了日志输出,并且显示可能是与表面(Surface)有关的问题,它可能无效,但我不确定如何修复。有人能帮帮我吗?我将包括我在 XML 文件中的内容以及 startCamera 和 takePhoto 函数。
<!-- XML 文件代码 -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/camera_capture_button"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="50dp"
android:scaleType="fitCenter"
android:text="Take Photo"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:elevation="2dp" />
<androidx.camera.view.PreviewView
android:id="@+id/viewFinder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
// 启动相机函数
private void startCamera() throws ExecutionException, InterruptedException {
previewView = findViewById(R.id.viewFinder);
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(MainActivity.this);
cameraProviderFuture.addListener(() -> {
try {
// 用于将相机的生命周期绑定到生命周期所有者
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
// 预览
Preview preview = new Preview.Builder()
.build();
// 默认选择后置摄像头
CameraSelector cameraSelector = new CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build();
preview.setSurfaceProvider(previewView.createSurfaceProvider());
// 在重新绑定之前解绑用例
//cameraProvider.unbindAll();
// 将用例绑定到相机
cameraProvider.bindToLifecycle(MainActivity.this, cameraSelector, preview);
} catch (Exception e) {
e.printStackTrace();
}
}, ContextCompat.getMainExecutor(MainActivity.this));
}
// 拍照函数
private void takePhoto() {
// 获取可修改的图像捕获用例的稳定引用
ImageCapture imageCapture = new ImageCapture.Builder().setTargetRotation(
this.getWindowManager().getDefaultDisplay().getRotation()).build();
// 创建带时间戳的输出文件来保存图像
File photoFile;
photoFile = new File(outputDirectory, FILENAME_FORMAT + ".jpg");
// 创建包含文件和元数据的输出选项对象
ImageCapture.OutputFileOptions outputOptions = new ImageCapture.OutputFileOptions.Builder(photoFile).build();
imageCapture.takePicture(outputOptions, ContextCompat.getMainExecutor(this), new ImageCapture.OnImageSavedCallback () {
@Override
public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
Toast.makeText(MainActivity.this, "照片拍摄成功:" + outputFileResults, Toast.LENGTH_SHORT).show();
}
@Override
public void onError(@NonNull ImageCaptureException error) {
Toast.makeText(MainActivity.this, "照片拍摄失败:" + error, Toast.LENGTH_SHORT).show();
}
});
}
英文:
I was trying to follow the "Getting Started with CameraX" codelab from google and I tried to do it in Java instead of Kotlin but when I ran it and tried to take a picture it gave me an error that says not bound to a valid camera. I can't find where the error is in the code. I checked the logcat and it says that there could be a problem with the surface that it might not be valid but I am not sure how to fix that can someone help me please. I will include what I have in my XML file and the startCamera and takePhoto functions.
//xml file code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/camera_capture_button"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginBottom="50dp"
android:scaleType="fitCenter"
android:text="Take Photo"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:elevation="2dp" />
<androidx.camera.view.PreviewView
android:id="@+id/viewFinder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
//start camera function
private void startCamera() throws ExecutionException, InterruptedException {
previewView = findViewById(R.id.viewFinder);
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(MainActivity.this);
cameraProviderFuture.addListener(() -> {
try {
// Used to bind the lifecycle of cameras to the lifecycle owner
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
// Preview
Preview preview = new Preview.Builder()
.build();
// Select back camera as a default
//CameraSelector cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA;
CameraSelector cameraSelector = new CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build();
preview.setSurfaceProvider(previewView.createSurfaceProvider());
// Unbind use cases before rebinding
//cameraProvider.unbindAll();
// Bind use cases to camera
cameraProvider.bindToLifecycle(MainActivity.this, cameraSelector, preview);
} catch (Exception e) {
e.printStackTrace();
}
}, ContextCompat.getMainExecutor(MainActivity.this));
}
//take photo function
private void takePhoto() {
// Get a stable reference of the modifiable image capture use case
ImageCapture imageCapture = new ImageCapture.Builder().setTargetRotation(
this.getWindowManager().getDefaultDisplay().getRotation()).build();
// Create time-stamped output file to hold the image
File photoFile;
photoFile = new File(outputDirectory, FILENAME_FORMAT + ".jpg");
// Create output options object which contains file + metadata
ImageCapture.OutputFileOptions outputOptions = new ImageCapture.OutputFileOptions.Builder(photoFile).build();
imageCapture.takePicture(outputOptions, ContextCompat.getMainExecutor(this), new ImageCapture.OnImageSavedCallback () {
@Override
public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
Toast.makeText(MainActivity.this, "Photo Capture Succeeded: "+ outputFileResults, Toast.LENGTH_SHORT).show();
}
@Override
public void onError(@NonNull ImageCaptureException error) {
Toast.makeText(MainActivity.this, "Photo capture failed: "+ error, Toast.LENGTH_SHORT).show();
}
});
}
答案1
得分: 29
ImageCapture
用例并不绑定到相机,正如错误消息所提到的那样。它应该与相机绑定,就像您绑定Preview
用例一样,使用ProcessCameraProvider#bindToLifecycle()
。
因此在您的代码中,创建并配置这两个用例,然后按如下方式绑定它们:
cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, preview, imageCapture);
英文:
The ImageCapture
use case isn't bound to a camera, as the error message mentions. It should be bound to a camera the same way you bind the Preview
use case, with ProcessCameraProvider#bindToLifecycle()
.
So in your code, create and configure both use cases, then bind them as follows:
cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, preview, imageCapture);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论