Home > Java APIs > Retrieve Exchangeable Image File Format data using Apache Sanselan

Retrieve Exchangeable Image File Format data using Apache Sanselan

Exchangeable image file format (Exif) is a specification for the image file format used by digital cameras. The specification uses the existing JPEG, TIFF Rev. 6.0, and RIFF WAV file formats, with the addition of specific metadata tags. As we are now aware of what these things are, why not write a Small Java application to extract these data. But developing a full-fledged API is beyond the scope of this blog. So, I chose to use Sanselan from Apache Foundation. This Pure-Java library reads and writes a variety of image formats, including fast parsing of image info (size, color space, icc profile, etc.) and metadata. You can download it from Apache website at http://commons.apache.org/sanselan/download_sanselan.cgi. Using Sanselan, it is very easy to read Exif tags. I have written a small piece of code to get the basic information of an image. The steps required to perform are as follows:

STEP 1: Download Sanselan JAR files from Apache website.

STEP 2: Create a Java Project in your eclipse and Add Sanselan lib at your classpath.

STEP 3: Write ExifReader class to read Exif tag information.

package blog.exif.util;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import org.apache.sanselan.ImageReadException;
import org.apache.sanselan.Sanselan;
import org.apache.sanselan.common.IImageMetadata;
import org.apache.sanselan.formats.jpeg.JpegImageMetadata;
import org.apache.sanselan.formats.tiff.TiffField;
import org.apache.sanselan.formats.tiff.constants.TiffConstants;
/**
* @author Bikash
*/
public class ExifReader {
public static Hashtable getMetadata(File file) throws ImageReadException, IOException {
  IImageMetadata metadata = Sanselan.getMetadata(file);
  Hashtable<String, String> exifInfomationTable = new Hashtable<String, String>();
  if (metadata instanceof JpegImageMetadata) {
    JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
    TiffField field = jpegMetadata
    exifInfomationTable.put(field.getTagName(), field.getValueDescription());
    field = jpegMetadata.findEXIFValue(TiffConstants.EXIF_TAG_MODEL);
    exifInfomationTable.put(field.getTagName(), field.getValueDescription());
    field = jpegMetadata.findEXIFValue(TiffConstants.EXIF_TAG_CREATE_DATE);
    exifInfomationTable.put(field.getTagName(), field.getValueDescription());
    field = jpegMetadata.findEXIFValue(TiffConstants.EXIF_TAG_FNUMBER);
    exifInfomationTable.put(field.getTagName(), field.getValueDescription());
    field = jpegMetadata.findEXIFValue(TiffConstants.EXIF_TAG_ISO);
    exifInfomationTable.put(field.getTagName(), field.getValueDescription());
    field = jpegMetadata.findEXIFValue(TiffConstants.EXIF_TAG_EXPOSURE_TIME);
    exifInfomationTable.put(field.getTagName(), field.getValueDescription());
    field = jpegMetadata.findEXIFValue(TiffConstants.EXIF_TAG_EXIF_IMAGE_WIDTH);
    exifInfomationTable.put(field.getTagName(), field.getValueDescription());
    field = jpegMetadata.findEXIFValue(TiffConstants.EXIF_TAG_EXIF_IMAGE_LENGTH);
    exifInfomationTable.put(field.getTagName(), field.getValueDescription());
  }
  return exifInfomationTable;
 }
}

STEP 4: Create a Main class to validate the correctness of the information

package blog.exif.main;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import org.apache.sanselan.ImageReadException;
import blog.exif.util.ExifReader;
/**
* @author Bikash
*/
public class Main {
  public static void main(String[] args) {
  File file = new File("D:\\Photos\\Kashmir\\DSC01945.JPG");
  try {
    Hashtable<String, String> exifInfomationTable = ExifReader.getMetadata(file);
    for(String key : exifInfomationTable.keySet())
    {
      System.out.println(key + " : " + exifInfomationTable.get(key));
    }
  } catch (ImageReadException e) {
      e.printStackTrace();
  } catch (IOException e) {
      e.printStackTrace();
  }
 }
}

STEP 5: Execute the application to see the output.



Now, the power is in your hand. Feel free to add or change the values of TiffConstants to get more information. You can even develop you swing base GUI utility for more robust image metadata reader.

Advertisements
Categories: Java APIs
  1. claudio
    November 23, 2011 at 2:18 pm

    hello there.
    there is an error in the code,line 20.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: