简体   繁体   English

“未绑定到有效相机” CameraX 错误

[英]"Not Bound To A Valid Camera" CameraX Error

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.我试图按照谷歌的“CameraX 入门”代码实验室进行操作,我尝试用 Java 而不是 Kotlin 来完成,但是当我运行它并尝试拍照时,它给了我一个错误,提示未绑定到有效相机. 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.我检查了 logcat,它说表面可能存在问题,它可能无效,但我不知道如何解决这个问题,有人可以帮助我。 I will include what I have in my XML file and the startCamera and takePhoto functions.我将在我的 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();
        }
    });

}

The ImageCapture use case isn't bound to a camera, as the error message mentions.如错误消息所述, ImageCapture用例未绑定到相机。 It should be bound to a camera the same way you bind the Preview use case, with ProcessCameraProvider#bindToLifecycle() .它应该以与绑定Preview用例相同的方式绑定到相机,使用ProcessCameraProvider#bindToLifecycle()

So in your code, create and configure both use cases, then bind them as follows:因此,在您的代码中,创建和配置两个用例,然后按如下方式绑定它们:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 针对 Android 11 上的 imageCapture 用例,camerax“未绑定到有效相机” - camerax "Not bound to a valid Camera" for imageCapture use case on Android 11 绑定不匹配错误:该类型不是有效替代 - Bound Mismatch Error: The type is not a valid substitute 如何使用 CameraX 构建我的自定义相机? - How to use CameraX to build my custom Camera? CameraX 和 OpenGL 在实时相机上应用效果 - CameraX and OpenGL to apply effects on real time Camera 如何在 camerax 相机提供商上替换用例 - How to replace use case on camerax camera provider CameraX camera-core:1.0.0-beta01 在 Camera2Config.java defaultConfig() 崩溃 - CameraX camera-core:1.0.0-beta01 crashed at Camera2Config.java defaultConfig() 如何创建泛型子类的实例? 获取错误:“绑定不匹配:类型...不是有界参数的有效替代...” - How do I make an instance of generic subclass? Getting error: “Bound mismatch: The type … is not a valid substitute for the bounded parameter …” 如何检查android设备是否支持用于camera2或cameraX视频稳定的OIS或EIS - How to check whether android device supports OIS or EIS for camera2 or cameraX video stabilization Android Camera App 使用 CameraX 以 YUV_420_888 格式保存图像 - Android Camera App using CameraX to save images in YUV_420_888 format 绑定不匹配:该类型不是绑定参数的有效替代 - Bound mismatch: The type is not a valid substitute for the bounded parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM