get Java Image Reader - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage

Description

get Java Image Reader

Demo Code


//package com.java2s;

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

public class Main {
    public static ImageReader getJavaImageReader(boolean preferBetter) {
        ImageReader imwriter = null;
        // com.sun.media.imageioimpl.plugins.png.CLibPNGImageWriter (better)
        // com.sun.imageio.plugins.png.PNGImageWriter
        List<ImageReader> list = new ArrayList<ImageReader>();
        for (Iterator<ImageReader> iter = ImageIO
                .getImageReadersByFormatName("PNG"); iter.hasNext();) {
            list.add(iter.next());//from w  w  w  .  j ava2  s . co  m
        }

        for (ImageReader 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);

        return imwriter;
    }
}

Related Tutorials