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 / First Aid / July 2006

Tip: Looking for answers? Try searching our database.

Newbie Image class question

Thread view: 
fiziwig - 28 Jul 2006 00:20 GMT
I have an image in an ImageIcon that I need to BASE64 encode for
transmission. How do I get the pixel bytes from an Image object?

Image.getSource() gets me something called an ImageProducer, (whatever
that is) but I don't see how that gets me any closer to being able to
turn the actual image into an array of bytes so I can encode them.

What I seem to need is the OPPOSITE of the ImageIcon constructor:
ImageIcon(byte[] imageData) that will get me back the image data as a
byte array, but there doesn't seem to be such a method in the ImageIcon
or Image class.

Any hints or clues would be deeply appreciated.

--gary
Knute Johnson - 28 Jul 2006 01:27 GMT
> I have an image in an ImageIcon that I need to BASE64 encode for
> transmission. How do I get the pixel bytes from an Image object?

You can't easily.

> Image.getSource() gets me something called an ImageProducer, (whatever
> that is) but I don't see how that gets me any closer to being able to
> turn the actual image into an array of bytes so I can encode them.

It won't help you.

> What I seem to need is the OPPOSITE of the ImageIcon constructor:
> ImageIcon(byte[] imageData) that will get me back the image data as a
> byte array, but there doesn't seem to be such a method in the ImageIcon
> or Image class.

Doesn't exist.

> Any hints or clues would be deeply appreciated.

Don't use ImageIcons as the class to load images with.  Use the methods
from the ImageIO class.  That will give you a BufferedImage that has a
method to return the actual byte values of the pixels.

If you are using an ancient version of the VM then you can:

1) get the Image from your ImageIcon
2) create a BufferedImage of the same dimensions
3) draw the Image onto the BufferedImage
4) recover the pixel values from the BufferedImage

My best suggestion is to use the modern classes and methods.

Signature

Knute Johnson
email s/nospam/knute/

fiziwig - 28 Jul 2006 19:23 GMT
<snip>
> If you are using an ancient version of the VM then you can:
>
> 1) get the Image from your ImageIcon
> 2) create a BufferedImage of the same dimensions
> 3) draw the Image onto the BufferedImage
> 4) recover the pixel values from the BufferedImage

Thanks, that worked.

> My best suggestion is to use the modern classes and methods.

Thanks. I'm sure that's good advice. As a newbie to Java, is there
anything in the Java docs that tells me which are the modern classes
and methods? I just built my very first ap on snipets of code borrowed
from the Java tutorial on the sun site. I don't recall any mention of
"modern" vs "ancient" classes.

--gary
Oliver Wong - 28 Jul 2006 19:52 GMT
>> My best suggestion is to use the modern classes and methods.
>
[quoted text clipped - 3 lines]
> from the Java tutorial on the sun site. I don't recall any mention of
> "modern" vs "ancient" classes.

   Not really, AFAIK. Some classes have a "since" tag, which tell you when
they were introduced. If they were introduced since 1.5, for example,
they're probably not ancient (seeing as how 1.5 is the current version at
the time of writing this post). However, even if you see something like
"since 1.1", while that's an indication that the class is old, that doesn't
nescessarily mean there exist anything newer to replace it. Maybe that old
class is doing its job well, and Sun is following a "If it ain't broke,
don't fix it" philosophy.

   On the other than, there's also a "deprecated" tag. This is supposed to
clearly indicate that you should avoid the class whenever possible, because
something newer HAS replaced it. Ideally, near the "deprecated" tag will be
an explanation of what to use instead of this class, but sometimes the
documentation authors forget to write that stuff.

   - Oliver
Andrew Thompson - 29 Jul 2006 01:20 GMT
> >> My best suggestion is to use the modern classes and methods.
> >
[quoted text clipped - 18 lines]
> an explanation of what to use instead of this class, but sometimes the
> documentation authors forget to write that stuff.

Yes, that's occasionally true for both the @deprecated and
@since attributes.

OTOH, if you can get your hands on a JVM/rt.jar of that flavor,
simply and add it to the -bootclasspath at compilation time for
a definitive answer.

Andrew T.
Andrew Thompson - 29 Jul 2006 04:24 GMT
> > >> My best suggestion is to use the modern classes and methods.
> > >
[quoted text clipped - 25 lines]
> simply and add it to the -bootclasspath at compilation time for
> a definitive answer.

(..ummm) to the '@since' in any case, I don't think* it
would warn of /deprecated/ classes or members.

*  I can see no way that compilation against a 1.2 rt.jar
could warn you of something that was deprecated in 1.5.

Perhaps compile against both lowest and highest,
lowest for @since, highest for @deprecated.(?)

Andrew T.


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



©2008 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.