Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a image from an OutputStream

Status
Not open for further replies.

wangdong

Programmer
Oct 28, 2004
202
CN
Hi, I want to have a method to create a image file from an outputStream, something likes?

void createImage(OutputStream out, String filename){}

Anyone knows how to do this?

Thanks in advance.

Dong Wang


Chinese Java Faq Forum
 
I'd expect an OutputStream to be an object to write to, not to read from. Any background for this?

Cheers,
Dian
 
no, the OutputStream is what you are going to read the image data.

and it's return from
Code:
ByteArrayOutputStream bout = new ByteArrayOutputStream();
// write buffered image to output stream
ImageIO.write(bufferedImage, "jpg", bout);
return bout;
bout becomes the OutputStream of this new method.

Chinese Java Faq Forum
 
OK, firstly you cannot "read from an output stream", unless you are a networked client - at which point your outputstream is their input stream.

Having said that, ByteArrayOutputStream is a special case, because you can call :

byte[] data = bout.toByteArray()

which gives you your image in the form of byte data.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Can I cast the OutputStream object to ByteArrayOutputStream,
byte []data = ((ByteArrayOutputStream)outputStream).toByteArray();

If so, what is the next thing to do?

Thank you,

Chinese Java Faq Forum
 
Yeah, nice, but is the OutputStream generated by some kind of charm or comes from antoher place? :p

Cheers,
Dian
 
No you can't just cast it - ByteArrayOutputStream is a special case - because it is not really an output stream at all, in the sense of a networked stream.


--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Sorry guys, I am still confused. Let me make the question more clear.

I have a method to generate a jpg with a specific string, but return an outputstream.

Code:
public OutputStream convert(InputStream input) throws IOException {
	// create image buffer
	BufferedImage bi = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT,
	BufferedImage.TYPE_INT_BGR);
	Graphics2D g = bi.createGraphics();
	// set canvas color
	g.setColor(Color.LIGHT_GRAY);
	// creat canvas for drawing text
	g.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
	// set font color
	g.setColor(Color.BLUE);

	BufferedReader br = new BufferedReader(new StringReader(input.toString()));
	String oneline;
	
	// read line by line
	int y = 20; //line y coordinator
	while ((oneline = br.readLine()) != null) {
		g.drawString(oneline, 20, y = y + 20);
	}
	
	ByteArrayOutputStream bout = new ByteArrayOutputStream();
	// write buffered image to output stream
	ImageIO.write(bi, "jpg", bout);
	return bout;
}

To be able to see the generated picture, I have to write outputstream into a jpg file, which I am struggling with.

Any idea?

Thank you for all of your help!

Dong Wang

Chinese Java Faq Forum
 
Code:
ImageIO.write(bi, "jpg", bout);
byte[] data = bout.toByteArray();
FileOutputStream fos = new FileOutputStream("image.jpg");
fos.write(data);
fos.flush();
fos.close();

or even more easily :

Code:
FileOutputStream fos = new FileOutputStream("image.jpg");
ImageIO.write(bi, "jpg", fos);
fos.flush();
fos.close();

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Odd - it should work OK.

Anyhow, looking at the ImageIO class, there seems to be an even easier way :

Code:
ImageIO.write(bi, "jpg", new File("image.jpg"));

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top