Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-13-2007, 01:04 PM   #1
Registered: Aug 2005
Posts: 39

Rep: Reputation: 15
Converting Image to Byte Array (Java)


I am looking for a way to convert an Image to a byte array. So that I may be able to send the byte array to a J2ME client. Basically it will work like this; Mappoint generates an Image from a given Route, than this Image is sent to the Webservice and the Webservice will send it to J2ME client.

The trouble I am experiencing now is after retrieving the Image from Mappoint, is converting the Image to a byte array.

I am using the following code, but it's obvious this doensn't work.

Image ima = mappoint.retrieveImage(Route);

BufferedImage bu = new BufferedImage(ima.getWidth(null),ima.getHeight(null),BufferedImage.TYPE_INT_RGB);

ByteArrayOutputStream bas = new ByteArrayOutputStream();
ImageIO.write(bu,"png", bas);
byte[] data = bas.toByteArray();

I have trouble converting the Image to a BufferedImage as it is not displayed anywhere, it's obvious that in above code BufferedImage is empty.

Does anyone know how it is possible to convert the Image to a BufferedImage? What I also wonder is there any other way in converting the Image to a byte array? Is my approach in the right direction?
Help would be really appreciated, stuck on this one for a while. Thanks in advance.
Old 03-16-2007, 02:00 PM   #2
Registered: Aug 2003
Location: Vancouver, Canada
Distribution: RH, SL
Posts: 37

Rep: Reputation: 15
Hi, I am not familiar with Java, but it is obvious to me that the line:

BufferedImage bu =
new BufferedImage(ima.getWidth(null),ima.getHeight(null),BufferedImage.TYPE_INT_RGB);

is declaring a brand new object, with the same width and height as "ima", but not actually using or copying the contents of "ima". If I were you, I would look at the class definition for BufferedImage, and see if there is a constructor which allows you to copy an Image, or if BufferedImage inherits from Image, then you certainly can copy or clone "ima".
Old 03-17-2007, 09:49 AM   #3
Senior Member
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
get the graphics of the BufferedImage you created, then use the drawImage method to draw to the buffer.

Image ima = mappoint.retrieveImage(Route);

BufferedImage bu = new BufferedImage(ima.getWidth(null),ima.getHeight(null),BufferedImage.TYPE_INT_RGB);
Graphics g = bu.getGraphics();


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
C: a float from 2 bytes of a 4-byte array carcassonne Programming 8 01-17-2007 06:23 PM
How to byte-get an image? kornerr Programming 9 04-24-2006 07:25 PM
java; converting strings to char into an array xemous Programming 5 03-25-2006 08:40 AM
how to convert long integer value to byte array appas Programming 11 11-23-2004 01:56 PM
Java byte array problem Mohsen Programming 2 03-11-2004 01:37 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:45 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration