Alex Rivera | Logout

How to convert BufferedImage to InputStream?

Asked 2010-11-22T23:41:46.773
23

I am uploading images using servlet. To perform resize operations i am converting InputStream to BufferedImage. Now i want to save it in mongoDB. Since, i am new to mongoDB as far as i know, GridFS takes InputStream.

So, is there any way to convert BufferedImage to InputStream?

Edit
Report

2 Answers

13

First of all you must get your "bytes":

byte[] buffer = ((DataBufferByte)(bufferedImage).getRaster().getDataBuffer()).getData();

And then use ByteArrayInputStream(byte[] buf) constructor to create your InputStream;

answered 2010-11-22T23:54:39.617
12

You need to save the BufferedImage to a ByteArrayOutputStream using the ImageIO class, then create a ByteArrayInputStream from toByteArray().

answered 2010-11-22T23:51:18.740

Your Answer