Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6254
  • Last Modified:

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
0
lomidien
Asked:
lomidien
  • 8
  • 4
1 Solution
 
objectsCommented:
Use the ImageIO class
0
 
objectsCommented:
> 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?
0
 
objectsCommented:
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
lomidienAuthor Commented:
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
0
 
objectsCommented:
>  I already have a BufferedImage and need to return a InputStream is my problem.

What do want the input stream for?
0
 
lomidienAuthor Commented:
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
0
 
objectsCommented:
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
0
 
objectsCommented:
ImageIO.write(bi, format, out);
0
 
lomidienAuthor Commented:
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
0
 
objectsCommented:
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)
0
 
lomidienAuthor Commented:
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
0
 
objectsCommented:
:)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now