Originally posted by James Hodgkiss:
It's a jpg image.
Then you don't want to be messing with
ImageProducer or
MemoryImageSource. Those aren't designed to handle encodings such as JPEG, PNG, or GIF.
I am using standard http code to connect to the image url (http://www...../image.jpg) which returns the image data as a String, called httpContent. I then convert this String to imageDataByteArray with httpContent.getBytes[].
How exactly does the binary data coming over the connection get converted to a String of 16-bit unicode chars? I guess if it's done correctly
String.getBytes() might put it back again. But even if so, it seems strange from me to convert to a unicode String and back.
I would think that reading the data directly into a byte[] array would be preferable or, even better, pass the unread network InputStream to the image-parsing code directly.
As I say, the problem is converting imageDataByteArray back to an Image.
I've also tried converting it using
Image image = Toolkit.getDefaultToolkit().createImage(imageDataByteArray)
- but the image does not display correctly.
Well if the data hasn't been corrupted (have you checked?) then
Toolkit.createImage(byte[]) should work. To what extent does it "not display correctly"? Do you get anything viewable at all?
The other way to convert a byte[] array in JPEG format to an image you can use is to wrap it in a
ByteArrayInputStream and pass it to
ImageIO.read(InputStream).
If you really have a URL object, then it should be easier to dispense with the byte[] array and just call
ImageIO.read(URL) or
Toolkit.createImage(URL).