private static boolean writeImage(BufferedImage image, String imageFormat, Object outputStream, int resolution)
throws IOException
{
boolean bSuccess = true;
ImageOutputStream output = null;
ImageWriter imageWriter = null;
try
{
output = ImageIO.createImageOutputStream( outputStream );
boolean foundWriter = false;
Iterator<ImageWriter> writerIter = ImageIO.getImageWritersByFormatName( imageFormat );
while( writerIter.hasNext() && !foundWriter )
{
try
{
imageWriter = (ImageWriter)writerIter.next();
ImageWriteParam writerParams = imageWriter.getDefaultWriteParam();
if( writerParams.canWriteCompressed() )
{
writerParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
// reset the compression type if overwritten by setCompressionMode
if (writerParams.getCompressionType() == null)
{
writerParams.setCompressionType(writerParams.getCompressionTypes()[0]);
}
writerParams.setCompressionQuality(1.0f);
}
IIOMetadata meta = createMetadata( image, imageWriter, writerParams, resolution);
imageWriter.setOutput( output );
imageWriter.write( null, new IIOImage( image, null, meta ), writerParams );
foundWriter = true;
}
catch( IIOException io )
{
throw new IOException( io.getMessage() );
}
finally
{
if( imageWriter != null )
{
imageWriter.dispose();
}
}
}
if( !foundWriter )
{
bSuccess = false;
}
}
finally
{
if( output != null )
{
output.flush();
output.close();
}
}
return bSuccess;
}