-
Notifications
You must be signed in to change notification settings - Fork 486
Getting Started (Java)
metadata-extractor
is available via Maven, or via a download from the releases page.
Ensure both JAR files are available on your classpath: metadata-extractor-x.x.x.jar
and xmpcore-x.x.x.jar
(where x.x.x
represents the version you're using).
Using this library is straightforward. Conceptually there are two steps to its usage:
- Read a
Metadata
object from an image - Query the
Metadata
object to retrieve one or more values
NOTE:
- These examples are based upon version 2.7.2 of the API. Earlier versions may differ slightly.
-
import
statements and exception handling code have been omitted for clarity. - Fully-featured sample classes are available in the source distribution.
The simplest way to read metadata is to use ImageMetadataReader
.
If you know the type of file, you can replace ImageMetadataReader
with the specific reader, for example JpegMetadataReader
. Using ImageMetadataReader
is often safer as it uses FileTypeDetector
to inspect the file's contents in order to determine which decoding method to use and incurs very little overhead.
For an image file of any supported type, use:
File jpegFile = new File("myImage.jpg");
Metadata metadata = ImageMetadataReader.readMetadata(jpegFile);
Reading from a stream is much the same:
Metadata metadata = ImageMetadataReader.readMetadata(stream);
A Metadata
object contains zero or more Directory
objects. These in turn contain zero or
more Tag
objects. Tags hold values representing the metadata for the source image.
for (Directory directory : metadata.getDirectories()) {
for (Tag tag : directory.getTags()) {
System.out.println(tag);
}
}
// obtain the Exif directory
ExifSubIFDDirectory directory
= metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
// query the tag's value
Date date
= directory.getDate(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL);
Note that tag values have specific data types. Attempts will be made to convert to the type you request, but this is not always possible.
If you only wish to display a friendly string version of the tag's value, simply call Tag.toString()
.
Alternatively, you can wrap the directory with a Descriptor
and have strongly-typed access with
descriptive method names:
// obtain a specific directory
Directory directory
= metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
// create a descriptor
ExifSubIFDDescriptor descriptor
= new ExifSubIFDDescriptor(directory);
// get tag description
String program = descriptor.getExposureProgramDescription();
In the above example, program
would contain a detailed string such as "Manual control"
or
"Aperture Priority"
, whereas the raw tag value is actually an integer. Descriptors not only
decode enum values, but also prepend/append units ("-1 EV"
, "f/2.8"
) and provide methods that
calculate derived properties for convenience.