LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 03-13-2007, 02:04 PM   #1
megabot
Member
 
Registered: Aug 2005
Posts: 39

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


Hello,

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, 03:00 PM   #2
asgeirss
Member
 
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, 10:49 AM   #3
xhi
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.

Code:
Image ima = mappoint.retrieveImage(Route);

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


Reply


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 07:23 PM
How to byte-get an image? kornerr Programming 9 04-24-2006 08:25 PM
java; converting strings to char into an array xemous Programming 5 03-25-2006 09:40 AM
how to convert long integer value to byte array appas Programming 11 11-23-2004 02:56 PM
Java byte array problem Mohsen Programming 2 03-11-2004 02:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:41 AM.

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