how to get byte[] from an image

Discussion in 'Java' started by vaneric, Nov 12, 2008.

  1. vaneric

    vaneric Guest

    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
    vaneric, Nov 12, 2008
    #1
    1. Advertising

  2. On Nov 12, 7:42 pm, vaneric <> 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();
    }
    softwarepearls_com, Nov 12, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas
    Replies:
    1
    Views:
    843
    Jonathan Bromley
    May 4, 2004
  2. =?Utf-8?B?RGVhc3Vu?=

    Conversion issue: Image>Byte array>Image

    =?Utf-8?B?RGVhc3Vu?=, Jul 14, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    894
    =?Utf-8?B?RGVhc3Vu?=
    Jul 14, 2005
  3. Bharat Bhushan

    Appending byte[] to another byte[] array

    Bharat Bhushan, Aug 5, 2003, in forum: Java
    Replies:
    15
    Views:
    40,235
    Roedy Green
    Aug 5, 2003
  4. Jean-Daniel Gamache
    Replies:
    0
    Views:
    419
    Jean-Daniel Gamache
    Jul 14, 2004
  5. Peter
    Replies:
    3
    Views:
    724
    Michael Borgwardt
    Aug 5, 2004
Loading...

Share This Page