Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > how to get byte[] from an image

Reply
Thread Tools

how to get byte[] from an image

 
 
vaneric
Guest
Posts: n/a
 
      11-12-2008
hi
i have a jpeg color image .i need to get the image data as a byte
array .Is there any easy way of doing this using jdk alone?I couldn't
figure out how to create an Image from a filename like say 'F:
\myimages\testimage.jpeg'.

if someone can help pls do.
thanks in advance
eric
 
Reply With Quote
 
 
 
 
softwarepearls_com
Guest
Posts: n/a
 
      11-12-2008
On Nov 12, 7:42*pm, vaneric <(E-Mail Removed)> wrote:
> hi
> i have a jpeg color image .i need to get the image data as a byte
> array .Is there any easy way of doing this using jdk alone?I couldn't
> figure out how to create an Image *from a filename like say 'F:
> \myimages\testimage.jpeg'.
>
> if someone can help pls do.


A byte array or an int array? Java's natural pixel format is 32-bit
ARGB.. so you normally work with int[], not byte[].

Here's a routine I wrote to get the raw BYTE pixels of an 8BPP
BufferedImage. It's probably trivial to adapt it to return an int[]
for a normal 32-bit Image.

/
************************************************** ****************************************
* Get the low-level byte[] pixel data of a TYPE_BYTE_INDEXED
{@link BufferedImage}.
* <P>
* Implementation note: this routine is quick because it just digs
down the
* BufferedImage/Raster/DataBuffer APIs to get the underlying byte
[]. Compare this to
* {@link #extractPixelArray} that is much slower.
*
* @param bufferedImage an 8 bits-per-pixel {@link BufferedImage}
* @return an byte[] containing entire image of 8-bit pixels as
linear byte array
* @see #extractPixelArray
* @throws IllegalArgumentException if passed image isn't an 8BPP
image or if number of
* internal DataBuffer banks isn't 1.

************************************************** ***************************************/
public static byte[] getBytePixelArrayFor(final BufferedImage
bufferedImage) {

final WritableRaster writableRaster = bufferedImage.getRaster
();
final DataBuffer dataBuffer = writableRaster.getDataBuffer();

final int pixelSize = DataBuffer.getDataTypeSize
(dataBuffer.getDataType());
if (pixelSize != {
throw new IllegalArgumentException("Passed BufferedImage
not an 8bpp image (bpp=" + pixelSize + ")");
}

final DataBufferByte dataBufferByte = (DataBufferByte)
dataBuffer;

final int numBanks = dataBuffer.getNumBanks();
if (numBanks != 1) {
throw new IllegalArgumentException("Passed BufferedImage
has more than one bank (# banks=" + numBanks + ")");
}

return dataBufferByte.getData();
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Pil Raw Image Viewer-- How to get image mode Andrew Python 0 04-05-2007 08:49 PM
How to get a copy of image when mouse clicked is performed on image in JSP rajbala.3399@gmail.com Java 1 10-28-2006 05:04 AM
wx.Image: Couldn't add an image to the image list. Laszlo Zsolt Nagy Python 1 01-26-2005 09:55 PM



Advertisments