Android Camera2 API Raw Capture

This android tutorial series is about explaining how to do the android camera2 api raw capture. Where we show how to capture the raw image to a file.

This tutorial is a follow on from this one where the setup for the application is done.

Get Code

The code is now on github you can get it from here

https://github.com/mobapptuts/recyclerview_image_gallery.git Tag camera2-raw-capture

or else run this command

git clone –branch camera2-raw-capture https://github.com/mobapptuts/recyclerview_image_gallery.git

Code Samples

Capture setup in the captureStillIMage method

Add the raw image reader surface to the capture request

captureStillBuilder.addTarget(mRawImageReader.getSurface());

In the camera captured session onCaptureStarted callback create the file for the raw image

CameraCaptureSession.CaptureCallback captureCallback =
                    new CameraCaptureSession.CaptureCallback() {
                        @Override
                        public void onCaptureStarted(CameraCaptureSession session, CaptureRequest request, long timestamp, long frameNumber) {
                            super.onCaptureStarted(session, request, timestamp, frameNumber);

                            try {
                                if(mRequestingAppUri != null) {
                                    mImageFile = new File(mRequestingAppUri.getPath());
                                } else {
                                    mImageFile = createImageFile();
                                    mRawImageFile = createRawImageFile();
                                }


                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

Create an activity member for CaptureResult

private CaptureResult mCaptureResult;

In the camera captured session onCaptureCompleted callback assign the TotalCapturedResult to the   Activity’s mCaptureResult member

CameraCaptureSession.CaptureCallback captureCallback =
                    new CameraCaptureSession.CaptureCallback() {

 @Override
                        public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
                            super.onCaptureCompleted(session, request, result);

                            /*
                            Toast.makeText(getApplicationContext(),
                                    "Image Captured!", Toast.LENGTH_SHORT).show();
                            */
                            mCaptureResult = result;
                            unLockFocus();
                        }
                    };

Adding Raw support to the ImageSaver runnable

Add CaptureResult & CameraCharateristic members to the ImageSaver class

private static class ImageSaver implements Runnable {

        private final Image mImage;
        private final Activity mActivity;
        private final Handler mHandler;
        private final CaptureResult mCaptureResult;
        private final CameraCharacteristics mCameraCharacteristics;

        private ImageSaver(Activity activity, Image image, Handler handler, CaptureResult captureResult,
                           CameraCharacteristics cameraCharacteristics) {
            mActivity = activity;
            mImage = image;
            mHandler = handler;
            mCaptureResult = captureResult;
            mCameraCharacteristics = cameraCharacteristics;
        }

In the run method add a switch statement to support either a jpeg or raw sensor image

@Override
       public void run() {
           int format = mImage.getFormat();
           switch (format) {
               case ImageFormat.JPEG:

    break;
               case ImageFormat.RAW_SENSOR:

  break;

Implement the code to create a digital negative and write it to file that was created for the raw image

case ImageFormat.RAW_SENSOR:
    DngCreator dngCreator = new DngCreator(mCameraCharacteristics, mCaptureResult);
    FileOutputStream rawFileOutputStream = null;
    try {
        rawFileOutputStream = new FileOutputStream(mRawImageFile);
        dngCreator.writeImage(rawFileOutputStream, mImage);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        mImage.close();
        if(rawFileOutputStream != null) {
            try {
                rawFileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    break;

Now add the CaptureResult & CameraCharacteristics to the creation of ImageSaver when the image is available for saving

private final ImageReader.OnImageAvailableListener mOnImageAvailableListener =
            new ImageReader.OnImageAvailableListener() {
                @Override
                public void onImageAvailable(ImageReader reader) {
                    mBackgroundHandler.post(new ImageSaver(mActivity, reader.acquireNextImage(), mUiHandler,
                            mCaptureResult, mCameraCharacteristics));
                }
            };

private final ImageReader.OnImageAvailableListener mOnRawImageAvailableListener =
           new ImageReader.OnImageAvailableListener() {
               @Override
               public void onImageAvailable(ImageReader reader) {
                   mBackgroundHandler.post(new ImageSaver(mActivity, reader.acquireNextImage(), mUiHandler,
                           mCaptureResult, mCameraCharacteristics));
               }
           };

That complete’s the Raw capture series using the android camera2 api’s.

About The Author
-

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>