Kieran France Kieran France is a programmer for IDRSolutions in charge of there internal test suite. In his spare time he enjoys tinkering with gadgets and code.

Are you a Java Developer working with PDF files?

Find out why you should be using JPedal

What is XMP metadata and why it is useful

2 min read

XMPRecently at IDR Solutions we have been busy working away and added support for reading and writing of XMP metadata to Tiff images with the JDeli Java Image Library. So we are doing a quick article detailing what is XMP metadata and why it is useful.

What is XMP metadata?

XMP is a XML based metadata format to describe the contents of the file to which it is attached. With images this can be incredibly useful as it means you can attach data to the image and it will not be lost as the image is transferred and used. This, of course, is assuming you have embedded the data, storing the metadata as an external object can lead to the data being lost or ignored. Admittedly embedding the metadata will increase the filesize of the image, this is only a small increase and ensures the image and the metadata are kept together. The other thing you should know about XMP is that it is extensible allowing you to add custom parameters to suit your needs.

How can I use it?

With some creativity you can use metadata for a variety of different tasks. Below I have covered 2 common activities you may wish to perform and how they can be done using JDeli.

Writing Licence details to XMP metadata.
This first, most obvious and frequently used use of metadata is to assert the rights of the creator. Adding metadata to an image can allow you to add the creators details along with any license the image is under. In this way even if the image is transferred to others these details travel with it ensuring that, if needed, the images creator and licence can easily be checked. Adding XMP metadata to a Tiff file can be done with the following code.

try {
    String metaData = getMetaData();
    BufferedImage bi = getImage();
 
    File file = new File("C:\\output\\image.tiff");
    if(!file.exists()){
        file.mkdirs();
        file.createNewFile();
    }
    FileOutputStream fos = new FileOutputStream(file);
 
    TiffEncoder tiffEnc = new TiffEncoder();
    tiffEnc.setXMPMetaData(metaData);
    tiffEnc.write(bi, fos);
 
    fos.flush();
    fos.close();
 
} catch (FileNotFoundException ex) {
    ex.printStackTrace();
} catch (IOException ex) {
    ex.printStackTrace();
}

Sorting images based on XMP metadata.
This is something many image processing and storing softwares already do but is incredibly useful. Metadata can be used to help sort images into different albums and folders. Many cameras will add metadata to images when they are taken. This can include date, time and location to name just a few. This data can then be used with some software to sort the images based on this data. Getting the meta data to be used in sorting can be done with the following code.

try {
    File file = new File("C:\\output\\image.tiff");
    if(file.exists()){
        FileInputStream fis = new FileInputStream(file);
        byte[] data = new byte[fis.available()];
        fis.read(data);
        
        TiffDecoder tiffDec = new TiffDecoder(data);
        
        //Get XMP as string
        String xmp = tiffDec.getXMPMetaData();
        
        //Convert to Document for easy searching through the XMP
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xmp));
        Document doc = db.parse(is);
        
        fis.close();
    }
} catch (Exception ex) {
    ex.printStackTrace();
}

Why use JDeli to read and write Images in Java?

JDeli offers a range of advantages over ImageIO and alternatives, including:

  • prevent heap related JVM crashes
  • support for additional image formats such as Heic
  • reduce output file size
  • improve read/write performance
  • create smaller files
  • control over output
  • support threading
  • superior image scaling algorithms


Our software libraries allow you to

Convert PDF to HTML in Java
Convert PDF Forms to HTML5 in Java
Convert PDF Documents to an image in Java
Work with PDF Documents in Java
Read and Write AVIF, HEIC, WEBP and other image formats
Kieran France Kieran France is a programmer for IDRSolutions in charge of there internal test suite. In his spare time he enjoys tinkering with gadgets and code.

One Reply to “What is XMP metadata and why it is useful”

  1. Hi Kieran,

    Interesting article.

    I was just wondering if social media (Facebook, Instagram etc.) are using META- data in their marketing? Without telling the users.

    Here i mean they get a lot of informations as you mention like GPS, date, camera etc. that could be used to marketing.

    Best regards,

    Frederik

Comments are closed.