Java method to read image EXIF information

  • 2020-04-01 03:58:45
  • OfStack

This article illustrates a Java method for reading image EXIF information. Share with you for your reference. Specific analysis is as follows:

Let's start with EXIF, short for Exchangeable Image File, a format for digital camera photos. This format can be used to record the attributes of digital photos, such as the brand and model of the camera, the time the photo was taken, the aperture, shutter speed, ISO, and so on. In addition, it can record shooting data and format photos so that they can be output to exif-compatible peripherals, such as photo printers.

At present, the most common image format that supports EXIF information is JPG. Many image tools can directly display the EXIF information of the image, including some famous photo album websites that also provide pages for displaying the EXIF information of the photo. This article focuses on how the Java language reads the EXIF information of an image, including how to adjust the image to fit the user.

The most easy-to-use Java package for EXIF information processing so far is the metadata-extractor written by Drew Noakes. The latest version of the project is 2.3.4, which supports EXIF 2.2. You can download the latest version of this project directly from http://www.drewnoakes.com/code/exif/ including its source code.

It is important to note that not every JPG image file contains EXIF information. You can click on the selected image in Windows explorer and if the image contains EXIF information, the property -> This is shown in the abstract.

ExifTester. Java is as follows:


import java.io.File;
import java.util.Iterator;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import com.drew.metadata.exif.ExifDirectory;

public class ExifTester {
   public static void main(String[] args) throws Exception {
     File jpegFile = new File("C:/1.JPG");
     Metadata metadata = JpegMetadataReader.readMetadata(jpegFile);
     Directory exif = metadata.getDirectory(ExifDirectory.class);
     Iterator tags = exif.getTagIterator();
     while (tags.hasNext()) {
       Tag tag = (Tag)tags.next();
       System.out.println(tag);
     }
   }
}

Operation results:


[Exif] Make - OLYMPUS OPTICAL CO.,LTD
[Exif] Model - u10D,S300D,u300D
[Exif] Orientation - Top, left side (Horizontal / normal)
[Exif] X Resolution - 72 dots per inch
[Exif] Y Resolution - 72 dots per inch
[Exif] Resolution Unit - Inch
[Exif] Software - 22-1012            
[Exif] Date/Time - 2005:04:14 13:47:10
[Exif] YCbCr Positioning - Datum point
[Exif] Exposure Time - 0.01 sec
[Exif] F-Number - F5.2
[Exif] Exposure Program - Program creative (slow program)
[Exif] ISO Speed Ratings - 80
[Exif] Exif Version - 2.20
[Exif] Date/Time Original - 2005:04:14 13:47:10
[Exif] Date/Time Digitized - 2005:04:14 13:47:10
[Exif] Components Configuration - YCbCr
[Exif] Exposure Bias Value - 0 EV
[Exif] Max Aperture Value - F3.1
[Exif] Metering Mode - Multi-segment
[Exif] Light Source - Unknown
[Exif] Flash - Flash did not fire, auto
[Exif] Focal Length - 17.4 mm
[Exif] User Comment - 
[Exif] FlashPix Version - 1.00
[Exif] Color Space - sRGB
[Exif] Exif Image Width - 1024 pixels
[Exif] Exif Image Height - 768 pixels
[Exif] File Source - Digital Still Camera (DSC)
[Exif] Windows XP Title -  The scenery 
[Exif] Windows XP Author -  pick-up 
[Exif] Windows XP Keywords -  You are the only one for me 
[Exif] Windows XP Subject -  My first one 
[Exif] Custom Rendered - Normal process
[Exif] Exposure Mode - Auto exposure
[Exif] White Balance - Auto white balance
[Exif] Digital Zoom Ratio - 1
[Exif] Scene Capture Type - Landscape
[Exif] Gain Control - None
[Exif] Contrast - None
[Exif] Saturation - None
[Exif] Sharpness - None
[Exif] Unknown tag (0xc4a5) - 80 114 105 110 116 73 77 0 480 2 -10...
[Exif] Compression - JPEG (old-style)
[Exif] Thumbnail Offset - 2022 bytes
[Exif] Thumbnail Length - 5864 bytes
[Exif] Thumbnail Data - [5864 bytes of thumbnail data]

Read only one item of information:


package test;
import java.io.File;
import java.util.Iterator;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import com.drew.metadata.exif.ExifDirectory;

public class PicExif {
   public static void main(String[] args) throws Exception {
     File jpegFile = new File(
             "C:/1.JPG");
     Metadata metadata = JpegMetadataReader.readMetadata(jpegFile);
     Directory exif = metadata.getDirectory(ExifDirectory.class);
     Iterator tags = exif.getTagIterator();
     if(exif.containsTag(ExifDirectory.TAG_WIN_AUTHOR)){
       System.out.println("Pic author is "+exif.getDescription(ExifDirectory.TAG_WIN_AUTHOR));
     }
     if(exif.containsTag(ExifDirectory.TAG_WIN_TITLE)){
      System.out.println("Pic title is "+exif.getDescription(ExifDirectory.TAG_WIN_TITLE));  
     }
     if(exif.containsTag(ExifDirectory.TAG_WIN_KEYWORDS)){
    System.out.println("Pic keyword is "+exif.getDescription(ExifDirectory.TAG_WIN_KEYWORDS));
     }
   }
}

I hope this article has been helpful to your Java programming.


Related articles: