首页 > 解决方案 > java.lang.illegalArgumentException:找不到相机设备支持的表面组合

问题描述

安卓相机 X

java.lang.illegalArgumentException: No Supported surface combination is found for camera device - id: 1 可能试图绑定太多案例。

现有表面:[] 新配置:[androidx.camera.core.impl.ImageAnalysisConfig@37a677,androidx.camera.core.Impl.PreviewConfig@174f711,androidx.camera.core.impl.ImageCaptureConfig@e20f34d]

执行以下行时出现异常。

Camera camera = cameraProvider.bindToLifecycle((LifecycleOwner)this, cameraSelector, preview, 
imageCapture, imageAnalysis);

Used Dependencies
def camerax_version = "1.0.0-beta05"
implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
implementation "androidx.camera:camera-view:1.0.0-alpha12"
implementation "androidx.camera:camera-extensions:1.0.0-alpha12"

专门针对 Samaung A71 Android 10 设备和目前其他运行良好的设备

注意:我无法调试,因为这是在最终用户设备中出现的。

标签: androidandroid-cameraandroid-camera2android-camerax

解决方案


你可以使用我在 Github 上分享的代码。

在这里,我将 CameraX 用于相机和多次点击图像将显示在相机屏幕中

完整源代码/项目: https ://github.com/abhishekhzb/quick_camera

筛选:

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

XML 代码:

 <TextureView
        android:id="@+id/view_finder"
        android:layout_marginTop="90dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout"
        app:layout_constraintVertical_bias="0.0" />

Java 代码:

 private void startCamera() {

        CameraX.unbindAll();

        Rational aspectRatio = new Rational(textureView.getWidth(), textureView.getHeight());
        Size screen = new Size(textureView.getWidth(), textureView.getHeight()); //size of the screen


        PreviewConfig pConfig = new PreviewConfig.Builder().setTargetAspectRatio(aspectRatio).setTargetResolution(screen).build();
        Preview preview = new Preview(pConfig);

        preview.setOnPreviewOutputUpdateListener(
                new Preview.OnPreviewOutputUpdateListener() {
                    //to update the surface texture we  have to destroy it first then re-add it
                    @Override
                    public void onUpdated(Preview.PreviewOutput output) {
                        ViewGroup parent = (ViewGroup) textureView.getParent();
                        parent.removeView(textureView);
                        parent.addView(textureView, 0);

                        textureView.setSurfaceTexture(output.getSurfaceTexture());
                        updateTransform();
                    }
                });


        ImageCaptureConfig imageCaptureConfig = new ImageCaptureConfig.Builder().setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY)
                .setTargetRotation(getWindowManager().getDefaultDisplay().getRotation()).build();
        imgCap = new ImageCapture(imageCaptureConfig);

        findViewById(R.id.imgCapture).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String root = Environment.getExternalStorageDirectory().toString();
                myDir = new File(root + "/QuickCamera/" + folderName);
                myDir.mkdirs();
                count++;

                counter.setText(String.valueOf(count));

                fname = "" + folderName + "-" + count + ".jpg";

                File file = new File(myDir, fname);
                if (file.exists()) file.delete();


                // File file = new File(Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis() + ".png");
                imgCap.takePicture(file, new ImageCapture.OnImageSavedListener() {
                    @Override
                    public void onImageSaved(@NonNull File file) {
                        String msg = "Pic captured at " + file.getAbsolutePath();
                        Log.e("PATH", "" + msg);

                        list.add(new ImageModel(count, file.getAbsolutePath(), fname));

                        mAdapter = new ImageRecyclerAdapter(CameraActivity.this, list);
                        recyclerView.setAdapter(mAdapter);
                        mAdapter.notifyDataSetChanged();

                    }

                    @Override
                    public void onError(@NonNull ImageCapture.UseCaseError useCaseError, @NonNull String message, @Nullable Throwable cause) {
                        String msg = "Pic capture failed : " + message;
                        Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
                        if (cause != null) {
                            cause.printStackTrace();
                        }
                    }
                });
            }
        });

        //bind to lifecycle:
        CameraX.bindToLifecycle((LifecycleOwner) this, preview, imgCap);
    }


    private void updateTransform() {
        Matrix mx = new Matrix();
        float w = textureView.getMeasuredWidth();
        float h = textureView.getMeasuredHeight();

        float cX = w / 2f;
        float cY = h / 2f;

        int rotationDgr;
        int rotation = (int) textureView.getRotation();

        switch (rotation) {
            case Surface.ROTATION_0:
                rotationDgr = 0;
                break;
            case Surface.ROTATION_90:
                rotationDgr = 90;
                break;
            case Surface.ROTATION_180:
                rotationDgr = 180;
                break;
            case Surface.ROTATION_270:
                rotationDgr = 270;
                break;
            default:
                return;
        }

        mx.postRotate((float) rotationDgr, cX, cY);
        textureView.setTransform(mx);
    }

推荐阅读