×

Discussion Board

Results 1 to 9 of 9
  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    41

    getting byte array from image

    Hi all,

    please dont shoot me down for posting on this topic again, I am well aware that this is detailed on many different forums...... my probem is that I cant seem to get the solutions to work. I can successfully send an image to my server if the image is stored in the res folder using getimageasresource() (not sure of the excact wording of this method) as this returns a byte array and then i simply send that. My problem is getting bytes from an image stored on the device. I can create an image stored on the device using fileconnection api and display it on screen but I need it in bytes to be able to send it. Does anyone know of a way which definately works as as have tried many methods which all create an image on the server but the image format is then unrecognisable bec ause I am not converting the image to bytes properly.

    my current code is :

    int width = 0;
    int height = 0;

    try {
    Image im = Image.createImage("/camera.png");
    width = im.getWidth();
    height = im.getHeight();
    imgData = new byte[width*height*4];
    rgbArray = new int[width*height];
    rgbArray = im.getRGB();
    }
    catch(Exception e) {}

    for(int ii=0; ii<rgbArray.length*4; ii+=4)
    {
    imgData[ii+0] = (byte) (rgbArray[ii/4] >> 24);
    imgData[ii+1] = (byte) ((rgbArray[ii / 4] >> 16) & 0x000000FF);
    imgData[ii+2] = (byte) ((rgbArray[ii / 4] >> 8) & 0x000000FF);
    imgData[ii+3] = (byte) (rgbArray[ii / 4] & 0x000000FF);
    }

    I have seen this posted a few times elsewhere but it doent seem to work.

    Any help or ideas I would really appreciate them as this is getting to be very annoying.

    Thank you.

    Jules

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

    Re: getting byte array from image

    How doesn't it work? You don't get a byte[], you can't send the byte[], or you can't convert the byte[] back to an image at the other end?

    Remember that the byte[] you will end up with from this process is raw ARGB data. It is not in any established "file format", like PNG or JPEG. You won't be able to convert it back to an Image like:

    Code:
    // won't work
    Image img = Image.createImage(imgData, 0, imgData.length);
    Graham.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    41

    Re: getting byte array from image

    Thanks for the reply,

    What is the easiest method to create an image from this raw data?? Also I will need to send this data to a server where php will re-create the image.
    The issue I am currently having as as you pointed out the data is not directly convertable to an image.

    Many thanks

    Jules

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

    Re: getting byte array from image

    The data from Image.getRGB() can be converted back into an Image using Image.createRGBImage().

    However, if you are getting the original image from a file, then your best bet is to keep the original file, and send that.

    I'm not sure why you find getResourceAsStream() OK, and have a problem file FileConnection. Both enable you to get an InputStream, from which you can read the bytes into a byte array. I suggest you go back to the original problem, and try again to solve that.

    Graham.

  5. #5
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    88

    Re: getting byte array from image

    Hello,
    if you are able to get your image using FileConnection api, you can have a InputStream object from your file connection object. So, to get the byte data, you can do:

    Code:
    InputStream input = //input reference from you FileConnection reference
    
    byte[] imageInBytes = new byte[size];
    
    input.read(imageInBytes);
    []'s

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    41

    Re: getting byte array from image

    Thanks again for your replies..... my code is now as follows

    conn = (FileConnection) Connector.open(filePaths[storedImages[0]],Connector.READ);//path of my image (which exists as Im displaying it)

    is = conn.openInputStream();
    review0 = Image.createImage(is);//review0is an image object
    imageDataLength = is.available();//imageDataLength is a global int
    System.out.println("imagelength is "+imageDataLength);//this si giving me 0???
    imgData1 = new byte[imageDataLength];//imgData1 is a global byte []
    is.read(imgData1);

    Now I know I am on the right lines as this is similar to how I got it to work for the resource images. Can you see where Im going wrong??

    Many thanks

    Jules
    Last edited by jules_; 2009-09-11 at 16:04. Reason: comments

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

    Re: getting byte array from image

    OK... three problems...

    Quote Originally Posted by jules_ View Post
    review0 = Image.createImage(is);
    This reads data from the stream, so there won't be any left for you to read.

    Quote Originally Posted by jules_ View Post
    imageDataLength = is.available();
    Does this always return zero? That might be because: this always returns zero. Check the JavaDocs. available() doesn't return the length of a file; that's not what it's for. Be very careful when using available(). You can be tricked because, in some cases, on some devices, it might seem to do what you want.

    Quote Originally Posted by jules_ View Post
    is.read(imgData1);
    This is another InputStream thing to take care with. Check the JavaDocs, see the phrase "Reads some number of bytes...". That means, it doesn't always read the number of bytes you expect. You need to check the return value, and see how much actually got read. (The skip() method as a similar gotcha.)

    Try this instead:

    PHP Code:
    conn = (FileConnectionConnector.open(filePaths[storedImages[0]],Connector.READ);
    // get the size of the file
    final int fileSize = (int) conn.fileSize();
    // create a big enough buffer
    byte[] imgData = new byte[fileSize];
    // open the stream
    is conn.openInputStream();
    // ...don't call createImage() here, or there will be no more data to read next
    // this will track the number of bytes we've read so far
    int bytesRead 0;
    // we keep reading, until we've read everything
    while (bytesRead fileSize) {
        
    bytesRead += is.read(imgDatabytesReadfileSize bytesRead);

    Graham.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    41

    Thumbs up Re: getting byte array from image

    Hi,

    this now works perfectly. You were right, it was the available() method which was causing problems. Thank you very much for your help.

    Jules

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    34

    Re: getting byte array from image

    Graham you are great.
    Last edited by angry_boy; 2009-10-11 at 14:40.
    dont lose hope

Similar Threads

  1. Image Problem
    By Spider_Eg in forum Mobile Java General
    Replies: 2
    Last Post: 2007-01-30, 13:52
  2. Question about Images in byte array format
    By wise-owl in forum Mobile Java Media (Graphics & Sounds)
    Replies: 2
    Last Post: 2006-11-06, 11:14
  3. Nokia Image Converter
    By davidpurdie in forum General Development Questions
    Replies: 0
    Last Post: 2004-02-18, 15:31
  4. Loading Image data from 'raw' bytes
    By LongSteve in forum Mobile Java General
    Replies: 2
    Last Post: 2002-11-20, 17:38
  5. Loading images from byte array on 6130i
    By enlightment in forum Mobile Java General
    Replies: 2
    Last Post: 2002-06-05, 19:44

Posting Permissions

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