19

I would like to write metadata to a PNG image that I create.

My understanding of Java Advanced Image API is that I should use IIOMetadata, but code snippets I found seem overly complicated. Then I searched for a library and found Sanselan but it seems a bit old, and not very handy for writing metadata.

To actually create the image, I use

ImageIO.write(image, "png", baos);

I understand image metadatas are complex to handle due to its XML-like structure. Could anybody point me to a tutorial, solution, or library that will help?

Paul Wintz
  • 2,542
  • 1
  • 19
  • 33
Alexis Laporte
  • 515
  • 1
  • 4
  • 14
  • 1
    How did you end up getting this to work? Can you share your code, or answer this question: http://stackoverflow.com/questions/24714345/unable-to-read-write-image-metadata-in-java – Ali Jul 12 '14 at 15:29
  • Using [this](https://github.com/dragon66/icafe) library, it is as easy as PNGTweaker.insertTextChunk(ChunkType.TEXT, "keyword", "value", is, os) – dragon66 Mar 30 '16 at 05:22

4 Answers4

30

I had to do the the same thing some days ago.. I have not found the exact solution on the internet either but looking at the com.sun.imageio.plugins.png.PNGMetadata class I could achieve some results..

To write a custom metadata to a PNG file:

public byte[] writeCustomData(BufferedImage buffImg, String key, String value) throws Exception {
    ImageWriter writer = ImageIO.getImageWritersByFormatName("png").next();

    ImageWriteParam writeParam = writer.getDefaultWriteParam();
    ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);

    //adding metadata
    IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);

    IIOMetadataNode textEntry = new IIOMetadataNode("tEXtEntry");
    textEntry.setAttribute("keyword", key);
    textEntry.setAttribute("value", value);

    IIOMetadataNode text = new IIOMetadataNode("tEXt");
    text.appendChild(textEntry);

    IIOMetadataNode root = new IIOMetadataNode("javax_imageio_png_1.0");
    root.appendChild(text);

    metadata.mergeTree("javax_imageio_png_1.0", root);

    //writing the data
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream stream = ImageIO.createImageOutputStream(baos);
    writer.setOutput(stream);
    writer.write(metadata, new IIOImage(buffImg, null, metadata), writeParam);
    stream.close();

    return baos.toByteArray();
}

Then, to read the data:

public String readCustomData(byte[] imageData, String key) throws IOException{
    ImageReader imageReader = ImageIO.getImageReadersByFormatName("png").next();

    imageReader.setInput(ImageIO.createImageInputStream(new ByteArrayInputStream(imageData)), true);

    // read metadata of first image
    IIOMetadata metadata = imageReader.getImageMetadata(0);

    //this cast helps getting the contents
    PNGMetadata pngmeta = (PNGMetadata) metadata; 
    NodeList childNodes = pngmeta.getStandardTextNode().getChildNodes();

    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        String keyword = node.getAttributes().getNamedItem("keyword").getNodeValue();
        String value = node.getAttributes().getNamedItem("value").getNodeValue();
        if(key.equals(keyword)){
            return value;
        }
    }
    return null;
}
Rogel Garcia
  • 1,895
  • 14
  • 16
1

Java provides the metadata package and the ImageWriter class along with the ImageIO package.

You create your IIOMetadata object, then getImageWriters for your BufferedImage or IIOImage and use them to write the metadata.

Paul Wintz
  • 2,542
  • 1
  • 19
  • 33
c00kiemon5ter
  • 16,994
  • 7
  • 46
  • 48
0

Using the method from posted by the OP gets most of the way there; the only issue is that PNGMetadata is proprietary and so causes compiler warnings.

There is a method of doing it without using the proprietary API, by searching the metadata tree for tEXtEntry nodes:

private List<Node> findNodesWithName(String name, Node root) {
    List<Node> found = new ArrayList<>();
    Node n = root.getFirstChild();
    while (n != null) {
        if (n.getNodeName().equals(name)) {
            found.add(n);
        }
        found.addAll(findNodesWithName(name, n));
        n = n.getNextSibling();
    }
}

// ...
// To use it:
IIOMetadata metadata = ...;
List<Node> tEXtNodes = findNodesWithName(
        "tEXtEntry",
        metadata.getAsTree(metadata.getNativeMetadataFormatName()));

for (Node n : tEXtNodes) {
    String keyword = node.getAttributes().getNamedItem("keyword");
    String value = node.getAttributes().getNamedItem("value");
    System.out.println("keyword: " + keyword + "; value: " + value);
}
wakjah
  • 4,541
  • 1
  • 18
  • 23
0

To add to other answer, you can also try the PNGJ library, it has full metadata support.

BTW, I don't understand what you are refering to with the "XML-like" structure of metadata.

Thomas
  • 354
  • 1
  • 3
  • 13
leonbloy
  • 73,180
  • 20
  • 142
  • 190
  • looks very promising, I will give it a try. Thanks ! – Alexis Laporte Jun 28 '11 at 07:38
  • "Metadata may contain complex, hierarchical structures. The Java XML Document Object Model (DOM) API is used to represent these structures..." https://docs.oracle.com/javase/8/docs/technotes/guides/imageio/spec/apps.fm5.html – abyrd Aug 04 '23 at 09:22