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;
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;
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.
CodeProject