IntelliJ Java IDE
The moose likes Swing / AWT / SWT / JFace and the fly likes Want to create Image from byte [] from internet Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT / JFace
Reply Bookmark "Want to create Image from byte [] from internet" Watch "Want to create Image from byte [] from internet" New topic
Author

Want to create Image from byte [] from internet

James Hare
Greenhorn

Joined: Nov 08, 2001
Posts: 5

Hi folks, I'm trying to download an image from the internet and display it (in particular, when someone selects a ticker in a program, I try to download a yahoo stock graph gif that is dynamically created). My problem is, I can get back the byte [] for the gif from the connection, but is there some way I can change this dirrectly into an image besides storing it in a temp file first???
Note where I save to byte array then save to file then re-open file. I'd like to take it straight to an Image object if possible.
Here's my code:
<code>
import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import com.boa.common.util.ImagePanel;
public class URLReader
{
public static void main(String[] args) throws Exception
{
URL yahoo = new URL("http://ichart.yahoo.com/v?s=MSFT");
// i'm writing to temp file because not sure what I can do!
FileOutputStream fileOut = new FileOutputStream("Test.gif");
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
int bytesRead = 0;
// read input stream from url
InputStream is = yahoo.openStream();
byte [] inputBytes = new byte[8192];
// read bytes out of the socket and into our byte array
while ((bytesRead = is.read(inputBytes)) > 0) {
System.out.println("read in " + bytesRead + " bytes");
// copy the contents of our byte array into the
// ByteArrayOutputStream's internal array
bytesOut.write(inputBytes, 0, bytesRead);
}
// write bytes to file
bytesOut.writeTo(fileOut);
fileOut.close();
bytesOut.close();
is.close();
// create new frame to show image
JFrame frame = new JFrame();
frame.setSize(400,400);
frame.show();
// load image
MediaTracker mt = new MediaTracker(frame);
graphImage = Toolkit.getDefaultToolkit().getImage(URLReader.class.getResource("test.gif"));
mt.addImage(graphImage, 0);
mt.waitForAll();
// a class which displays image centered in a panel
ImagePanel imPanel = new ImagePanel(graphImage);
// add to frame
frame.getContentPane().add(imPanel);
}
}
</code>


=========================<BR>Jim Hare<BR>Bank of America<BR>james.m.hare@bankofamerica.com
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
You should be able to simply use an Image object instead. It has a constructor that takes a URL object iirc. Check the API docs for more information.
Layne


Java API Documentation
The Java Tutorial
James Hare
Greenhorn

Joined: Nov 08, 2001
Posts: 5
Will that work through a proxy?
James Hare
Greenhorn

Joined: Nov 08, 2001
Posts: 5
That did work, sorry. I figured out what my real problem was. My old code that did:
<code>
Image graphImage = null;
MediaTracker mt = new MediaTracker(frame);
graphImage = Toolkit.getDefaultToolkit().getImage("http://ichart.yahoo.com/v?s=MSFT");
mt.addImage(graphImage, 0);
mt.waitForAll();
</code>
Actually DID work before, but the image was not painting on the panel until I resized it. So I was working around a perceived bug in the image, when it was really the drawing of the image in my custom panel that was failing *smacks head*
Sorry to bother you all. I shall go hang my head in shame now...
 
 
subject: Want to create Image from byte [] from internet
 
Threads others viewed
how to compile this type of code..
Display byte array of PDF in browser
Retrive Images in Binary from Database and write to a gif or jpg
Display PDF in IE from byte array using Servlet
Using Apache Commons Codec
developer file tools