how to use a byte array to create image

Discussion in 'Java' started by jimgardener, Jun 25, 2008.

  1. jimgardener

    jimgardener Guest

    i have a byte array that i contains image data.Can someone tell me how
    i can use it to create a TIFF image with JAI api? I created a
    bufferedimage etc as below..but i don't know how to use my byte array
    to create the image

    BufferedImage result = mycreateImage(width, height);
    WritableRaster raster = result.getRaster();

    where
    private static BufferedImage mycreateImage(int width, int height) {
    int colorSpace = ColorSpace.CS_sRGB;
    ColorSpace cs = ColorSpace.getInstance(colorSpace);
    boolean hasAlpha = false;
    boolean isAlphaPremultiplied = true;
    int transparency = java.awt.Transparency.OPAQUE;
    int dataType = java.awt.image.DataBuffer.TYPE_USHORT;
    java.awt.image.ColorModel cm = new
    java.awt.image.ComponentColorModel(
    cs,
    hasAlpha,
    isAlphaPremultiplied,
    transparency,
    dataType
    );

    int numBands = 3;
    java.awt.image.SampleModel sm = new
    java.awt.image.BandedSampleModel(
    dataType,
    width,
    height,
    numBands
    );

    java.awt.Point location = null;
    java.awt.image.WritableRaster raster =
    java.awt.image.Raster.createWritableRaster(sm, location);

    java.util.Hashtable properties = null;
    BufferedImage result = new BufferedImage(
    cm,
    raster,
    isAlphaPremultiplied,
    properties
    );

    return result;
    }
    jimgardener, Jun 25, 2008
    #1
    1. Advertising

  2. jimgardener

    Stefan Ram Guest

    Stefan Ram, Jun 25, 2008
    #2
    1. Advertising

  3. jimgardener

    Tom Anderson Guest

    On Wed, 25 Jun 2008, jimgardener wrote:

    > i have a byte array that i contains image data.Can someone tell me how i
    > can use it to create a TIFF image with JAI api? I created a
    > bufferedimage etc as below..but i don't know how to use my byte array to
    > create the image


    You don't actually need to create a BufferedImage. Instead, put your byte
    array inside a ByteArrayInputStream. Feed the stream to the appropriate
    decoder from javax.imageio or JAI.

    tom

    --
    Yesterday's research projects are today's utilities and tomorrow's
    historical footnotes. -- Roy Smith
    Tom Anderson, Jun 25, 2008
    #3
  4. jimgardener wrote:
    > i have a byte array that i contains image data.Can someone tell me how
    > i can use it to create a TIFF image with JAI api? I created a
    > bufferedimage etc as below..but i don't know how to use my byte array
    > to create the image
    >
    > BufferedImage result = mycreateImage(width, height);
    > WritableRaster raster = result.getRaster();
    >
    > where
    > private static BufferedImage mycreateImage(int width, int height) {
    > int colorSpace = ColorSpace.CS_sRGB;
    > ColorSpace cs = ColorSpace.getInstance(colorSpace);
    > boolean hasAlpha = false;
    > boolean isAlphaPremultiplied = true;
    > int transparency = java.awt.Transparency.OPAQUE;
    > int dataType = java.awt.image.DataBuffer.TYPE_USHORT;
    > java.awt.image.ColorModel cm = new
    > java.awt.image.ComponentColorModel(
    > cs,
    > hasAlpha,
    > isAlphaPremultiplied,
    > transparency,
    > dataType
    > );
    >
    > int numBands = 3;
    > java.awt.image.SampleModel sm = new
    > java.awt.image.BandedSampleModel(
    > dataType,
    > width,
    > height,
    > numBands
    > );
    >
    > java.awt.Point location = null;
    > java.awt.image.WritableRaster raster =
    > java.awt.image.Raster.createWritableRaster(sm, location);
    >
    > java.util.Hashtable properties = null;
    > BufferedImage result = new BufferedImage(
    > cm,
    > raster,
    > isAlphaPremultiplied,
    > properties
    > );
    >
    > return result;
    > }


    If what you have is truly 'image data' in the Java sense then creating
    the image from a byte array is done by converting the bytes to an int[]
    and setting the data of the BufferedImage with setRGB(). From there use
    ImageIO to write out your TIFF file.

    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.

    --

    Knute Johnson
    email s/nospam/knute2008/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, Jun 25, 2008
    #4
  5. jimgardener

    Qu0ll Guest

    "Knute Johnson" <> wrote in message
    news:4862688e$0$4084$...
    > jimgardener wrote:


    [...]

    > If what you have is truly 'image data' in the Java sense then creating the
    > image from a byte array is done by converting the bytes to an int[] and
    > setting the data of the BufferedImage with setRGB(). From there use
    > ImageIO to write out your TIFF file.
    >
    > 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 too am interested in this. 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. I can read the contents from
    the file as a byte array and transmit that array but how do I turn the array
    into some form of image to be displayed on a screen at the other end?

    --
    And loving it,

    -Qu0ll (Rare, not extinct)
    _________________________________________________

    [Replace the "SixFour" with numbers to email me]
    Qu0ll, Jun 26, 2008
    #5
  6. On Jun 26, 7:49 pm, "Qu0ll" <> wrote:
    >...I can read the contents from
    > the file as a byte array and transmit that array but how do I turn the array
    > into some form of image to be displayed on a screen at the other end?


    Toolkit.createImage(byte[])

    --
    Andrew Thompson
    http://pscode.org/
    Andrew Thompson, Jun 26, 2008
    #6
  7. jimgardener

    Qu0ll Guest

    "Andrew Thompson" <> wrote in message
    news:...
    > On Jun 26, 7:49 pm, "Qu0ll" <> wrote:
    >>...I can read the contents from
    >> the file as a byte array and transmit that array but how do I turn the
    >> array
    >> into some form of image to be displayed on a screen at the other end?

    >
    > Toolkit.createImage(byte[])


    Excellent, thank you.

    --
    And loving it,

    -Qu0ll (Rare, not extinct)
    _________________________________________________

    [Replace the "SixFour" with numbers to email me]
    Qu0ll, Jun 26, 2008
    #7
    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. =?Utf-8?B?RGVhc3Vu?=

    Conversion issue: Image>Byte array>Image

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

    Appending byte[] to another byte[] array

    Bharat Bhushan, Aug 5, 2003, in forum: Java
    Replies:
    15
    Views:
    40,240
    Roedy Green
    Aug 5, 2003
  3. Kirby
    Replies:
    3
    Views:
    640
    Kirby
    Oct 8, 2004
  4. Replies:
    20
    Views:
    9,759
    licebmi
    Sep 7, 2009
  5. Tom McGlynn
    Replies:
    4
    Views:
    852
    Mark Space
    Apr 19, 2008
Loading...

Share This Page