Skip to main content

Why is my ByteArrayOutputStream not getting loaded by the ImageWriter?

2 replies [Last post]
mjc123
Offline
Joined: 2010-07-22

Can someone please put eyes on the following code and tell me what I am doing wrong here? I am trying to return a ByteArrayOutputStream, but it is not getting loaded as expected by the ImageWriter. The following method successfully generates a file when selected for output, but the ByteArrayOutputStream does not load for some reason. What am I missing?

Thanks in advance!

Begin code ------------------------------------------------------------------------------------------------
// Establish a null ByteArrayOutputStream reference
ByteArrayOutputStream out = null;
// Establish a null ImageInputStream reference
ImageInputStream iis = null;
// Establish a null ImageOuputStream reference
ImageOutputStream ios = null;
// Establish a null ImageReader reference
ImageReader reader = null;
// Establish a null ImageWriter reference
ImageWriter writer = null;

// Attempt to process the PNG file
try {
//------------------------------------------------------------------------------
// Set reader resources for PNG file input
//------------------------------------------------------------------------------
// Retrieve the available ImageIO readers for PNG file input
Iterator readers = ImageIO.getImageReadersByFormatName("png");
// Establish an ImageReader
reader = (ImageReader)readers.next();
// Set the ImageInputStream for the ImageReader
reader.setInput(ImageIO.createImageInputStream(new ByteArrayInputStream(pngBytes)));

//------------------------------------------------------------------------------
// Set writer resources for merged image output
//------------------------------------------------------------------------------
/*
// Establish a File object pointing to the output file
File f = new File("c:\\image\\output_2.png");
// Establish an ImageOutputStream to the output file
ios = ImageIO.createImageOutputStream(f);
*/

// Instantiate a new ByteArrayOutputStream
out = new ByteArrayOutputStream();
// Establish an ImageOutputStream for output
ios = ImageIO.createImageOutputStream(out);

// Retrieve the available ImageIO writers for PNG file output
Iterator writers = ImageIO.getImageWritersByFormatName("png");
// Establish an ImageWriter
writer = (ImageWriter)writers.next();
// Tie the ImageOutputStream to the ImageWriter
writer.setOutput(ios);

//------------------------------------------------------------------------------
// Set the desired parameter values for image writing
//------------------------------------------------------------------------------
// Establish an ImageWriteParam instance
ImageWriteParam param = writer.getDefaultWriteParam();

//------------------------------------------------------------------------------
// Read the TIF input and write the output
//------------------------------------------------------------------------------
// Read the background image into a BufferedImage
BufferedImage image = reader.read(0);
// Merge any overlays onto the BufferedImage
generateMergedImagePage(imager.getPageKeeper(0), image);
// Write the merged PNG file
writer.write(null, new IIOImage(image, null, null), param);

// The ImageOutputStream is loaded
System.out.println("ios-----> " + ios.length());
// The ByteArrayOutputStream is zero length
System.out.println("out-----> " + out.size());

writer.dispose();

return out.toByteArray();

}
catch (Exception e) {
// Throw the Exception
throw e;
}
catch (Error err) {
// Throw the Error
throw err;
}
finally {
// When an ImageReader has been established
if (reader != null) {
// Dispose the ImageReader
reader.dispose();
}
// When an ImageWriter has been established
if (writer != null) {
// Dispose the ImageWriter
writer.dispose();
}
// When an ImageInputStream has been established
if (iis != null) {
// Flush the ImageInputStream
iis.flush();
// Close the ImageInputStream
iis.close();
}
// When the ImageOutputStream has been established
if (ios != null) {
// Flush the ImageOutputStream
ios.flush();
// Close the ImageOuputStream
ios.close();
}
}
End code --------------------------------------------------------------------------------------------------

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
marcojacob
Offline
Joined: 2006-07-10

You can try to flush ios before checking the size of the
underlying ByteArrayOutputStream maybe dispose the
writer first.

You can write BufferedImage directly by writer.write(image)
which uses default metadata and params.

Kind regards,
Marco

mjc123
Offline
Joined: 2010-07-22

Thanks Marco that did the trick. Most appreciated indeed!