×

Discussion Board

Results 1 to 15 of 15
  1. #1
    Registered User
    Join Date
    Sep 2009
    Location
    Bangalore, India
    Posts
    49

    Convert an muttable image into Array of Bytes

    Hi everyone,
    I have am mutable image which was captured as a screen shot.. I want to send that image as MMS, So i need to convert that image into byte of Arrays.....
    Can anyone provide me a possible solution for the same...
    i have written the below code but it gives the memory exceeds problem..: Can anyone please suggest me..

    int rawInt[]=new int[targetImage.getWidth()*targetImage.getHeight()];
    targetImage.getRGB(rawInt,0,targetImage.getWidth(),0,0,targetImage.getWidth(),targetImage.getHeight());
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    for(int i=0;i<rawInt.length;i++)
    baos.write(rawInt[i]);
    byte byteData[]=baos.toByteArray();
    rawInt=null;
    message.addMessagePart(new MessagePart(byteData, 0, byteData.length, "image/png", "card", "card image", null));
    ( And please forgive the lack of correct terminology , if any)
    Regards
    Nilanchala

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

    Re: Convert an muttable image into Array of Bytes

    Try to directly get a ByteArray from the image and send it, dont go for Int array.For this you can use -- NokiaUI
    .DirectGraphics dg = DirectUtils.getDirectGraphics(g); // g is the graphics instance of your Image.
    dg.getPixels();

    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

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

    Re: Convert an muttable image into Array of Bytes

    Hi ektasrv ....

    Thanks for the information dude...
    And, Can Please find me some help for downloading the NokiaUI and about the usages of the same...
    I am not able to find the same...
    Regards
    Nilanchala

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

    Re: Convert an muttable image into Array of Bytes

    Quote Originally Posted by nilanchalapanigrahy View Post
    Hi ektasrv ....

    Thanks for the information dude...
    And, Can Please find me some help for downloading the NokiaUI and about the usages of the same...
    I am not able to find the same...
    You get this Nokia UI zip with any Nokia SDK or emulator that you download from Nokia website. (Download Nokia S60 Platform SDK or S40 Platform SDK)
    For API doc see this - http://library.forum.nokia.com/index...w-summary.html
    Thanks,
    Ekta

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

    Re: Convert an muttable image into Array of Bytes

    Quote Originally Posted by ektasrv View Post
    You get this Nokia UI zip with any Nokia SDK or emulator that you download from Nokia website. (Download Nokia S60 Platform SDK or S40 Platform SDK)
    For API doc see this - http://library.forum.nokia.com/index...w-summary.html
    I got the classes.zip file... But when i includes that in my project the jar file size becomes more then 900 KB after using the Obfuscating.. But before my jar size was around 300 KB now it is huge.. What can be a possible solution for the same..

    Anyways thanks for your support man...
    Regards
    Nilanchala

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

    Re: Convert an muttable image into Array of Bytes

    Quote Originally Posted by nilanchalapanigrahy View Post
    I got the classes.zip file... But when i includes that in my project the jar file size becomes more then 900 KB after using the Obfuscating.. But before my jar size was around 300 KB now it is huge.. What can be a possible solution for the same..

    Anyways thanks for your support man...
    You don't need to include the entire Nokia UI Classes.zip in your project jar, you just need to include it in your project as external Library on which your project depends.
    In WTK project you can keep it lib folder.
    This way you will not add any extra overload to your JAR or app size.
    Which IDE your are using ?
    Thanks,
    Ekta

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

    Re: Convert an muttable image into Array of Bytes

    How big is the image you are trying to convert?

    I recommend you avoid the Nokia API, as it will prevent your application from working on other devices.

    Graham.

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

    Re: Convert an muttable image into Array of Bytes

    If you don't want to use Nokia UI, try to convert the RGBInt array into byte array and then send it over MMS.

    Refer to: http://stackoverflow.com/questions/5...-back-to-image

    You can use this only if your image size is small enough and you are able to allocate this much of memory -Int rawInt[]=new int[targetImage.getWidth()*targetImage.getHeight()]; on the device.
    Thanks,
    Ekta

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

    Re: Convert an muttable image into Array of Bytes

    Quote Originally Posted by grahamhughes View Post
    How big is the image you are trying to convert?

    I recommend you avoid the Nokia API, as it will prevent your application from working on other devices.

    Graham.
    Hi..
    Thanks for your help dude...
    The image size is around 30KB to 50 KB..
    Yeah as Mr. Ektasrv answered.. the conversation from image to IntRGB[ ] array and then to Byte consumes more memory.. if my image size is around 1/2 KB then it works fine..
    Any possible solution dude.. Hope you understand my requirements..
    Regards
    Nilanchala

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

    Re: Convert an muttable image into Array of Bytes

    Quote Originally Posted by nilanchalapanigrahy View Post
    Hi..
    Thanks for your help dude...
    The image size is around 30KB to 50 KB..
    Yeah as Mr. Ektasrv answered.. the conversation from image to IntRGB[ ] array and then to Byte consumes more memory.. if my image size is around 1/2 KB then it works fine..
    Any possible solution dude.. Hope you understand my requirements..
    Here for GetRGB and Int Array, its the size (Height x width) of the image more important then size in KBs, so if you are targeting only Nokia and SE devices for now you can go with Nokia UI based approach and try to find DirectGraphics compatible things in other OEM's SDK's later when you want to port your app to those devices.
    Thanks,
    Ekta

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

    Re: Convert an muttable image into Array of Bytes

    Quote Originally Posted by ektasrv View Post
    Here for GetRGB and Int Array, its the size (Height x width) of the image more important then size in KBs, so if you are targeting only Nokia and SE devices for now you can go with Nokia UI based approach and try to find DirectGraphics compatible things in other OEM's SDK's later when you want to port your app to those devices.
    Dear Ekta,
    My application is targeted towards different handsets of different OS, with ME support.. So I think use of Nokia UI may not be the right solution...
    And to say like my app is in the final stage so i need to find out some way out of it...
    Regards
    Nilanchala

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

    Re: Convert an muttable image into Array of Bytes

    Sorry... how big are the images in pixels? Check out Tiger79's wiki article on this subject.

    Graham.

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

    Re: Convert an muttable image into Array of Bytes

    Quote Originally Posted by grahamhughes View Post
    Sorry... how big are the images in pixels? Check out Tiger79's wiki article on this subject.

    Graham.
    Hi...
    As answer to your question, As the application is for bigger sized images also so the size of the image is not static.. but As for now we can consider for 240 X 320 size...
    Regards
    Nilanchala

  14. #14
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: Convert an muttable image into Array of Bytes

    try running it with the WTK emulator, and use the Memory Monitor... The shown numbers must not be taken as real-world memory usage which would be the case on a real device BUT it will give you a good idea on the behaviour of your app in regards to memory usage... Also it will show you the type of objects that are using the most memory...
    Where are you testing on ? Emulator or device ? Do you have acces to a S60 to test on ?

    Also take into account that when you are working with a 320x240 Image onscreen it will use quite some memory, and if then you are also trying to make a screenshot of that it will add up to the memory already in use...
    Is this the only Image you are using in your app ? Are there any other memory-consuming objects ? Are you releasing resources you don't need anymore so that the Garbage Collector can do it's work ?
    Note: please don't send private questions to me, as I don't have time to provide private support. All such questions should be posted on public forums, where I and others can see and answer them.

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

    Re: Convert an muttable image into Array of Bytes

    Try using the following methods to convert the RGB Integer Array from Image into btye[] for sending as MMS.

    Code:
    private static byte[] convertIntArrayToByteArray(int[] integers)
    {
            byte[] bytes = new byte[integers.length*4];
            for (int index = 0; index < integers.length; index++)
            {
                    byte[] integerBytes = convertIntToByteArray(integers[index]);
                    bytes[index*4] =                integerBytes[0];
                    bytes[1 + (index*4)] = integerBytes[1];
                    bytes[2 + (index*4)] = integerBytes[2];
                    bytes[3 + (index*4)] = integerBytes[3];
            }
            return bytes;
    }
    private static byte[] convertIntToByteArray(int integer)
    {
            byte[] bytes = new byte[4];
            bytes[0] =(byte)( integer >> 24 );
            bytes[1] =(byte)( (integer << 8) >> 24 );
            bytes[2] =(byte)( (integer << 16) >> 24 );
            bytes[3] =(byte)( (integer << 24) >> 24 );
            return bytes;
    }
    Thanks,
    Ekta

Similar Threads

  1. how to convert image to byte array ?
    By kris_sagar in forum Mobile Java General
    Replies: 16
    Last Post: 2011-02-28, 13:31
  2. How to convert image into byte array
    By dhiraj.deka in forum Mobile Java General
    Replies: 6
    Last Post: 2009-11-23, 09:23
  3. How to convert an InputStream to array of bytes?
    By Eng.Shefooooooo in forum Mobile Java Media (Graphics & Sounds)
    Replies: 5
    Last Post: 2009-07-17, 11:04
  4. convert bytes to image object
    By Alpinet in forum Python
    Replies: 0
    Last Post: 2008-05-04, 20:11
  5. convert image to byte array
    By s4028469 in forum Symbian Media (Closed)
    Replies: 4
    Last Post: 2007-08-15, 11:07

Posting Permissions

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