【问题标题】:"Not Bound To A Valid Camera" CameraX Error“未绑定到有效的相机”CameraX 错误
【发布时间】:2020-09-19 09:56:20
【问题描述】:

我试图遵循 google 的“Getting Started with CameraX”代码实验室,我尝试用 Java 而不是 Kotlin 来做,但是当我运行它并尝试拍照时,它给了我一个错误,说不受约束有效的相机。我在代码中找不到错误的位置。我检查了 logcat,它说表面可能存在问题,它可能无效,但我不知道如何解决这个问题,有人可以帮助我。我将在我的 XML 文件以及 startCamera 和 takePhoto 函数中包含我的内容。

//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();
        }
    });

}

【问题讨论】:

    标签: java android android-camerax


    【解决方案1】:

    正如错误消息所述,ImageCapture 用例未绑定到相机。它应该以与绑定Preview 用例相同的方式绑定到相机,使用ProcessCameraProvider#bindToLifecycle()

    因此,在您的代码中,创建并配置两个用例,然后将它们绑定如下:

    cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, preview, imageCapture);
    

    【讨论】:

    • 谢谢,这确实帮助我解决了这个问题:) 但我认为我在代码中做错了什么,因为它不想保存图像并且给了我新的错误。我为此创建了一个新问题,其中包括它给我的错误。
    猜你喜欢
    • 1970-01-01
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多