简体   繁体   中英

How to Freeze/Lock android Camera X preview with Flash Light updates on taking picture?

I am implementing Camera X. The issue i am facing is to implement a mechanism to lock/freeze camera preview when picture is captured. Currently i have implement a workaround but it doesn't work well if the flash light is on while capturing. I get a frame from previewView (PreviewView) previewView.getBitmap() as before capturing the image and then display in an captureImage (ImageView). But the the freeze frame not show flash light update. My current code is below


    private void capturePhoto() {

        showProgress(true);

        // Get the Information to be used & stored with Image
        ContentValues contentValues = getImageSaveInfo();

        Uri externalUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

        ImageCapture.OutputFileOptions options = new ImageCapture.OutputFileOptions
                .Builder(getContentResolver(), externalUri, contentValues)
                .build();


        // Play the Capture Sound when a picture is captured.
        playCameraShutterSound();

        // Display current frame From Preview in ImageView.
        freezePreview(true);

        imageCapture.takePicture(options,
                ContextCompat.getMainExecutor(this),
                new ImageCapture.OnImageSavedCallback() {

                    @Override
                    public void onImageSaved(@NonNull ImageCapture.OutputFileResults results) {

                        ToastUtility.successToast(getApplicationContext(),
                                "Photo Capture Successfully");

                        // Update Last Taken Image View with new Image

                        getLastTakenImage();

                        if (results.getSavedUri() != null) {

                            Log.d(TAG, "Image Saved At -> " + results.getSavedUri().toString());

                        }

                        showProgress(false);
                        freezePreview(false);

                    }

                    @Override
                    public void onError(@NonNull ImageCaptureException exception) {

                        ToastUtility.errorToast(getApplicationContext(),
                                "Photo Couldn't Capture");

                        Log.d(TAG, "Image Capture Error -> " + exception.getMessage());

                        showProgress(false);
                        freezePreview(false);

                    }
                });

    }


    private void freezePreview(boolean value) {

        if (value) {

            Bitmap bitmap = mainBinding.previewView.getBitmap();

            Glide.with(getApplicationContext())
                    .load(bitmap).into(mainBinding.captureImage);

            mainBinding.captureImage.setVisibility(View.VISIBLE);

            mainBinding.previewView.setVisibility(View.INVISIBLE);

        } else {

            mainBinding.previewView.setVisibility(View.VISIBLE);

            mainBinding.captureImage.setVisibility(View.INVISIBLE);

        }

    }

The flash is triggered at some point after takePicture() is called, there isn't a callback for it in CameraX, so there isn't a direct way to know when it's fired.

You can instead use camera2 interop to indirectly check for the flash state . You can add a session CaptureCallback to ImageCapture's config, then inside the callback's onCaptureCompleted , check if the flash state of the total result is FIRED .

// Override onCaptureCompleted to check for the flash state
CameraCaptureSession.CaptureCallback sessionCaptureCallback = //... ;

// Initialize an ImageCapture builder
ImageCapture.Builder configBuilder = new ImageCapture.Builder();

// Add the session CaptureCallback to it
new Camera2Interop.Extender<>(configBuilder)
    .setSessionCaptureCallback(sessionCaptureCallback);

// Build the ImageCapture use case
ImageCapture useCase = configBuilder.build();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM