首页 > 解决方案 > 为什么CameraX预览在真机上模糊不清,怎么对焦?

问题描述

现在,我使用 CameraX 制作了自己的相机。预览工作正常,图像正在保存。然而,预览是模糊的,几乎没有任何关于如何自动对焦甚至手动对焦的文档。因为我使用 Firebase 的机器学习工具包来识别文本,所以我的应用程序非常清晰非常重要。

   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).setTargetRotation(Surface.ROTATION_0).build();
        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) {

                cameraImage.setImageResource(R.drawable.cameraon);

                CameraX.unbind(preview);

                loadingDialog.startLoadingDialog();

                file = new File(Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis() + ".png");
                imgCap.takePicture(file, new ImageCapture.OnImageSavedListener() {
                    @Override
                    public void onImageSaved(@NonNull File file) {

                        String filePath = file.getPath();

                        Bitmap bitmap = BitmapFactory.decodeFile(filePath);

                        rotateImage(bitmap);

                    }

                    @Override
                    public void onError(@NonNull ImageCapture.ImageCaptureError imageCaptureError, @NonNull String message, @Nullable Throwable cause) {
                        String msg = "Image 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);
    }

标签: androidandroid-cameraandroid-camera2android-camerax

解决方案


如果您可以分享一些代码示例,我认为回答这个问题会更有帮助。总的来说,这个CameraControl类(链接到源代码)有两个相关的 API

ListenableFuture<FocusMeteringResult> startFocusAndMetering(FocusMeteringAction action);

ListenableFuture<Void> cancelFocusAndMetering();

我相信您可以使用startFocusMetering相关参数来触发中心焦点或面部焦点(如果您有面部坐标)。现在,如果您在每次拍摄之前调用此函数或在循环中运行此函数以每 500 毫秒调用一次,它可能会解决您的问题。

Github查找后的一些代码参考:

示例 1

示例 2

示例 3


推荐阅读