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