EXIF (Exchangeable Image File Format) is the standard to specify the image and sound formats mainly used by digital cameras and scanners. EXIF data includes the tagging and metadata information about the captured image file. Metadata may contain information like camera make, model, shutter speed, date and time, aperture, exposure time, X resolution, Y resolution. etc.
If you want to manage, extract, update, or remove EXIF data of your images programmatically, then this article is for you. This article will cover the following ways to manipulate with EXIF data in Java:
- Extract EXIF data - EXIF Data Viewer
- Extract all EXIF tags from images
- Update EXIF properties
- Remove EXIF metadata
Java Metadata Manipulation Library
GroupDocs.Metadata for Java is an easy to use metadata management Java API. It has the ability to not only extract metadata from images like JPG, PNG, or WebP but also it can add, edit, update, and remove metadata from the images and other documents with different options.
I am using this API in this article so please make sure to download or integrate it in your Maven-based applications by just adding the following configurations to the pom.xml.
Repository & Dependency
<repository>
<id>GroupDocsJavaAPI</id>
<name>GroupDocs Java API</name>
<url>http://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-metadata</artifactId>
<version>20.5</version>
<classifier>javadoc</classifier>
</dependency>
Extract EXIF Data from Images in Java - Metadata Viewer
You can read the EXIF data properties by following simple steps. Let’s start with the extraction of EXIF data from this picture of Eiffel Tower. I have selected a JPG file as an example image, you can use any of your files whether its a PNG, WebP, BMP, GIF, or TIFF.
- Load the image source file containing EXIF data information using the Metadata class constructor.
- Get its root package by calling getRootPackage() method.
- From the root package, get its EXIF package by calling getExifPackage() method.
- Once you have the EXIF package, you can get image EXIF properties like Make, Model, Width, Length, Date-Time, etc as shown in the below Java code example.
// Extract EXIF Data Package Information from image in Java
try (Metadata metadata = new Metadata("eiffel-tower.jpg")) {
IExif root = (IExif) metadata.getRootPackage();
if (root.getExifPackage() != null) {
// Extract EXIF Package
ExifPackage exifPackage = root.getExifPackage();
System.out.println("Make : " + exifPackage.getMake());
System.out.println("Model : " + exifPackage.getModel());
System.out.println("Width : " + exifPackage.getImageWidth());
System.out.println("Length : " + exifPackage.getImageLength());
System.out.println("DateTime : " + exifPackage.getDateTime());
}
}
Here is the EXIF information you will get as a result of the above code.
Make : NIKON CORPORATION
Model : NIKON D3000
Width : 640
Length : 424
DateTime : 2014:08:09 10:35:13
For further IFD (Image File Directory) and GPS (Global Positioning System) package information, you just have to call the respective methods of EXIF package i.e. getExifIfdPackage() or getGpsPackage(). From these packages, you may extract more information like;
- Image capture device serial number
- Camera Owner name
- User comments
- Altitude
- Latitude
- Longitude
- etc.
Here is the code that you can add in your above method to display EXIF data along with IFD and GPS information.
// EXIF IFD Package
ExifIfdPackage exifIfdPackage = exifPackage.getExifIfdPackage();
System.out.println("BodySerialNumber : " + exifIfdPackage.getBodySerialNumber());
System.out.println("CameraOwnerName : " + exifIfdPackage.getCameraOwnerName());
System.out.println("UserComment : " + exifIfdPackage.getUserComment());
// EXIF GPS Information Package
ExifGpsPackage exifGpsPackage = exifPackage.getGpsPackage();
System.out.println("getAltitude : " + exifGpsPackage.getAltitude());
System.out.println("Latitude Ref : " + exifGpsPackage.getLatitudeRef());
System.out.println("LongitudeRef : " + exifGpsPackage.getLongitudeRef());
Read All EXIF Tags of Images using Java
If you want to show or extract all the EXIF properties of any image or file, you may do it in steps similar to the above examples:
- Just load the file with Metadata constructor.
- Get the EXIF root package by calling the method getRootPackage().
- Get the EXIF package by calling getExifPackage() method.
- Iterate through the EXIF package to get your desired name-value pairs.
- Similarly, get the IFD and GPS packages and displays its keys and values.
try (Metadata metadata = new Metadata("eiffel-tower.jpg")) {
IExif root = (IExif) metadata.getRootPackage();
if (root.getExifPackage() != null) {
String pattern = "%s = %s";
// Reading all EXIF tags.
for (TiffTag tag : root.getExifPackage().toList()) {
System.out.println(String.format(pattern, tag.getName(), tag.getValue()));
}
// Extract all EXIF IFD tags.
for (TiffTag tag : root.getExifPackage().getExifIfdPackage().toList()) {
System.out.println(String.format(pattern, tag.getName(), tag.getValue()));
}
// Extract all EXIF GPS tags
for (TiffTag tag : root.getExifPackage().getGpsPackage().toList()) {
System.out.println(String.format(pattern, tag.getName(), tag.getValue()));
}
}
}
Update EXIF Properties in Java
You can even change the existing EXIF data of any image or any document easily. Steps are simple:
Update EXIF Package
- Get the EXIF package by calling getExifPackage() method.
- Use the setter methods like;
- setCopyright() - to set updated copyrights information.
- setImageDescription() - to set description of the image.
- Similarly, you can set the values for Artist, Make, Model, Software, Image Width & Height, Date, Time, etc.
Update EXIF IFD Package
Just like updating the EXIF package, you can update the properties of EXIF IFD and GPS packages. Please visit the ExifIfdPackage or ExifGpsPackage class to know how much you can customize for your valuable images and documents.
// Update/Set new values in EXIF Data (EXIF Package and EXIF IFD Package).
try (Metadata metadata = new Metadata("eiffel-tower.jpg")) {
IExif root = (IExif) metadata.getRootPackage();
// Set the EXIF package if it's missing
if (root.getExifPackage() == null) {
root.setExifPackage(new ExifPackage());
}
ExifPackage exifPackage = root.getExifPackage();
// Setting the desired values in EXIF Package and EXIF IFD Package.
exifPackage.setCopyright("Copyright (C) 2011-2020 GroupDocs. All Rights Reserved.");
exifPackage.setImageDescription("Eiffel Tower for EXIF");
exifPackage.setSoftware("GroupDocs.Metadata");
exifPackage.getExifIfdPackage().setBodySerialNumber("GD-2020");
exifPackage.getExifIfdPackage().setCameraOwnerName("GroupDocs");
exifPackage.getExifIfdPackage().setUserComment("Nice image captured in 2014");
metadata.save("eiffel-tower-updated.jpg");
}
Remove EXIF Metadata from Images in Java
This is very simple if you want to remove EXIF package from any file, just set its EXIF package to null by calling setExifPackage(null) of root package.
// Removing the EXIF data from an image.
try (Metadata metadata = new Metadata("eiffel-tower.jpg")) {
IExif root = (IExif) metadata.getRootPackage();
root.setExifPackage(null);
metadata.save("eiffel-tower-no-exif.jpg");
}
Supported Images & Other Formats
Here are the currently supported file formats by GroupDocs.Metadata. You may always visit the documentation for the updated information.
Document Type | File Formats |
---|---|
Images | BMP, GIF, JPG, JPEG, JPE, JP2, PNG, DJVU, DWG, DXF, WebP, TIFF, PSD, EMF, WMF |
Audio & Video | MP3, WAV, AVI, MOV / QT, FLV, ASF, DICOM |
See more about GroupDocs.Metadata
- Documentation
- Source code examples. Java | .NET
- GroupDocs.Metadata – The Metadata Management Solution
Let’s talk more @ Free Support Forum.