Home:ALL Converter>camera2 How to get Exif data from YUV_420_888 image in image reader listener

camera2 How to get Exif data from YUV_420_888 image in image reader listener

Ask Time:2019-07-06T17:48:41         Author:Shivam Pokhriyal

Json Formatter

I am trying to get Exif data from YUV_420_888 image but it is not working. I tried several solutions like saving image to disk as jpeg, converting it to a input stream but nothing seems to work.

I capture YUV_420_888 image using android camera2 api. Then in the OnImageAvailableListener I get the image and try to read its EXIF data using ExifInterface APIs. But it is always empty. I tried all the approaches from this link to get the correct byte array.

Here is my code:

@Override
public void onImageAvailable(ImageReader imageReader) {
    if (!isRecording) {
        return;
    }
    Image image = imageReader.acquireNextImage();
    File file = Util.getImagePath(context);

    OutputStream outputStream = null;
    try {
        outputStream = new FileOutputStream(file);
        outputStream.write(data); 
//// This byte array I am making using all the approaches given in this link 
https://stackoverflow.com/questions/44022062/converting-yuv-420-888-to-jpeg-and-saving-file-results-distorted-image

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    try {
        ExifInterface exifInterface = new ExifInterface(file.getAbsolutePath()); /// This is always empty
        int currentIso = (int)exifInterface.getAttributeDouble(ExifInterface.TAG_ISO_SPEED_RATINGS, 0); /// Always 0
    } catch (Exception e) {
        e.printStackTrace();
    }
    image.close();
}

EDIT: Code for capturing image:

captureRequest = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureRequest.addTarget(preview);
captureRequest.addTarget(imageReader.getSurface());
captureRequest.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_OFF);
captureRequest.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_OFF);
captureRequest.set(CaptureRequest.SENSOR_SENSITIVITY, <MANUAL_ISO>);
captureRequest.set(CaptureRequest.SENSOR_EXPOSURE_TIME, <MANUAL_EXPOSURE>);
mSession.capture(captureRequest.build(), new CameraCaptureSession.CaptureCallback() {
        @Override
        public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
            int capturedISO = result.get(CaptureResult.SENSOR_SENSITIVITY);
            long timeStamp = result.get(CaptureResult.SENSOR_TIMESTAMP);
/// Save somewhere to be used later
            super.onCaptureCompleted(session, request, result);
        }
    }, backgroundHandler);

Author:Shivam Pokhriyal,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/56912958/camera2-how-to-get-exif-data-from-yuv-420-888-image-in-image-reader-listener
yy