×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Dec 2008
    Location
    Bangalore
    Posts
    32

    How to get byte array of Image object.

    Hi all

    In My application i am rescaling captured image to new size now i want to save image but how to get byte array of the rescaled image object.I am using following method to get byte array but after saving its creating image file but after opening saved file in memory nothing available in preview means unable to open.So anybody know how to get image byte array from image object.


    public byte[] getByteArray(Image image) {
    int raw[] = new int[image.getWidth() * image.getHeight()];
    image.getRGB(raw, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
    byte rawByte[] = new byte[image.getWidth() * image.getHeight() * 4];
    int n = 0;
    for(int i = 0; i < raw.length; i++) {
    int ARGB = raw[i];
    int a = (ARGB & 0xff000000) >> 24;
    int r = (ARGB & 0xff0000) >> 16;
    int g = (ARGB & 0xff00) >> 8;
    int b = ARGB & 0xff;
    rawByte[n] = (byte)b;
    rawByte[n + 1] = (byte)g;
    rawByte[n + 2] = (byte)r;
    rawByte[n + 3] = (byte)a;
    n += 4;
    }
    raw = null;
    return rawByte;
    }
    Swapnil Desai
    TELiBrahma Softwares,
    Bangalore.

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: How to get byte array of Image object.

    You need to encode the data into some format the other software will recognize, such as PNG or JPEG. There is a PNG encoder in the wiki.

    Graham.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Location
    Bangalore
    Posts
    32

    Re: How to get byte array of Image object.

    Thanks grahamhughes


    do you know any encoder for jpeg images as i am using jpeg image.
    Swapnil Desai
    TELiBrahma Softwares,
    Bangalore.

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: How to get byte array of Image object.

    No, I don't. It's much more complicated.

    It doesn't matter that the original image was a JPEG. The raw Image object can be converted to a PNG.

    Graham.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: How to get byte array of Image object.

    Also See this thread --> http://www.forum.nokia.com/forum/sho...d.php?p=727960

    You get the correct methods to convert the integer array to byte array and covert it back to Int array for recreating image on retrieval.

    thanks,
    Ekta

  6. #6
    Registered User
    Join Date
    Sep 2009
    Location
    Bangalore, India
    Posts
    49

    Re: How to get byte array of Image object.

    Quote Originally Posted by swap842 View Post
    Thanks grahamhughes


    do you know any encoder for jpeg images as i am using jpeg image.
    Try this link for PNG Encoder.. Hope it ll help you...HERE
    Regards
    Nilanchala

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: How to get byte array of Image object.

    Quote Originally Posted by nilanchalapanigrahy View Post
    Try this link for PNG Encoder.. Hope it ll help you...HERE
    @ nilanchalapanigrahy

    Please read the complete thread before posting your reply - graham has already suggested the same link below http://discussion.forum.nokia.com/fo...l=1#post741819, and do not repeat what others have already suggested.


    Quote Originally Posted by swap842 View Post
    So anybody know how to get image byte array from image object
    Try NokiaUI's com.nokia.mid.ui.DirectGraphics.getPixels for this, it will work on all Nokia and SE devices:

    getPixels

    public void getPixels(byte[] pixels,
    byte[] transparencyMask,
    int offset,
    int scanlength,
    int x,
    int y,
    int width,
    int height,
    int format)
    Copies the pixel (including any transparency mask) values of the graphics context from a specific location to an array of byte values. The pixels will be passed in the format defined by format parameter. If an implementation doesn't support the format an IllegalArgumentException is thrown.
    This method returns only byte-based formats. Requesting all other formats will result in an IllegalArgumentException.

    Throws ArrayIndexOutOfBoundsException if array size is too small for image pixels or transparency mask. The argument transparencyMask can be null if the caller is not interested in getting the mask. See transparencyMask definition on drawPixels(...).

    Note that the scanlength and offset parameters indicate the scanlength and offset in number of pixels. This is not necessarily the same as array indices since multiple pixels may be stored in a byte.

    The current clip region does not affect pixel values stored in the pixels array, i.e. even if a clip region is set and the region intersects the region queried with this method, also the clip region pixels are stored in the pixels array. The operation is subject to the current translation for this Graphics object.

    Parameters:
    pixels - an array which the pixel information will be stored in
    transparencyMask - an array which the transparency mask will be stored in
    offset - offset in the array where the first pixel and mask value will be stored
    scanlength - the relative offset in the array between the corresponding pixels and the mask value in consecutive rows
    x - the x-coordinate of the upper left corner of the region in the graphics context
    y - the y-coordinate of the upper left corner of the region in the graphics context
    width - the width of the region in the graphics context
    height - the height of the region in the graphics context
    format - the format which the pixels are requested in
    Throws:
    java.lang.NullPointerException - if pixels is null
    java.lang.ArrayIndexOutOfBoundsException - if array size is too small for the image pixels or negative index access is attempted, contents of the array remain unchanged
    java.lang.IllegalArgumentException - if x, y, width or height is negative, or the format is wrong or unsupported
    Since:
    1.0
    Thanks,
    Ekta

Similar Threads

  1. Replies: 1
    Last Post: 2009-05-18, 10:51
  2. Error when trying to create image from Byte array
    By chordfone in forum Mobile Java Media (Graphics & Sounds)
    Replies: 0
    Last Post: 2007-11-16, 00:34
  3. Loading Image data from 'raw' bytes
    By LongSteve in forum Mobile Java General
    Replies: 2
    Last Post: 2002-11-20, 17:38
  4. Loading images from byte array on 6130i
    By enlightment in forum Mobile Java General
    Replies: 2
    Last Post: 2002-06-05, 19:44
  5. Get pixel data from an image object on Nokia phones?
    By inopia in forum Mobile Java General
    Replies: 1
    Last Post: 2002-05-15, 09:27

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×