How can I retrieve EXIF data with Camera2 API when using YUV format?

I’m developing a camera application that captures images using ImageFormat.YUV_420_888 for processing. However, when saving the image, I need the associated EXIF metadata. The EXIF information provided by CaptureResult is insufficient for my needs. Is there an approach to obtain the complete EXIF data, similar to what I would get if I were working with JPEG format?

To retrieve full EXIF data with the Camera2 API while using the YUV_420_888 format, follow these steps:

  1. Capture JPEG for EXIF: To get EXIF data, capture a JPEG image temporarily. Use this image to extract EXIF metadata since JPEG typically retains comprehensive EXIF information.
  2. Use ExifInterface: Load the temporary JPEG into ExifInterface to read the EXIF data. For instance,

File jpegFile = new File(context.getCacheDir(), "temp.jpeg");
ExifInterface exif = new ExifInterface(jpegFile.getAbsolutePath());
String datetime = exif.getAttribute(ExifInterface.TAG_DATETIME);
// Access other EXIF tags as needed
  1. Manage Files: Delete the temporary JPEG after extracting the EXIF data to optimize storage use.

This approach allows you to process YUV images while still retrieving necessary EXIF data, optimizing your workflow and saving time without complicating the process.

Solution: Since YUV_420_888 lacks direct EXIF support, use a workaround:

  1. Temporary JPEG Capture: Briefly capture in JPEG format to access complete EXIF data.
  2. Extract Using ExifInterface: Use ExifInterface to read EXIF details:

File tempJpeg = new File(context.getCacheDir(), "temp.jpeg");
ExifInterface exif = new ExifInterface(tempJpeg.getAbsolutePath());
String dateTime = exif.getAttribute(ExifInterface.TAG_DATETIME);
// Extract additional attributes as required

Once done, delete the temporary JPEG if not needed. This method maintains workflow while providing EXIF data.