Click here to Skip to main content
15,896,154 members
Articles / All Topics

Retrieve Exchangeable Image File Format Data using Apache Sanselan

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
6 Jan 2011CPOL1 min read 12.1K   1  
How to 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.

Java
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:

Java
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 your swing base GUI utility for more robust image metadata reader.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Société Générale
India India
Summary:

1. Extensive working experience in web and windows application development, database programming using Java and .Net technologies.

2. Good knowledge in SDLC and Processes like project planning, requirement gathering, development, test planning, release management and production support.

3. Good knowledge and working experience in following methodologies:
a. Agile Development Approach
b. Test Driven Development
c. Behavior Driven Development
d. Continuous Integration & Delivery Model.

4. Excellent communication & analytical skills, good team player, great mentoring capability.

5. Interaction with customer and team spread over different geographical area.

Technologies / Languages: J2SE 5/6, J2EE, .Net 2.0/3.5, C#, ASP.NET, AJAX, XHTML, CSS, JavaScript, jQuery, PL/SQL, Web Services (SOAP based), Winforms, Hibernate, Spring, GWT, XML, XSD, XPath, SAX, JAXB, JUnit, JBehave, Mockito, Selenium, StringTemplate, Log4J, Apache Commons API

Database: Oracle, SQL Server, MySQL, Sybase

Application Server: Tomcat, Sun Application Server, JBoss, GlassFish, Jetty, IIS

Development Environments: Eclipse, NetBeans, Visual Studio.

Designing Tools: Enterprise Architect, Microsoft Visio

Version Control: SVN, Perforce

Build Management: Hudson & JCruisemonitor, TeamCity

Bug Tracking Tools: HP Quality Center, Bugzilla, JIRA

Specialties:
1. Agile Test and Behavior Driven Development
2. Continuous Delivery Approach
2. Spring IOC and MVC
3. Web Services

Comments and Discussions

 
-- There are no messages in this forum --