The Extensible Metadata Platform (XMP) data model can be used to store any set of metadata properties in the form of name/value pair encoded as XML-formatted text. As a Java developer, you can easily add a custom XMP metadata package to your images programmatically. In this article, you will learn how to add or remove custom XMP metadata from GIF using Java.
The following topics are discussed/covered in this article:
- Java API to Add or Remove Custom XMP Metadata
- Add Custom XMP Metadata Package to GIF using Java
- Read Custom XMP Metadata Package Properties using Java
- Remove Custom XMP Metadata Package using Java
Java API to Add or Remove Custom XMP Metadata
For adding or removing the custom XMP metadata package from GIF images, we will be using GroupDocs.Metadata for Java API. It allows you to add, edit, retrieve, and remove metadata properties from the supported documents and image file formats. The API works with the most notable metadata standards such as built-in metadata properties e.g., author, created date, format-specific metadata properties e.g., XMP, EXIF, IPTC, Image Resource Blocks, ID3, and custom metadata properties.
You can download the JAR of the API or just add the following pom.xml configuration in your Maven-based Java application to try the below-mentioned code examples.
<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>21.8</version>
</dependency>
Add Custom XMP Metadata Package to GIF using Java
You can create and add a fully custom XMP package with user-defined properties by following the simple steps given below:
- Firstly, load the GIF image using the Metadata class.
- Then, call the Metadata.getRootPackage() as IXmp standard to get the root package.
- Create an instance of the XmpPackage class to create a new package.
- Now, set various properties to define the package such as Prefix, and NamespaceUri.
- Then, call the Set() method to set user-defined metadata properties in Name/ Value pair.
- Create an instance of the XmpPacketWrapper class which contains serialized XMP package.
- Now, call the XmpPacketWrapper.AddPackage() method to add the created custom XmpPackage.
- Use the IXmp.setXmpPackage() method to set the XMP metadata package.
- Finally, save the output file using the Metadata.save() method.
The following code sample shows how to create and add a custom XMP metadata package to a GIF image using Java.
The above code sample shall add the XMP metadata package to the input image. Please find below the output generated by the ExifTool.
Read Custom XMP Package Metadata Properties using Java
You can read all the user-defined properties of the custom XMP package by following the steps given below:
- Firstly, load the GIF image using the Metadata class.
- Then, call the Metadata.getRootPackage() as IXmp standard to get the root package. It provides access to all metadata properties extracted from the file.
- Call the IXmp.getXmpPackage() method to check if XmpPackage exist.
- Now, get array of XmpPackage by calling the IXmp.getXmpPackage().getPackages() method
- Iterate over all the packages and call the XmpPackage.getNamespaceUri() and XmpPackage.getPrefix() methods to show package Namespace URI and the prefix for each package
- Loop through all the keys returned by XmpPackage.getKeys() method to print metadata value
- Finally, call the XmpPackage.findProperties() method for each package Key to recursively search and find the metadata property matching the package key.
The following code sample shows how to read all the properties defined in the custom XMP package using Java.
The above code sample shall produce the following output:
https://groupdocs.com
gd
gd:Copyright: Copyright (C) 2021 GroupDocs. All Rights Reserved.
gd:CreationDate: Sat Oct 16 00:13:15 PKT 2021
gd:Company: <rdf:Seq><rdf:li>Aspose</rdf:li><rdf:li>GroupDocs</rdf:li></rdf:Seq>
Remove Custom XMP Package using Java
You can remove the XMP package from GIF images by following the steps given below:
- Firstly, load the GIF image using the Metadata class.
- Then, call the Metadata.getRootPackage() as IXmp standard to get the root package.
- Now, use the IXmp.setXmpPackage() to set the XmpPackege as null
- Finally, save the output file using the Metadata.save() method
The following code sample shows how to remove the XMP metadata package from a GIF image using Java.
The above code sample shall remove the XMP metadata package from the input image. The ExifTool read the following metadata from the generated output GIF image.
Get a Free License
You can try the API without evaluation limitations by requesting a free temporary license.
Conclusion
In this article, you have learned how to add custom XMP package metadata to GIF images using Java. Moreover, you have seen how to read XMP package properties and remove them from a GIF image programmatically. You can learn more about GroupDocs.Metadata for Java API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.