Following episode 3, where I talked about metadata in relational databases, this week, I am talking about metadata as found in digital photography (and specifically EXIF).
Wikipedia defines EXIF as an exchangeable image file format (officially Exif, according to JEIDA/JEITA/CIPA specifications). It is a standard that specifies the formats for images, sound, and ancillary tags used by digital cameras (including smartphones), scanners, and other systems handling image and sound files recorded by digital cameras.
The set of tags are grouped into directories. The directories form an essential set of the metadata associated to a digital photo.
The code is available on GitHub.
For convenience, the Java code is added here. You will see the main steps of this small application:
- Extract the metadata from the file.
- Loop through each directory.
- Loop through each tag for each directory
package net.jgp.labs.photo.lab100_read_exif;
import java.io.File;
import java.io.IOException;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
/**
* Displays the EXIF attributes of a specific photo.
*
* @author jgp
*/
public class ReadExifApp {
/**
* main() is your entry point to the application.
*
* @param args
*/
public static void main(String[] args) {
ReadExifApp app = new ReadExifApp();
app.start();
}
/**
* The processing code.
*/
private void start() {
File file = new File("data/obelix-clio.jpeg");
try {
Metadata metadata = JpegMetadataReader.readMetadata(file);
print(metadata, "Using JpegMetadataReader");
} catch (JpegProcessingException e) {
print(e);
} catch (IOException e) {
print(e);
}
}
private static void print(Metadata metadata, String method) {
System.out.println();
System.out.println("-------------------------------------------------");
System.out.print(' ');
System.out.print(method);
System.out.println("-------------------------------------------------");
System.out.println();
// A Metadata object contains multiple Directory objects
for (Directory directory : metadata.getDirectories()) {
// Each Directory stores values in Tag objects
for (Tag tag : directory.getTags()) {
System.out.println(tag);
}
// Each Directory may also contain error messages
for (String error : directory.getErrors()) {
System.err.println("ERROR: " + error);
}
}
}
private static void print(Exception exception) {
System.err.println("EXCEPTION: " + exception);
}
}
This is a basic metadata extraction process. You can imagine that there is a more complex follow-up coming…
More resources:
The YouTube channel for DataFriday lists all episodes. You can attend the live show every Friday morning at 8 AM EST, check out the DataFriday page.