File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get byte[] from the Image?

 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
Can anyone give me a hint on how to a get a byte[] array or InputStream from the Image/Icon object. Need to store an image that doesn't come from the filesytem in the DB's LONG RAW field. I can get int[] with PixelGrabber class, but how does one convert it to byte[]? thanks...
 
Thomas Suer
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please try this:

Good luck
Tom
 
Eric Hoskland
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a similar problem:
I want to convert an java.awt.image into a byte array NOT into an int array. Shouldn't there be a way to specify a custom colorspace and colormodel to pixelgrabber so that it grabs the pixels and puts them into a byte array opposed to a int array??? Or is this just not possible
The image ONLY exists as a java.awt.image NEVER as anything else so there isn't a prior step that I can intercept (which is how the example above does it)
Thanks
 
Martin Thorpe
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok this is the sort of problem I am having too. I understand all of the above method apart from the baseClass stuff!?!?! can anyone explain that to me? How I should call this method?

thanks for taking the time to read.

cheers
Martin
 
Craig Wood
Ranch Hand
Posts: 1535
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Class argument is the class whose class loader will be asked to locate the file (see getResource Method Detail in Class api). If it is simply the enclosing class you can use the keyword this. If called from a static context you will need to use the MyClass.class form of class reference. If called from within an inner class you can use a member variable reference created at construction.

See the tutorial page How to Use Icons, especialy the section Loading Images Using getResource.
 
Ben Hur
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not just write the image out using the javax.imageio.ImageIO which has the following static method:



You're image does get encoded into the format you specify (i.e. jpeg or png for example), which does seem to be a good side-effect.

Take a peek at the
Java Almanac.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic