Skip to main content

image serialization / deserialisation

2 replies [Last post]
Anonymous

Hi all,

First of all : sorry if this has been asked over and over again ... I just
couldn't find anything on this subject on the LWUIT list.

I'm trying to serialize / deserialize an Image and I'm failing when using
the method I traditionally used in LCDUI.

Here's the code I've been using to test this :

/* IMAGE SERIALIZATION / DESERIALIZATION TEST /*
*
*/

Image originalImg = Image.createImage("/mainMenuIcon0.png");
byte[] bytes = Tools.getImageByteArray(originalImg); //the purpose
her eis to be able to save the byte[] into the RMS
Image deserializedImg = Image.createImage(bytes, 0, bytes.length);

Form imgTestForm = new Form("IMAGE SERIALIZATION TEST");
Label originalLabel = new Label(originalImg);
Label deselabel = new Label(deserializedImg);
imgTestForm.setLayout(new BorderLayout());
imgTestForm.addComponent(BorderLayout.NORTH, originalLabel);
imgTestForm.addComponent(BorderLayout.SOUTH, deselabel);

imgTestForm.show();

/*
* END OF IMAGE SERIALIZATION / DESERIALIZATION TEST
*/

And here is my getImageByteArray method (something adapted from a forum
Nokia example as far as I remember) :

public static byte[] getImageByteArray(Image image){

int[]raw = image.getRGB();

byte[]rawByte = new byte[image.getWidth() * image.getHeight() *
4];// ARGB
int a, r, g, b;
int n = 0;

// loop through the int-array and get the 4 bytes of each value to
the byte-array.
// since java isn't supporting unsigned bytes we need to store the
ARGB values from -128 to 127
// where -128 = 128, -127 = 129 and -1 = 255
for(int i=0; i> 24;
r = (ARGB & 0xFF0000) >> 16;
g = (ARGB & 0xFF00) >> 8;
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;
}

The test throws an illegal argument exception :

java.lang.IllegalArgumentException:
at javax.microedition.lcdui.ImmutableImage.(Image.java:906)
at javax.microedition.lcdui.Image.createImage(Image.java:367)
at com.sun.lwuit.Image.createImage(Image.java:442)

I was wondering what I was doing wrong and in what way the creation of LWUIT
images using byte[] differed from the one I available in plain old lcdui

Thanks in advance

Cheers

Alex
[att1.html]

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Shai Almog

Hi,
> First of all : sorry if this has been asked over and over again ...
> I just couldn't find anything on this subject on the LWUIT list.
>
> I'm trying to serialize / deserialize an Image and I'm failing when
> using the method I traditionally used in LCDUI.

this method won't work in LCDUI either. The createImage(byte
[],int,int) method expects a PNG file and you give it ARGB encoded
data, this is failing in MIDP... You need to do either one of the
following (not both):

1. Store the image bytes before you decode it (e.g. the bytes you
downloaded from your web server).

2. Convert the byte array from RMS back to an ARGB int array.

Shai Almog
http://lwuit.blogspot.com/

[att1.html]

Alex

OK my bad

I had forgotten a line in my test case.

On Tue, Sep 9, 2008 at 5:16 AM, Shai Almog wrote:

> Hi,
>
> First of all : sorry if this has been asked over and over again ... I just
> couldn't find anything on this subject on the LWUIT list.
>
> I'm trying to serialize / deserialize an Image and I'm failing when using
> the method I traditionally used in LCDUI.
>
>
> this method won't work in LCDUI either. The createImage(byte[],int,int)
> method expects a PNG file and you give it ARGB encoded data, this is failing
> in MIDP... You need to do either one of the following (not both):
>
> 1. Store the image bytes before you decode it (e.g. the bytes you
> downloaded from your web server).
>
> 2. Convert the byte array from RMS back to an ARGB int array.
>
> Shai Almog
> http://lwuit.blogspot.com/
>
>
[att1.html]