Link to home
Start Free TrialLog in
Avatar of lomidien
lomidien

asked on

BufferedImage to InputStream

Ok, I was using the following code to read an image file into an InputStream:

InputStream f=new FileInputStream(filename);

I would now like to take a BufferedImage which is dynamically generated and feed it to the InputStream.  The i/o classes and all the decorators used therein give me all sorts of trouble.....I know it seems like a dumb question, but i/o != fun.  :(

Thanks,
David
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Use the ImageIO class
> I would now like to take a BufferedImage which is dynamically generated and feed it to the InputStream.  

That does not seem to make sense, what exactly is it you are trying to achieve?
following shows you how to save a BI to a file if thats what you nned:

http://www.javaalmanac.com/egs/javax.imageio/Graphic2File.html
Avatar of lomidien
lomidien

ASKER

Looking at the ImageIO JavaDoc, I can see where I can read a InputStream and return a BufferedImage.  I already have a BufferedImage and need to return a InputStream is my problem.  Am I overlooking what you intend?

BTW, how on earth do you get to the posts so quickly??????

Thanks,
David
>  I already have a BufferedImage and need to return a InputStream is my problem.

What do want the input stream for?
I'm actually sending the data over a socket......a formatted response to a web request specifically.  The image itself is generated dynamically and not read from a file which is why I'm trying to figure out how to turn the BufferedImage directly into a InputStream.

Thanks,
David
I still don't see what you need an InputStream.
If you want to write it to somewhere then use ImageIO.write() with the output stream you want to write it to
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'll try posting the relevent code:

//generating screenshot
BufferedImage bufferedImage = robot.createScreenCapture(area);

//grabbing output
PrintStream out=new PrintStream(new BufferedOutputStream(socket.getOutputStream()));

//setting mime type
String mimeType="image/jpeg";
       
//returning ok message and mime type to browser
out.print("HTTP/1.0 200 OK\r\n" + "Content-type: "+mimeType+"\r\n\r\n");

//send data to client
//***********IF I'M SENDING DATA FROM A FILE, I USE THE FOLLOWING CODE
byte[] a=new byte[4096];
int n;
while ((n=f.read(a))>0)
     out.write(a, 0, n);
out.close();
//************END SENDING


The last section is what I need to change.  I would like to send the BufferedImage I created at the beginning of that snippet

Thanks,
David
use the line I posted above, but you need to write the image to the sockets output stream and *not* the PrintWriter (thats for text data)
Whoa, whoa, whoa.  You're right....I didn't see the logic in it at first and I'm glad you didn't just spell it out completely cause it made me investigate a little bit more.

Lost my connection before I could get back to post this.

Thanks,
David