Search code examples
androidandroid-camera2

Saving Images as Ultra HDR (JPEG_R)


The only code I've found related to the new Ultra-HDR (JPEG_R) format is this: https://github.com/google/libultrahdr/blob/main/examples/ultrahdr_app.cpp

As far as I can tell, it's a general purpose/desktop thing not directly applicable to android.

I'm working with a modified version of the Camera2Raw sample, and when I try to setup a JPEG_R ImageReader, I simply get a "Configuration Failed" error. My phone is a Pixel 6 Pro, which can save ultra HDR images with the official camera app. And even if the ImageReader were to successfully configure, I'd still need to save JPEG_Rs correctly. For instance, here is the code for saving normal JPEGs and DNGs:

switch (format) {
                case ImageFormat.JPEG: {
                    ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
                    byte[] bytes = new byte[buffer.remaining()];
                    buffer.get(bytes);
                    FileOutputStream output = null;
                    try {
                        output = new FileOutputStream(mFile);
                        output.write(bytes);
                        success = true;
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        mImage.close();
                        closeOutput(output);
                    }
                    break;
                }
                case ImageFormat.RAW_SENSOR: {
                    DngCreator dngCreator = new DngCreator(mCharacteristics, mCaptureResult);
                    FileOutputStream output = null;
                    try {
                        output = new FileOutputStream(mFile);
                        dngCreator.writeImage(output, mImage);
                        success = true;
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        mImage.close();
                        closeOutput(output);
                    }
                    break;
                }

What might a case ImageFormat.JPEG_R look like? If it's really complicated, then so be it. But I just want to make sure there isn't some simple way to do this that I don't know about. The format is still pretty new, so maybe it's just a matter of waiting for someone to make a good sample implementation.


Solution

  • First you need to check if device supports UltraHDR capture. you can refer this method. https://github.com/android/platform-samples/blob/main/samples/camera/camera2/src/main/java/com/example/platform/camera/imagecapture/Camera2UltraHDRCapture.kt#L363

    Image saving will be same as JPEG format.

    https://github.com/android/platform-samples/blob/609483bcf688d2779daeca6d257cba7cfb41ecfb/samples/camera/camera2/src/main/java/com/example/platform/camera/imagecapture/Camera2UltraHDRCapture.kt#L596