Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read JPEG file attributes with Java?

Tags:

java

image

I want to read information from an image (jpg) which has some extra information included from a digital camera like creation date, focus, flash on off, ... How can I get this information.

My first idea was.

BufferedImage image = ImageIO.read(filePicture);
if (image().getPropertyNames() != null) {
    for (int j = 0; j < image().getPropertyNames().length; j++) {
        String key = image().getPropertyNames()[j];
        String value = (String) image().getProperty(key);
        System.out.println(key + ": " + value);
    }
}

But the getPropertyNames() returns null!

like image 719
Sujeeth Avatar asked Oct 07 '13 11:10

Sujeeth


People also ask

Can Java read JPEG?

Java 2D supports loading these external image formats into its BufferedImage format using its Image I/O API which is in the javax. imageio package. Image I/O has built-in support for GIF, PNG, JPEG, BMP, and WBMP.

Does JPEG contain metadata?

A typical JPEG file contains a lot more information than just the bytes required to store image data. A JPEG file also has a lot of metadata in each file containing auxiliary information about the image. On an average, this kind of metadata occupies 16% of size of the JPEG file.

Which is used to read an image file in Java?

In Java, we can use the javax. imageio. ImageIO class to read and write an image.


1 Answers

Another simple option is to use metadata-extractor:

Metadata metadata = ImageMetadataReader.readMetadata(imagePath);

To iterate all values in the file:

for (Directory directory : metadata.getDirectories()) {
    for (Tag tag : directory.getTags()) {
        System.out.println(tag);
    }
}

You can also read specific values from specific directories:

// obtain the Exif SubIFD directory
ExifSubIFDDirectory directory 
    = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);

// query the datetime tag's value
Date date = directory.getDate(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL);

The library is available for Maven users too.

(Full disclosure: I am the author of this library)

like image 140
Drew Noakes Avatar answered Sep 21 '22 13:09

Drew Noakes