3

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();
            }
        });
    }
});
karatuno
  • 365
  • 5
  • 18
  • Please elaborate on "save image stream" and "burst of images"(How large is the number of images?)., And what problem you are having in current approach? (Can't dump in YUV_420_888 format? or only one image is saved?) – samabcde Jun 15 '20 at 01:26
  • @samabcde With 'saving image stream' I mean mainly to save images at 30fps on the disk. Currently, I'm only saving an image at a time to the disk, – karatuno Jun 15 '20 at 23:01
  • Could you find any solution? – momvart May 27 '22 at 19:41

1 Answers1

1

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).

Husayn Hakeem
  • 4,184
  • 1
  • 16
  • 31
  • In onCaptureSuccess method How do I save the image to the disk? Is this https://stackoverflow.com/questions/31984622/how-to-save-a-yuv-420-888-image way right to do that? – karatuno Jun 15 '20 at 22:52
  • 2
    The imageProxy object here is a JPEG and not YUV_420_888 (as with the ImageAnalysis) use case. I'm running an inline debugger inside the onCaptureSuccess and 'image.format' returns '256' rather than '35'. The reason I found this was that I can no longer use the [YuvToRgbConverter=(https://github.com/android/camera-samples/blob/3730442b49189f76a1083a98f3acf3f5f09222a3/CameraUtils/lib/src/main/java/com/example/android/camera/utils/YuvToRgbConverter.kt) as it asserts the YUV_420_888 in the `imageToByteBuffer()` method – topher217 Apr 02 '21 at 07:07
  • 1
    I'm not seeing any builder functionality to set the image format either. – topher217 Apr 02 '21 at 07:08