Skip to main content

Obtaining byte[] from Image

1 reply [Last post]
kawaiimomo
Offline
Joined: 2008-07-10

Hi all,

first of all, sorry because this is probably kinda off-topic. I have an Image and I want to get the byte[] to send it to a Servlet. The only method I found is Image.getRGB() which returns and int[] (ARGB). Now, how I can convert this int[] into byte[]? I don't care about alpha channel.

I tried this:

<br />
int j=0;<br />
int[] dat = imgPreview.getRGB();</p>
<p>byte[] data = new byte[dat.length*3];   //only RGB</p>
<p>for (int i=0;i> 16);<br />
	data[j++] = (byte)((dat[i] 	& 0x0000FF00) >> 8);<br />
	data[j++] = (byte)((dat[i] 	& 0x000000FF));<br />
}<br />

And I don't see this working as expected.

On the other hand, I think this int[] is raw (without format) ARGB, so it wouldn't be possible to create the image again using Image.createImage(byte[], offset, len) if the byte[] were created successfully.

Why all this?
I'm taking a snapshot and saving as an Image instead of byte[] because I need to show the image in one screen and rescaled in another one. So I thought it would be better to store the Image instead of the byte[] (which was my previous choice, but then you must createImage 2 times.

And when I need to send the image to the server, I want to use the byte[]. Any idea on how to obtain it? Or it's just better to store the byte[] and createImage everytime is needed?

Thanks in advance.

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 because this is probably kinda off-topic. I
> have an Image and I want to get the byte[] to send it to a Servlet.
> The only method I found is Image.getRGB() which returns and int[]
> (ARGB). Now, how I can convert this int[] into byte[]? I don't care
> about alpha channel.

The ARGB is encoded into the int as AARRGGBB byte per channel. We use
this format since its exposed internally in MIDP rather than a byte
array.

> I tried this:
> [code]
> int j=0;
> int[] dat = imgPreview.getRGB();
>
> byte[] data = new byte[dat.length*3]; //only RGB
>
> for (int i=0;i > data[j++] = (byte)((dat[i] & 0x00FF0000) >> 16);
> data[j++] = (byte)((dat[i] & 0x0000FF00) >> 8);
> data[j++] = (byte)((dat[i] & 0x000000FF));
> }
> [/code]

Your code looks generally in the right direction, this works for me:
int r = col >> 16 & 0xff;
int g = col >> 8 & 0xff;
int b = col & 0xff;

> On the other hand, I think this int[] is raw (without format) ARGB,
> so it wouldn't be possible to create the image again using
> Image.createImage(byte[], offset, len) if the byte[] were created
> successfully.

It is raw. If you are looking for a byte array of an image you will
need to encode it which isn't supported by plain MIDP.
Image represents raw memory data.

Thanks.

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

[att1.html]