# Imports, image metadata - adding text to png

**URL:** <https://discourse.processing.org/t/imports-image-metadata-adding-text-to-png/1025>\
**Category:** Coding Questions\
**Created:** [June 17, 2018, 12:12pm UTC](https://discourse.processing.org/t/imports-image-metadata-adding-text-to-png/1025 "2018-06-17T12:12:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmccandless](https://avatars.discourse-cdn.com/v4/letter/l/f0a364/32.png) [@lmccandless](https://discourse.processing.org/u/lmccandless)\
**Post date:** [June 17, 2018, 12:12pm UTC](https://discourse.processing.org/t/imports-image-metadata-adding-text-to-png/1025/1 "2018-06-17T12:12:36Z")

</div>

I’m trying to add text metadata to a png, I found java code to do it on stackoverflow. I’m trying to get it to work in processing, but I can’t seem to find the right imports for some these classes the code uses

```auto
import javax.imageio.*;
import javax.imageio.stream;
import javax.imageio.metadata;
import java.util.*;
import org.w3c.dom;

void setup(){
}

    void main2(final String[] args) throws IOException {
        File in = new File(args[0]);
        File out = new File(in.getParent(), createOutputName(in));

        System.out.println("Output path: " + out.getAbsolutePath());

        try (ImageInputStream input = ImageIO.createImageInputStream(in);
             ImageOutputStream output = ImageIO.createImageOutputStream(out)) {

            Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
            ImageReader reader = readers.next(); // TODO: Validate that there are readers

            reader.setInput(input);
            IIOImage image = reader.readAll(0, null);

            addTextEntry(image.getMetadata(), "foo", "bar");

            ImageWriter writer = ImageIO.getImageWriter(reader); // TODO: Validate that there are writers
            writer.setOutput(output);
            writer.write(image);
        }

        try (ImageInputStream input = ImageIO.createImageInputStream(out)) {
            Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
            ImageReader reader = readers.next(); // TODO: Validate that there are readers

            reader.setInput(input);
            String value = getTextEntry(reader.getImageMetadata(0), "foo");

            System.out.println("value: " + value);
        }
    }

    String createOutputName(final File file) {
        String name = file.getName();
        int dotIndex = name.lastIndexOf('.');

        String baseName = name.substring(0, dotIndex);
        String extension = name.substring(dotIndex);

        return baseName + "_copy" + extension;
    }

     void addTextEntry(final IIOMetadata metadata, final String key, final String value) throws IIOInvalidTreeException {
        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(IIOMetadataFormatImpl.standardMetadataFormatName);
        root.appendChild(text);

        metadata.mergeTree(IIOMetadataFormatImpl.standardMetadataFormatName, root);
    }

    String getTextEntry(final IIOMetadata metadata, final String key) {
        IIOMetadataNode root = (IIOMetadataNode) metadata.getAsTree(IIOMetadataFormatImpl.standardMetadataFormatName);
        NodeList entries = root.getElementsByTagName("TextEntry");

        for (int i = 0; i < entries.getLength(); i++) {
            IIOMetadataNode node = (IIOMetadataNode) entries.item(i);
            if (node.getAttribute("keyword").equals(key)) {
                return node.getAttribute("value");
            }
        }

        return null;
    }

```

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [June 17, 2018, 2:17pm UTC](https://discourse.processing.org/t/imports-image-metadata-adding-text-to-png/1025/2 "2018-06-17T14:17:34Z")

</div>

> [@lmccandless](#):
>
> try (ImageInputStream input = ImageIO.createImageInputStream(in);

Fix the try block. It should be open curly brackets, not parenthesis. It should fix your problem.

Kf
