get Java Image Writer - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage

Description

get Java Image Writer

Demo Code


//package com.java2s;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;

import javax.imageio.ImageWriter;

public class Main {
    public static ImageWriter getJavaImageWriter(boolean preferBetter) {
        ImageWriter imwriter = null;
        // com.sun.media.imageioimpl.plugins.png.CLibPNGImageWriter (better)
        // com.sun.imageio.plugins.png.PNGImageWriter
        List<ImageWriter> list = new ArrayList<ImageWriter>();
        for (Iterator<ImageWriter> iter = ImageIO
                .getImageWritersByFormatName("PNG"); iter.hasNext();) {
            list.add(iter.next());//from  ww  w . j a v  a 2  s.  com
        }

        for (ImageWriter iw : list) {
            String cname = iw.getOriginatingProvider().getPluginClassName();
            if (cname.startsWith("com.sun.media.imageio") && preferBetter) {
                imwriter = iw;
                break;
            }
            if (cname.startsWith("com.sun.imageio.plugins.png.")
                    && !preferBetter) {
                imwriter = iw;
                break;
            }
        }
        if (imwriter == null)
            imwriter = list.get(0); // whatever

        return imwriter;
    }
}

Related Tutorials