views:

2829

answers:

1

How can I get an InputStream from a BufferedImage object? I tried this but ImageIO.createImageInputStream() always returns NULL

BufferedImage bigImage = GraphicsUtilities.createThumbnail(ImageIO.read(file), 300);
ImageInputStream bigInputStream = ImageIO.createImageInputStream(bigImage);

The image thumbnail is being correctly generated since I can paint bigImage to a JPanel with success.

Thank you.

+1  A: 

If you are trying to save the image to a file try:

ImageIO.write(thumb, "jpeg", new File(....));

If you just want at the bytes try doing the write call but pass it a ByteArrayOutputStream which you can then get the byte array out of and do with it what you want.

TofuBeer
Thanks, writing to ByteArrayOutputStream and then converting it to a ByteArrayInputStream did the job. But I'm wondering why ImageIO.createImageInputStream() didn't work.
fromvega
It looks like that expects the Object passed in to be a File (or perhaps other things). I haven't used the API before... and not sure what it is you are trying to do :-) (consider it a lucky guess on my part that what I gave you worked :-)
TofuBeer
I'm trying to upload the image to a server, that's why I want to create an InputStream from it.
fromvega
Ah, look at http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageIO.html#createImageInputStream(java.lang.Object) input - an Object to be used as an input source, such as a File, readable RandomAccessFile, or InputStream.
TofuBeer
Thanks again. Just out of curiosity, where are you in Canada?
fromvega
Near Vancouver, BC
TofuBeer