| CONTENTS | PREV | NEXT | JavaTM Image I/O API Guide |
ImageWriter Class
Just as we obtained a list ofImageReaders for a particular format using a method from theImageIOclass, we can obtain a list ofImageWriters:
Iterator writers = ImageIO.getImageWritersByFormatName("png");
ImageWriter writer = (ImageWriter)writers.next();
Once anImageWriterhas been obtained, its destination must be set to anImageOutputStream:
File f = new File("c:\images\myimage.png");
ImageOutputStream ios = ImageIO.createImageOutputStream(f);
writer.setOutput(ios);
Finally, the image may be written to the output stream:
BufferedImage bi; writer.write(bi);
TheIIOImageclass is use to store references to an image, one or more thumbnail images, and metadata (non-image data) associated with the image. Metadata will be discussed below; for now, we will simply passnullas the value for the metadata-related parameters.The
ImageWriterclass contains awritemethod that creates a new file from anIIOImage, and awriteInsertmethod that adds anIIOImageto an existing file . By calling the two in sequence a multi-image file may be written:
BufferedImage first_bi, second_bi;
IIOImage first_IIOImage = new IIOImage(first_bi, null, null);
IIOImage second_IIOImage = new IIOImage(second_bi, null, null);
writer.write(null, first_IIOImage, null);
if (writer.canInsertImage(1)) {
writer.writeInsert(1, second_IIOImage, null);
} else {
System.err.println("Writer can't append a second image!");
}