Convert byte array to an Image

161 views
Skip to first unread message

Diamond

unread,
Mar 17, 2015, 6:39:11 AM3/17/15
to codenameone...@googlegroups.com
Hi,

I want to get an image from the json input of this format:

"Image":{"Bytes":[137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,3,140,0,0,4,157,8,2,0,0,0,132,133,69,69,0,0,0,1,115,82,71,66,0,174,206,28,233,0,0,0,4,103,65,77,65,0,0,177,143,11]}

The input is extremely longer than this, I shortened it for sample purposes.

I've tried:

byte[] data2 = (byte[]) tempImage.get("Bytes");
Image img = Image.createImage(data2, 0, data.length);

But I got ClassCastException:

java.lang.ClassCastException: java.util.ArrayList cannot be cast to [B

How do I go about this please?

Diamond.

Steve Hannah

unread,
Mar 17, 2015, 11:56:09 AM3/17/15
to codenameone...@googlegroups.com
tempImage.get("Bytes") returns an ArrayList with Byte objects.

You'll need to do something like

List bytes = (List)tempImage.get("Bytes");
byte[] data2 = bytes.toArray(new byte[bytes.size()]);

(I think that should work with autoboxing, but if it doesn't you may need to loop through the list to create your byte[] array).

Steve

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discu...@googlegroups.com.
Visit this group at http://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/353f5fab-81b0-4dfa-a240-f2cf623f19ea%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Steve Hannah
Software Developer
Codename One

Shai Almog

unread,
Mar 17, 2015, 1:56:05 PM3/17/15
to codenameone...@googlegroups.com
No it won't.
It returns an array list with double or long objects since we have no idea if the size of the variable can/should fit in a byte.

You will need to do something like:
byte[] b = new byte[bytesList.size()];
for(int iter = 0 ; iter < b.length ; iter++) {
   
Object o = bytesList.get(iter);
   
if(o instanceof Double) {
      b
= ((Double)o).byteValue();
   
} else {
      b
= ((Long)o).byteValue();
   
}
}

Its a bit painful since we don't have the java.lang.Number base class for primitive numeric wrappers (ideally we'll add it at some point).

Diamond

unread,
Mar 17, 2015, 2:20:50 PM3/17/15
to codenameone...@googlegroups.com
Thanks Steve and Shai.

I got it to work by combining your code with Steve's.

List bytesList = (List) tempImage.get("Bytes");
     byte[] bytes = new byte[bytesList.size()];
     for (int iter = 0; iter < bytes.length; iter++) {
         Object o = bytesList.get(iter);
         if (o instanceof Double) {
            bytes[iter] = ((Double) o).byteValue();
         } else {
            bytes[iter] = ((Long) o).byteValue();
         }
     }


Is there a way to cache the image to reduce RAM workload?

 

Shai Almog

unread,
Mar 18, 2015, 1:25:06 AM3/18/15
to codenameone...@googlegroups.com
I doubt it since you have byte data. If you had a unique image ID you could just use that as the cache key and skip the decoding.

Diamond Mubaarak

unread,
Mar 18, 2015, 12:14:52 PM3/18/15
to codenameone...@googlegroups.com
Hi Shai,

byteValue() is giving me error whenever I do clean build but run without cleaning. I also think its the reason the build server is returning an error.

Shai Almog

unread,
Mar 19, 2015, 2:33:37 AM3/19/15
to codenameone...@googlegroups.com
Hi,
on which platform and what error?
Which IDE?

Diamond Mubaarak

unread,
Mar 19, 2015, 2:38:27 AM3/19/15
to codenameone...@googlegroups.com
Netbeans 8.0.2 and Building for Android.

Now the error shows while sending the build on the IDE Log.

Shai Almog

unread,
Mar 19, 2015, 11:38:37 AM3/19/15
to codenameone...@googlegroups.com
Damn, seems to be a problem with the Long object... We'll fix it for the next update.
In the meantime you can just comment out the long code and rely on doubles which should work.
Reply all
Reply to author
Forward
0 new messages