• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to turn a byte[] into an image?

 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My app downloads image data from a server, storing the data in a byte[] called imageDataByteArray.

I want to use imageDataByteArray to create an image. For this, I have tried using:

int w = imageWidth; //488
int h = imageHeight; //245
int imageOffset = 0;
int scan = w;
Image image = component.createImage(new MemoryImageSource(w, h, java.awt.image.ColorModel.getRGBdefault(), imageDataByteArray, imageOffset, scan));

...but the image created is empty.

I think the length of imageDataByteArray is a problem - it's length is only 21461, whereas the number of pixels in the image ( = w * h) is 119560.

I've played around for a while, but cannot solve. Can anyone help?
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What kind of data is being sent over in the byte[]? Pixel-by-pixel color values? An image file being read off the disk? A BLOB/CLOB from a database?
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hiya Nate,

It's a jpg image. 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[].

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.

Do you know where I'm going wrong?

Cheers,
James
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're just trying to get the image and there's not any specific reason you're treating this as byte[] - you can load an image from a URL like this -

 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've got it sorted now. Doing byte[] -> String -> byte[] seemed to be the problem (along with several hours wasted working on the wrong branch of my http code!...)

The reason for the restriction is that the code of my main class has to compile for MIDP2.0 as well, so the only way to create the image with a byte[] or InputStream.

Many thanks for all the help.
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic