CONTENTS | PREV | NEXT | JavaTM Image I/O API Guide |
ImageWriter
Class
Just as we obtained a list ofImageReader
s for a particular format using a method from theImageIO
class, we can obtain a list ofImageWriter
s:
Iterator writers = ImageIO.getImageWritersByFormatName("png"); ImageWriter writer = (ImageWriter)writers.next();
Once anImageWriter
has 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);
TheIIOImage
class 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 passnull
as the value for the metadata-related parameters.The
ImageWriter
class contains awrite
method that creates a new file from anIIOImage
, and awriteInsert
method that adds anIIOImage
to 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!"); }