Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

convert BufferedImage to a byte array

Thread view: 
cyberco - 12 Feb 2006 14:19 GMT
I want to get the bytes from a BufferedImage to send it over the line,
how can I achieve that? I seems to be trickier than I thought.
Timo Stamm - 12 Feb 2006 15:32 GMT
cyberco schrieb:
> I want to get the bytes from a BufferedImage to send it over the line,
> how can I achieve that? I seems to be trickier than I thought.

See ImageIO
Roedy Green - 12 Feb 2006 16:36 GMT
>I want to get the bytes from a BufferedImage to send it over the line,
>how can I achieve that? I seems to be trickier than I thought.

you can't serialise it.  The actual form is platform dependent, so it
might not mean anything when you got to the other end. What you want
to do is extract an array of ints or shorts or bytes and send that by
serialisation or DataOutputStream.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 12 Feb 2006 16:47 GMT
On Sun, 12 Feb 2006 16:36:20 GMT, Roedy Green
<my_email_is_posted_on_my_website@munged.invalid> wrote, quoted or
indirectly quoted someone who said :

>>I want to get the bytes from a BufferedImage to send it over the line,
>>how can I achieve that? I seems to be trickier than I thought.
[quoted text clipped - 3 lines]
>to do is extract an array of ints or shorts or bytes and send that by
>serialisation or DataOutputStream.

you can convert it to png or jpg format and send that.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

cyberco - 12 Feb 2006 18:46 GMT
Yes, getting the bytes is what I'm trying to do. Unfortunately ImageIO
does not have any convenience methods for that purpose. Any other
suggestions?
Timo Stamm - 12 Feb 2006 19:04 GMT
cyberco schrieb:
> Yes, getting the bytes is what I'm trying to do.

Roedy pointed out why BufferedImage itself isn't serializable.

You could access the matrices and send that data over the line, but you
will most likely need some meta data for different color models etc. and
will reinvent the wheel, because there are already dozens of image file
formats available through ImageIO.

> Unfortunately ImageIO does not have any convenience methods for that
> purpose.

What could be more convenient than:

ImageIO.write(RenderedImage im, String formatName, OutputStream output);

?

Timo
cyberco - 12 Feb 2006 22:29 GMT
Oops! Sorry, Timo, you're right. My bad. I overlook ImageIO.write(). I
succeeded getting the byte[] in the following way.

####################
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufImage, formatName, baos);
byte[] bytesOut = baos.toByteArray();
Roedy Green - 13 Feb 2006 09:26 GMT
>Yes, getting the bytes is what I'm trying to do. Unfortunately ImageIO
>does not have any convenience methods for that purpose. Any other
>suggestions?

It can write to an OutputStream. Make that a ByteArrayOutputStream.
see
http://mindprod.com/jgloss/imageio.html#TOBYTES
for how.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

cyberco - 13 Feb 2006 21:33 GMT
Excellent examples, Roedy! Thanks for the link!
opalpa@gmail.com opalinski from opalpaweb - 13 Feb 2006 22:48 GMT
You can serialize ImageIcon after making an ImageIcon from
BufferedImage.

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
E. Naubauer - 12 Feb 2006 23:52 GMT
cyberco schrieb:
> I want to get the bytes from a BufferedImage to send it over the line,
> how can I achieve that? I seems to be trickier than I thought.

If you know the type of the data buffer of the BufferedImage its easy:

int[] t =
((DataBufferInt)(myBufferedImage).getRaster().getDataBuffer()).getData();

with DataBufferByte it works the same.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.