×

Discussion Board

Results 1 to 10 of 10
  1. #1
    Regular Contributor
    Join Date
    Dec 2009
    Posts
    59

    Java image and byte[] for J2ME and J2SE

    Hello,

    What i want is this:
    1. Convert Image object to byte[] in J2ME application
    2. Send it to Web Service (store it in database)
    3. Read byte[] in J2SE application, convert it back to Image

    If in step 1 i use J2SE, then everything is working great. Is there a 3rd party class that can be used to convert image-to-byte[] and back ? Or maybe another option.

    Thanks,
    Alex

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

    Re: Java image and byte[] for J2ME and J2SE

    What is the source of the image?

    Assuming you have an Image object (img), then you can:

    Code:
    // get raw pixel data
    int width = img.getWidth();
    int height = img.getHeight();
    int[] pixels = new int[width * height];
    img.getRGB(pixels, 0, width, 0, 0, width, height);
    
    // convert int[] to byte[]
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    DataOutputStream dout = new DataOutputStream(bout);
    // I suggest writing these first: you'll need them to convert back
    dout.writeInt(width);
    dout.writeInt(height);
    // then the pixels
    for (int i = 0; i < pixels.length; i++) {
        dout.writeInt(pixels[i]);
    }
    byte[] data = bout.toByteArray();
    At the J2SE end, you can use the corresponding InputStrem classes to reverse this, and reconstruct the int[] along with the width and height. The int[] you will have is basically the same as you would acquire in J2SE using an AWT PixelGrabber object, and can easily be converted to an AWT Image.

    Of course, if you acquired the Image object from a byte[] source in the first place, then you could just transmit that...

    Downside is: this will be a large volume of data, (width * height * 4) bytes. There's nothing (that I've seen) off-the-shelf that will generate a compressed stream for you.

    Graham.

  3. #3
    Regular Contributor
    Join Date
    Dec 2009
    Posts
    59

    Re: Java image and byte[] for J2ME and J2SE

    Thank you for the answer Graham,

    Yes, i've already used that code to convert the image to byte[], now getting it back is the problem (i'm not friendly with reading and writting pixels). If you have time maybe you could write the code for reading the image. I'll try it myself, but just in case i don't succed, your code would be a life-saver

    Thanks,
    Alex

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

    Re: Java image and byte[] for J2ME and J2SE

    It's a while since I've done it. Take a look at the JavaDocs for the class java.awt.image.MemoryImageSource. That's the class used to create Image objects from int[] data. Something like:

    Code:
    // this is Java SE, not ME!
    Image img = Toolkit.getDefaultToolkit().createImage(
                        new MemoryImageSource(width, height, data, 0, width)
                    );
    You just need to read the byte[] back into width, height and the int[], but ByteArrayInputStream and DataInputStream make that simple enough.

    Graham.

  5. #5
    Regular Contributor
    Join Date
    Dec 2009
    Posts
    59

    Re: Java image and byte[] for J2ME and J2SE

    Thank you very much Graham! I did it.

    If anyone else needs something like this, here is the complete code:
    Image to byte[] (only for J2ME):
    Code:
    import java.io.ByteArrayOutputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import javax.microedition.lcdui.Image;
    
    public class ImageToByteArray {
        
        public static byte[] getByteArray(Image img) {
            byte[] bytes = null;
            int width = img.getWidth();
            int height = img.getHeight();
            int[] pixels = new int[width * height];
            img.getRGB(pixels, 0, width, 0, 0, width, height);
    
            try {
                // convert int[] to byte[]
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                DataOutputStream dout = new DataOutputStream(bout);
                // I suggest writing these first: you'll need them to convert back
                dout.writeInt(width);
                dout.writeInt(height);
                // then the pixels
                for (int i = 0; i < pixels.length; i++) {
                    dout.writeInt(pixels[i]);
                }
                bytes = bout.toByteArray();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return bytes;
        }
    }
    byte[] to Image (only for J2SE):
    Code:
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.image.MemoryImageSource;
    import java.io.ByteArrayInputStream;
    import java.io.DataInputStream;
    import java.io.IOException;
    
    public class ByteArrayToImage {
    
        public static Image getImage(byte[] bytes) {
            Image image = null;
            try {
                ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
                DataInputStream dis = new DataInputStream(bais);
                int w = dis.readInt();
                int h = dis.readInt();
                int[] pixels = new int[w * h];
                for (int i = 0; i < pixels.length; i++) {
                    pixels[i] = dis.readInt();
                }
                image = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(w, h, pixels, 0, w));
            } catch(IOException e) {
                e.printStackTrace();
            }
    
            return image;
        }
    }

  6. #6
    Registered User
    Join Date
    Mar 2010
    Location
    Chester, UK
    Posts
    20

    Talking Re: Java image and byte[] for J2ME and J2SE

    Thank you so much Infernal01 for going to the trouble of posting the stream solution.

    I've seen some really unhelpful (arrogant) people just saying "No, not possible. Don't even try."
    I'd hate to see their programs - no ability to push the boundaries at all.

    Again, many thanks!

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

    Re: Java image and byte[] for J2ME and J2SE

    To be fair to the people whose posts you've seen (possibly mine!), some things really are not possible. MIDP Java is deliberately wrapped in a bunch of security restrictions (the "sandbox"), which are specifically designed to make some things impossible.

    In addition, some things are possibly only on a very limited number of devices, or possibly only when the application is suitably signed (with "suitably" meaning something quite different depending on manufacturer, device, and network operator branding).

    Sometimes, it is more helpful to tell someone that something can't be done (for example, determining the IMEI number of a Series 40 device), and give them the chance to find an alternative to solution to their situation, rather than let them waste a huge amount of time. I think it's also useful to tell people if the solution to their problem won't work on a significant number of handsets, rather than let them spend a lot of time developing a product that they will later discover has limited commercial opportunity.

    Graham.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    20

    Re: Java image and byte[] for J2ME and J2SE

    Hi all.
    I wrote a post about converting an image to byte array in JavaMe (J2ME):

    http://www.java-n-me.com/2011/02/con...y-byte-in.html

    Hope this help.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    10

    Re: Java image and byte[] for J2ME and J2SE

    thanks for the code but may i get the reverse way which convert the image to byte array in J2SE and send to the midlet then J2ME convert the byte array back to image and show in the ImageItem. i tried but i cannot get. Please help

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

    Re: Java image and byte[] for J2ME and J2SE

    Depends how you format your byte[]. If it's a PNG, its easy to create an Image on the device.

    Code:
    byte[] data = getImageDataFromServer();
    Image img = Image.createImage(data, 0, data.length);
    To create a PNG on the server, you'll probably want to use the Java Advanced Imaging API. For help using it, you probably want a Java SE forum.

    Graham.

Similar Threads

  1. N70 java jsr-82 OBEX Problem
    By jfgpimpollo in forum Mobile Java Networking & Messaging & Security
    Replies: 5
    Last Post: 2009-09-16, 19:10

Posting Permissions

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