Android Camera App using CameraX to save images in YUV_420_888 format

Viewed 4836

I want to save image stream (preferably 30fps) to the device. I came across this post.

How do I dump images in YUV_420_888 format using cameraX without encoding it in JPEG?

Also, I'll be glad if any one can suggest any direction in saving burst of images.

Till now I'm only saving an image by onclick of button

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

        File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES),
                "Image_" + System.currentTimeMillis() + ".jpg");

        imageCapture.takePicture(file, new ImageCapture.OnImageSavedListener() {
            @Override
            public void onImageSaved(@NonNull File file) {
                Toast.makeText(CameraCaptureActivity.this, "Photo saved as " + file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(@NonNull ImageCapture.ImageCaptureError imageCaptureError, @NonNull String message, @Nullable Throwable cause) {
                Toast.makeText(CameraCaptureActivity.this, "Couldn't save photo: " + message, Toast.LENGTH_SHORT).show();
                if (cause != null)
                    cause.printStackTrace();
            }
        });
    }
});
1 Answers

How do I dump images in YUV_420_888 format using cameraX without encoding it as JPEG?

You can use another version of ImageCapture.takePicture() that returns an im-memory image in YUV_420_888 format, ImageCapture.takePicture(Executor, OnImageCaptureCallback).

imageCapture.takePicture(executor, new ImageCapture.OnImageCapturedCallback() {

   @Override
   public void onCaptureSuccess (ImageProxy imageProxy) {
      // Use the image, then make sure to close it before returning from the method
      imageProxy.close()
   }

   @Override
   public void onError (ImageCaptureException exception) {
      // Handle the exception however you'd like
   }
})

I'll be glad I could any direction in saving burst of images.

Burst images are images taken one after another at a certain rate. How you implement it in your app depends on what you want the user experience to look like, it can be triggered from a single button click or a long click. The main thing is you'll have to make a number of calls to ImageCapture.takePicture(), you can control the image capture rate using APIs such as Handler.postDelayed(Runnable, Long).

Related