Java Code Examples for javax.imageio.ImageIO.getImageWritersBySuffix()

The following are Jave code examples for showing how to use getImageWritersBySuffix() of the javax.imageio.ImageIO class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: incubator-netbeans   File: ScreenshotComponent.java   View Source Code Vote up 6 votes
void save(File file) throws IOException {
    ImageWriter iw = null;
    String name = file.getName();
    int i = name.lastIndexOf('.');
    if (i >= 0) {
        String extension = name.substring(i + 1);
        Iterator<ImageWriter> imageWritersBySuffix = ImageIO.getImageWritersBySuffix(extension);
        if (imageWritersBySuffix.hasNext()) {
            iw = imageWritersBySuffix.next();
        }
    }
    if (iw != null) {
        file.delete();
        ImageOutputStream ios = ImageIO.createImageOutputStream(file);
        iw.setOutput(ios);
        try {
            iw.write((BufferedImage) image);
        } finally {
            iw.dispose();
            ios.flush();
            ios.close();
        }
    } else {
        ImageIO.write((BufferedImage) image, "PNG", file);
    }
}
 
Example 2
Project: openjdk-jdk10   File: GetReaderWriterInfo.java   View Source Code Vote up 5 votes
private static void testGetWriterFileSuffixes() {
    String[] suffixes = ImageIO.getWriterFileSuffixes();
    for (String s : suffixes) {
        Iterator<ImageWriter> it = ImageIO.getImageWritersBySuffix(s);
        if (!it.hasNext()) {
            throw new RuntimeException("getWriterFileSuffixes returned " +
                                       "an unknown suffix: " + s);
        }
    }
}
 
Example 3
Project: openjdk-jdk10   File: ImageWriterCompressionTest.java   View Source Code Vote up 4 votes
public static void main(String[] args) {
    Locale.setDefault(Locale.US);

    final BufferedImage image
        = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB);

    final Graphics2D g2d = image.createGraphics();
    try {
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
                             RenderingHints.VALUE_RENDER_QUALITY);
        g2d.scale(2.0, 2.0);

        g2d.setColor(Color.red);
        g2d.draw(new Rectangle2D.Float(10, 10, 100, 100));
        g2d.setColor(Color.blue);
        g2d.fill(new Rectangle2D.Float(12, 12, 98, 98));
        g2d.setColor(Color.green);
        g2d.setFont(new Font(Font.SERIF, Font.BOLD, 14));

        for (int i = 0; i < 15; i++) {
            g2d.drawString("Testing Compression ...", 20, 20 + i * 16);
        }

        final String[] fileSuffixes = ImageIO.getWriterFileSuffixes();

        final Set<String> testedWriterClasses = new HashSet<String>();

        for (String suffix : fileSuffixes) {

            if (!IGNORE_FILE_SUFFIXES.contains(suffix)) {
                final Iterator<ImageWriter> itWriters
                    = ImageIO.getImageWritersBySuffix(suffix);

                final ImageWriter writer;
                final ImageWriteParam writerParams;

                if (itWriters.hasNext()) {
                    writer = itWriters.next();

                    if (testedWriterClasses.add(writer.getClass().getName())) {
                        writerParams = writer.getDefaultWriteParam();

                        if (writerParams.canWriteCompressed()) {
                            testCompression(image, writer, writerParams, suffix);
                        }
                    }
                } else {
                    throw new RuntimeException("Unable to get writer !");
                }
            }
        }
    } catch (IOException ioe) {
        throw new RuntimeException("IO failure", ioe);
    }
    finally {
        g2d.dispose();
    }
}