Read PNG 'file' and extract its comment string. - Java 2D Graphics

Java examples for 2D Graphics:Image File

Description

Read PNG 'file' and extract its comment string.

Demo Code


//package com.java2s;

import java.io.File;

import java.util.Iterator;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;

import javax.imageio.metadata.IIOMetadata;

import javax.imageio.stream.FileImageInputStream;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class Main {
    /** The name of the root node of the "native" metadata XML format
     * for PNG images in the 'imageio' library. */
    public static final String pngMetadataFormatName = "javax_imageio_png_1.0";

    /** Read PNG 'file' and extract its comment string.  If the file
     * is a valid PNG but has no comment, return null.  Otherwise,
     * throw an exception. *///from   w w  w  . j  av  a  2 s.  com
    public static String getPNGComment(File file) throws Exception {
        // Get a reader than can read PNG.
        ImageReader reader;
        {
            Iterator<ImageReader> readers = ImageIO
                    .getImageReadersByFormatName("png");
            if (!readers.hasNext()) {
                throw new RuntimeException(
                        "Unable to read PNG image because there is no registered "
                                + "image reader for that format.  That might mean the Java "
                                + "run time library is incomplete somehow.");
            }
            reader = readers.next();
        }

        try {
            // Prepare to read the file.
            FileImageInputStream fiis = new FileImageInputStream(file);
            try {
                reader.setInput(fiis);
                ImageReadParam irp = reader.getDefaultReadParam();

                // Read it, including metadata, which is where comment is.
                IIOImage iioImage = reader.readAll(reader.getMinIndex(),
                        irp);
                IIOMetadata metadata = iioImage.getMetadata();

                // Traverse metadata tree to find a comment.
                Node tree = metadata.getAsTree(pngMetadataFormatName);
                for (Node elem = tree.getFirstChild(); elem != null; elem = elem
                        .getNextSibling()) {
                    String name = elem.getNodeName();

                    // For now, I only handle 'zTXt' because that is what I am
                    // using to write, since 'compression' is always true.
                    if (name.equals("zTXt")) {
                        for (Node entry = elem.getFirstChild(); entry != null; entry = entry
                                .getNextSibling()) {
                            if (entry.getNodeName().equals("zTXtEntry")) {
                                NamedNodeMap nnm = entry.getAttributes();

                                Node keyword = nnm.getNamedItem("keyword");
                                Node text = nnm.getNamedItem("text");
                                if (keyword != null
                                        && text != null
                                        && keyword.getNodeValue().equals(
                                                "Comment")) {
                                    return text.getNodeValue();
                                }
                            }
                        }
                    }
                }

                return null;
            } finally {
                fiis.close();
            }
        } finally {
            reader.dispose();
        }
    }
}

Related Tutorials