What is XMP metadata and why it is useful

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();
}

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (TwitterFacebook and Google+) or the  Blog RSS.

Related Posts:

The following two tabs change content below.
Kieran France is a programmer for IDRSolutions. He enjoys tinkering with most things including gadgets, code and electronics. He often has no idea what to write in his blog posts but tries his hardest to make them interesting and entertaining, he also makes no excuses for his odd sense of humor.
KieranF

About Kieran France

Kieran France is a programmer for IDRSolutions. He enjoys tinkering with most things including gadgets, code and electronics. He often has no idea what to write in his blog posts but tries his hardest to make them interesting and entertaining, he also makes no excuses for his odd sense of humor.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>