SoFunction
Updated on 2025-04-07

What is EXIF ​​under Android

1. What is Exif
Exif (Exchangeable Image File Interchangeable Image File) is an image file format, and its data storage is exactly the same as that of JPEG format. In fact, the Exif format is to insert digital photos into the head of the JPEG format, including various shooting conditions such as aperture, shutter, white balance, ISO, focal length, date and time, as well as camera brand, model, color encoding, sound recorded during shooting, as well as Global Positioning System (GPS), thumbnails, etc. Simply put, Exif=JPEG+ shooting parameters. Therefore, you can use any picture viewing software that can view JPEG files to browse photos in Exif format, but not all graphic programs can handle Exif information.

All JPEG files start with the string "0xFFD8" and end with the string "0xFFD9". There is a series of strings in the format "0xFF??" in the file header, called "identification", which are used to mark the information segments of the JPEG file. "0xFFD8" indicates the beginning of image information, "0xFFD9" indicates the end of image information. There is no information after these two identifiers, while other identifiers are immediately followed by some information characters.
0xFFE0 -- The identifier between 0xFFEF is called "application marks" and is not used by conventional JPEG files. Exif uses these information strings to record shooting information such as shutter speed, aperture value, etc., and can even include global positioning information. According to the definition of these identifiers by Exif2.1 standard, digital cameras can record various shooting information into digital images, application software can read these data, and then retrieve their specific meanings according to Exif2.1 standard, generally including the following information:
Image Description Image description, source. Refers to the tool for generating images
Artist Author Some cameras can enter the user's name
Make producer refers to product manufacturer
Model Model refers to the device model
Orientation Direction Some cameras support, while others do not
XResolution/YResolution X/Y direction resolution There are special entries in this column to explain this problem.
ResolutionUnit resolution unit is generally PPI
Software Display firmware version of Firmware
DateTime Date and Time
YCbCrPositioning Hue Positioning
ExifOffsetExif information location, defines the writing of Exif in the information in the file, and some software does not display it.
ExposureTime Exposure Time That is the shutter speed
FNumber aperture coefficient
ExposureProgram exposure program refers to the setting of programmatic automatic exposure. Different cameras are different, which may be Sutter Priority, Aperture Priority, etc.
ISO speed ratings sensitivity
ExifVersionExif version
DateTimeOriginal creation time
DateTimeDigitized Digital Time
ComponentsConfiguration image construction (multiple-referring to color combination scheme)
CompressedBitsPerPixel (BPP) color bits per pixel when compressed refer to the compression degree
ExposureBiasValue exposure compensation.
MaxApertureValue Maximum aperture
MeteringMode metering method, average metering, central key metering, spot metering, etc.
Lightsource light source refers to white balance setting
Whether to use flash.
FocalLength focal length, generally displays the physical focal length of the lens. Some software can define a coefficient to display the focal length equivalent to the 35mm camera MakerNote (User Comment) Author marking, description, and record
FlashPixVersionFlashPix version (some models support)
ColorSpace color gamut, color space
ExifImageWidth(Pixel X Dimension) image width refers to the number of horizontal pixels
ExifImageLength(Pixel Y Dimension) Image height refers to the number of vertical pixels
Interoperability IFD universality extension definition pointer is related to TIFF files, and the specific meaning is unknown
FileSource source file Compression compression ratio.

2. Photographing process in Camera

During the Android Camera program development process, you need to use knowledge related to Exif. If it is handled improperly, the captured JPEG pictures will not be browsed normally.
The Camera application in the Froyo (Android 2.2) source code does not write Exif information, but only reads it. The write operation for Exif is handed over to the Camera hardware abstraction layer to complete, which is Google's design logic. However, different Android platforms and related sub-platforms, coupled with different Camera applications, may be a situation like this: the bottom layer does not write Exif, and the upper layer application does not write Exif information, so the display information of the picture will be lost. The most serious impact is the Orientation parameter.

The logic of Froyo camera is as follows:
In the Camera Activity, there is an internal class ImageCapture, which contains an important method:

private void capture() {
// Set rotation.
(mLastOrientation);
....................
.....................
 (mParameters);

(mShutterCallback, mRawPictureCallback, mPostViewPictureCallback, new JpegPictureCallback(loc));
}

The general process is as follows:
1. Add the direction of the camera when taking a photo to the example;
2. Pass all camera photography parameters to the object;
3. Call the method takePicture and set up the 4 very important callbacks;
4. The thing about generating Exif data is done by HAL;
5. The fourth callback returns data (this callback is the most important and indispensable, which means that the first three callbacks are set to Null and will not affect the camera function), see the following code:

private final class JpegPictureCallback implements PictureCallback {
public void onPictureTaken(final byte[] jpegData, final  camera) {
//jpegData is JPEG data, which is generated by the HAL layer based on various parameters (ie, instances) transmitted by the application and the JPEG compression algorithm.(jpegData, camera, mLocation);
}
}

3.Exif usage method and code optimization plan

Where to use Exif information? I encountered at least a few places like this:
1. Generate the thumbnail in the upper right corner;
2. Image display application, such as the gallery3d application that comes with Android;
3. Picture echo;
4. Applications that require camera attachments to be added for short (color) messages.

Check out the source code: This is how the Exif direction parameters are read in ImageManager.

  public static int getExifOrientation(String filepath) {
    int degree = 0;
    ExifInterface exif = null;
    try {
      exif = new ExifInterface(filepath);
    } catch (IOException ex) {
      (TAG, "cannot read exif", ex);
    }
    if (exif != null) {
      int orientation = (
        ExifInterface.TAG_ORIENTATION, -1);
      if (orientation != -1) {
        // We only recognize a subset of orientation tag values.
        switch(orientation) {
          case ExifInterface.ORIENTATION_ROTATE_90:
            degree = 90;
            break;
          case ExifInterface.ORIENTATION_ROTATE_180:
            degree = 180;
            break;
          case ExifInterface.ORIENTATION_ROTATE_270:
            degree = 270;
            break;
        }

      }
    }
    return degree;
  }

This method can be further optimized so that the writing of Exif information no longer depends on the underlying layer. That is to compare whether the orientation transmitted to the underlying layer is equal to the actual returned. If it is not equal, an error occurs when writing the Exif information to the underlying layer, and we can correct it at the application layer.
You can add a judgment branch as follows: (where EXIF_ORIENTATION is the value passed to the underlying layer by our cached application).

else if(orientation == 0 && EXIF_ORIENTATION != 0) {
        switch (EXIF_ORIENTATION) {
        case 90:
          orientation = ExifInterface.ORIENTATION_ROTATE_90;
          degree = 90;
          break;
        case 180:
          orientation = ExifInterface.ORIENTATION_ROTATE_180;
          degree = 180;
          break;
        case 270:
          orientation = ExifInterface.ORIENTATION_ROTATE_270;
          degree = 270;
          break;
        }
        (ExifInterface.TAG_ORIENTATION, (orientation));
        try {
          ();
        } catch (IOException e) {
           (TAG, "cannot save exif", e);
        }
      }

Operations on Exif at the application layer are done through the interface.
The getAttribute (String tag, String value) is set, and the get can be set by public int getAttributeInt (String tag, int defaultValue) and public String getAttribute (String tag) both. The getAttributeInt overloads the method with the second parameter is the default value set by us. If successful, the value of the corresponding tag is returned; the specific integer content is the method directly. Overload method 2 This method directly returns the result, and if it fails, it is null.

The above is all about this article, I hope it will be helpful to everyone's learning.