Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an InputStream from a BufferedImage?

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.

like image 382
fromvega Avatar asked Mar 16 '09 03:03

fromvega


1 Answers

From http://usna86-techbits.blogspot.com/2010/01/inputstream-from-url-bufferedimage.html

It works very fine!

Here is how you can make an InputStream for a BufferedImage:

URL url = new URL("http://www.google.com/intl/en_ALL/images/logo.gif"); BufferedImage image = ImageIO.read(url); ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(image, "gif", os); InputStream is = new ByteArrayInputStream(os.toByteArray()); 
like image 172
Felipe Avatar answered Sep 19 '22 06:09

Felipe