Re: how to get byte[] from an image



On Nov 12, 7:42 pm, vaneric <vaneric...@xxxxxxxxx> 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 != 8) {
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();
}

.



Relevant Pages

  • how to use a byte array to create image
    ... i have a byte array that i contains image data.Can someone tell me how ... bufferedimage etc as below..but i don't know how to use my byte array ... private static BufferedImage mycreateImage(int width, ... int dataType = java.awt.image.DataBuffer.TYPE_USHORT; ...
    (comp.lang.java.programmer)
  • Re: how to use a byte array to create image
    ... image from a byte array is done by converting the bytes to an int[] and setting the data of the BufferedImage with setRGB. ... If the 'image data' is an array of bytes read from some file then I would have to know what sort of file to tell you how to proceed. ... I have some JPEG files from which I need to create images suitable for transmission across a network and I note that BufferedImage does not implement Serializable. ...
    (comp.lang.java.programmer)
  • Re: byte[] to Image
    ... setRGB(int, int, int) on a newly created BufferedImage for every pixel in ... the array. ...
    (comp.lang.java.programmer)
  • Re: Fastest pixel by pixel operation
    ... can easily change for something different for the video memory. ... I'm thinking the fastest thing would be to create a BufferedImage ... the pixel array in its DataBuffer. ...
    (comp.lang.java.programmer)
  • Re: Binary file read/write
    ... Get InputFileNum,, DataBuffer ... Put OutputFileNum,, DataBuffer ... developers would use the FileCopy statement! ... and grab it all in one go, (Get the entire array) then ...
    (microsoft.public.vb.general.discussion)