问题
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:
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);
来源:https://stackoverflow.com/questions/63960826/not-bound-to-a-valid-camera-camerax-error