1

在此处输入图像描述我正在尝试使用新的 CameraX api 来简单地拍摄图像,并且每第二张图像(比如第一张很好,第二张不是,第三张很好,第四张不是)变成了绿色背景(请参见上图)

private void startCamera() {
    CameraX.unbindAll();

    int aspRatioW = textureView.getWidth(); 
    int aspRatioH = textureView.getHeight(); 
    Rational asp = new Rational (aspRatioW, aspRatioH); 

    PreviewConfig previewConfig=new PreviewConfig.Builder()
            .setLensFacing(lensFacing)
            .build();

    preview=new Preview(previewConfig);


    preview.setOnPreviewOutputUpdateListener(new Preview.OnPreviewOutputUpdateListener() {
        @Override
        public void onUpdated(Preview.PreviewOutput output) {
            ViewGroup viewGroup=(ViewGroup) textureView.getParent();
            viewGroup.removeView(textureView);
            viewGroup.addView(textureView, 0);

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

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

    findViewById(R.id.camera_photoButton).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            File file=new File(Environment.getExternalStorageDirectory()+"/"+"Prixpi_"+System.currentTimeMillis()
                    +".jpg");
            imgCap.takePicture(file, new ImageCapture.OnImageSavedListener() {
                @Override
                public void onImageSaved(@NonNull File file) {
                    String path=file.getAbsolutePath();
                    ScanImagePath obj=new ScanImagePath(path);
                    Log.d(TAG, "onImageSaved: path="+obj.getImagePath());
                    mRepo.insertImageTask(obj);
                    Toast.makeText(CameraActivity.this, "Image captured", Toast.LENGTH_SHORT).show();
                }

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

    CameraX.bindToLifecycle((LifecycleOwner)this, imgCap, preview);
}

不知道我做错了什么,任何帮助将不胜感激

4

0 回答 0