Write an image in PNG format to a file. - Java 2D Graphics

Java examples for 2D Graphics:PNG

Description

Write an image in PNG format to a file.

Demo Code


//package com.java2s;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOInvalidTreeException;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.metadata.IIOMetadataNode;
import javax.imageio.stream.ImageOutputStream;

public class Main {
    /** Write an image in PNG format to a file.
        @param metaData Optional PNG meta data (or null) */
    public static void writeImage(BufferedImage image, File file,
            Map<String, String> metaData) throws IOException {
        Iterator iter = ImageIO.getImageWritersBySuffix("png");
        ImageWriter writer = (ImageWriter) iter.next();
        IIOMetadata meta = null;/*from   ww w.j a va 2 s  .com*/
        if (metaData != null) {
            ImageTypeSpecifier specifier = new ImageTypeSpecifier(image);
            meta = writer.getDefaultImageMetadata(specifier, null);
            String formatName = "javax_imageio_1.0";
            org.w3c.dom.Node node = meta.getAsTree(formatName);
            for (Map.Entry<String, String> entry : metaData.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                addMeta(node, key, value);
            }
            try {
                meta.mergeTree(formatName, node);
            } catch (IIOInvalidTreeException e) {
                assert false;
                return;
            }
        }
        ImageOutputStream ios = ImageIO.createImageOutputStream(file);
        writer.setOutput(ios);
        try {
            writer.write(null, new IIOImage(image, null, meta), null);
        } catch (IllegalStateException e) {
            // ImageWriter on Linux Java 1.5 throws an  IllegalStateException
            // instead of an IOException, if it has no write permissions
            throw new IOException("Could not write to file " + file);
        }
    }

    private static void addMeta(org.w3c.dom.Node node, String keyword,
            String value) {
        IIOMetadataNode text = new IIOMetadataNode("Text");
        IIOMetadataNode textEntry = new IIOMetadataNode("TextEntry");
        textEntry.setAttribute("value", value);
        textEntry.setAttribute("keyword", keyword);
        textEntry.setAttribute("encoding", Locale.getDefault().toString());
        textEntry.setAttribute("language", "en");
        textEntry.setAttribute("compression", "none");
        text.appendChild(textEntry);
        node.appendChild(text);
    }
}

Related Tutorials