?
Solved

BufferedImage to InputStream

Posted on 2005-03-01
12
Medium Priority
?
6,003 Views
Last Modified: 2008-01-09
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
Comment
Question by:lomidien
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
12 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13435139
Use the ImageIO class
0
 
LVL 92

Expert Comment

by:objects
ID: 13435143
> 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
 
LVL 92

Expert Comment

by:objects
ID: 13435149
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:lomidien
ID: 13435175
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
 
LVL 92

Expert Comment

by:objects
ID: 13435190
>  I already have a BufferedImage and need to return a InputStream is my problem.

What do want the input stream for?
0
 

Author Comment

by:lomidien
ID: 13435193
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
 
LVL 92

Expert Comment

by:objects
ID: 13435220
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
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 13435265
ImageIO.write(bi, format, out);
0
 

Author Comment

by:lomidien
ID: 13435285
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
 
LVL 92

Expert Comment

by:objects
ID: 13435353
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
 

Author Comment

by:lomidien
ID: 13435375
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
 
LVL 92

Expert Comment

by:objects
ID: 13435415
:)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question