sample usage
home | software | metadata extraction | sample usage
People have requested some sample code that demonstrates usage of my metadata library. Import statements
and Exception handling code have been omitted for clarity. A fully-featured sample class is available
in the src distribution (as of v1.2).
Versions 1.n stored tag values in the ImageInfo class. Version 2.0 replaced this class with the Metadata class.
There are several ways to obtain the Metadata instance:
- This approach reads all types of known Jpeg metadata (at present,
Exif and Iptc) in a single call. In most cases, this is the most
appropriate usage.
File jpegFile = new File("myImage.jpg");
Metadata metadata = JpegMetadataReader.readMetadata(jpegFile);
Note that if you're using the beta version of release 2.4.0, you could use the more generic
ImageMetadataReader which handles several RAW formats as well as Jpeg.
- This approach shows using individual MetadataReader implementations
to read a file. This is less efficient than approach 1, as the file
is opened and read twice.
Metadata metadata = new Metadata();
new ExifReader(jpegFile).extract(metadata);
new IptcReader(jpegFile).extract(metadata);
- As fast as approach 1 (this is what goes on inside the JpegMetadataReader's
readMetadata() method), this code is handy if you want to look into other
Jpeg segments too.
JpegSegmentReader segmentReader = new JpegSegmentReader(jpegFile);
byte[] exifSegment = segmentReader.readSegment(JpegSegmentReader.SEGMENT_APP1);
byte[] iptcSegment = segmentReader.readSegment(JpegSegmentReader.SEGMENT_APPD);
Metadata metadata = new Metadata();
new ExifReader(exifSegment).extract(metadata);
new IptcReader(iptcSegment).extract(metadata);
- This approach is the slowest, because it decodes the Jpeg image. Of
course you now have a decoded image to play with. In some instances
this will be most appropriate.
JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(new FileInputStream(jpegFile));
BufferedImage image = jpegDecoder.decodeAsBufferedImage();
// now you can use the image
JPEGDecodeParam decodeParam = jpegDecoder.getJPEGDecodeParam();
Metadata metadata = JpegMetadataReader.readMetadata(decodeParam);
Once you have the Metadata object, you can use it like this:
// iterate through metadata directories
Iterator directories = metadata.getDirectoryIterator();
while (directories.hasNext()) {
Directory directory = (Directory)directories.next();
// iterate through tags and print to System.out
Iterator tags = directory.getTagIterator();
while (tags.hasNext()) {
Tag tag = (Tag)tags.next();
// use Tag.toString()
System.out.println(tag);
}
}
The Tag class has other methods you can use to customise the output:
int tag.getTagType()
String tag.getTagTypeHex()
String tag.getTagName()
String tag.getDescription() // the tag's value
tag.getDescription() returns a string. You can get the tag's value in it's original type
using the Directory instance. For example:
Directory exifDirectory = metadata.getDirectory(ExifDirectory.class);
int exifDirectory.getInt(tag.getTagType());
double exifDirectory.getDouble(tag);
float exifDirectory.getFloat(tag);
long exifDirectory.getLong(tag);
Rational exifDirectory.getRational(tag);
java.util.Date exifDirectory.getDate(tag);
Note that exceptions will be thrown if attempting to convert between significantly different
types, such as getDate where the value is a float.
Alternatively, you can search for a specific tag in a specific directory:
Directory exifDirectory = metadata.getDirectory(ExifDirectory.class);
String cameraMake = exifDirectory.getString(ExifDirectory.TAG_MAKE);
String cameraModel = exifDirectory.getString(ExifDirectory.TAG_MODEL);
Directory iptcDirectory = metadata.getDirectory(IptcDirectory.class);
String caption = iptcDirectory.getString(IptcDirectory.TAG_CAPTION);
