How to find image orientation in Java

Overview

In this article we will see how we can get the orientation of an image in Java. EXIF is short for Exchangeable Image File, a format that is a standard for storing interchange information in digital photography image files using JPEG compression. Some images may even store GPS information so you can easily see where the images were taken.

Many newer digital cameras (both dSLR and Point & Shoot digicams) have a built-in orientation sensor. The output of this sensor is used to set the EXIF orientation flag in the image file’s metatdata to reflect the positioning of the camera with respect to the ground.

Orientation flags
Figure 1. Orientation flags

Code

For this example we will make use of the metadata-extractor API. Define the below dependency in your pom.xml

<dependency>
    <groupId>com.drewnoakes</groupId>
    <artifactId>metadata-extractor</artifactId>
    <version>2.14.0</version>
</dependency>

Call the readMetadata() method of the ImageMetadataReader class.

final Metadata metadata = ImageMetadataReader.readMetadata(new ByteArrayInputStream(imageContent));

This will return the metadata information about the image. Now call the the getFirstDirectoryOfType() on this metadata object.

final ExifIFD0Directory exifDirectory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);

For images which don’t have the exif data information this value will be null, so you would need to check that before doing the next call. Now call the getInt() method by passing the TAG_ORIENTATION.

exifDirectory.getInt(ExifIFD0Directory.TAG_ORIENTATION);

This returns the specified tag’s value as an int. Below is the snippet of the function:

public int getImageOrientation(byte[] imageContent) throws ImageProcessingException, IOException, MetadataException {
    final Metadata metadata = ImageMetadataReader.readMetadata(new ByteArrayInputStream(imageContent));
    final ExifIFD0Directory exifDirectory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
    return exifDirectory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
}

Test

In this section we will see how we can test our code

@Test
public void getImageOrientation() throws Exception {
    assertEquals(1, getOrientation("one.jpg"));
    assertEquals(2, getOrientation("two.jpg"));
    assertEquals(3, getOrientation("three.jpg"));
}

private int getOrientation(String fileName) throws Exception {
    final InputStream imageStream = getClass().getClassLoader().getResourceAsStream(fileName);
    final byte[] imageContent = IOUtils.toByteArray(imageStream);
    return this.imageOrientation.getImageOrientation(imageContent);
}

Conclusion

In this article we saw how to find the orientation of an image in Java. To read more about EXIF orientation you can refer to EXIF Orientation. The full source-code can be found at java-image-orientation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: