Example usage for javax.imageio ImageWriter canInsertImage

List of usage examples for javax.imageio ImageWriter canInsertImage

Introduction

In this page you can find the example usage for javax.imageio ImageWriter canInsertImage.

Prototype

public boolean canInsertImage(int imageIndex) throws IOException 

Source Link

Document

Returns true if the writer supports the insertion of a new image at the given index.

Usage

From source file:ImageIOTest.java

/**
 * Save the current image in a file//from w w w .  j  a v  a 2s .  c o  m
 * @param formatName the file format
 */
public void saveFile(final String formatName) {
    if (images == null)
        return;
    Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName(formatName);
    ImageWriter writer = iter.next();
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("."));
    String[] extensions = writer.getOriginatingProvider().getFileSuffixes();
    chooser.setFileFilter(new FileNameExtensionFilter("Image files", extensions));

    int r = chooser.showSaveDialog(this);
    if (r != JFileChooser.APPROVE_OPTION)
        return;
    File f = chooser.getSelectedFile();
    try {
        ImageOutputStream imageOut = ImageIO.createImageOutputStream(f);
        writer.setOutput(imageOut);

        writer.write(new IIOImage(images[0], null, null));
        for (int i = 1; i < images.length; i++) {
            IIOImage iioImage = new IIOImage(images[i], null, null);
            if (writer.canInsertImage(i))
                writer.writeInsert(i, iioImage, null);
        }
    } catch (IOException e) {
        JOptionPane.showMessageDialog(this, e);
    }
}