“image-(getGraphics)->graphic-(setClip, 对每个象素getColor)->原始byte[]”
--- rypan
“要是在 J2ME 的话, 看来只有 Image.getRGB() 能帮上:
还有做int[] -> byte[] 的动作, 可以考虑用 ByteArrayOutputStream
+ DataOutputStream, 把 int[] 用 dos.writeInt 的方法写在 Byte array上.
当然可以用最老土的方法, 不知道那种快:
byteArray[i] = intArray[j] & 0xFF;
byteArray[i+1] = (intArray[j] >> 8)& 0xFF;
byteArray[i+2] = (intArray[j] >> 16)& 0xFF;
byteArray[i+3] = (intArray[j] >> 24)& 0xFF;”
--- wapeter
“
public static byte[] getByteArray(Image image)
{
int raw[] = new int[image.getWidth() * image.getHeight()];
image.getRGB(raw, 0, image.getWidth(), 0, 0,
image.getWidth(), image.getHeight());
byte rawByte[] = new byte[image.getWidth() * image.getHeight() * 4];
int n = 0;
for(int i = 0; i < raw.length; i++)
{
int ARGB = raw[i];
int a = (ARGB & 0xff000000) >> 24;
int r = (ARGB & 0xff0000) >> 16;
int g = (ARGB & 0xff00) >> 8;
int 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;
}
上面代码进一步的解释:
第一步:
如何获得image对象的int数组呢?这个就简单了可以通过获得RGB数组就可以。Image对象中有直接的getRGB方法,不过这里的参数的位置和J2SE中不太一样:
javax.microedition.lcdui.Image.getRGB(int[] rgbData, int offset, int scanlength, int x, int y, int width, int height)
第一个是目标数组,第二个是偏移量,第三个是扫描的长度,后两个是起始的位置,最后两个是要取得的宽度和高度。其中扫描的长度一般大于等于获取的宽度。
第二步,把得到的Int数组再生成byte数组。
“ byteArray[i+0] = (rgbArray[i/4] >> 24);
byteArray[i+1] = (rgbArray[i/4] >> 16) & 0x000000FF;
byteArray[i+2] = (rgbArray[i/4] >> 8) & 0x000000FF;
byteArray[i+3] = rgbArray[i/4] & 0x000000FF;
”
第一句,alpha channel;
第二句,red channel;
第三句,green channel;
第四句,blue channel。
为什么有这些东西呢?
我们从一篇文章《介绍MIDP2.0新特性Alpha混合》摘要几句:
[quote]
在MIDP2.0中新增了Alpha混合特性...MIDP2.0 java doc中关于Alpha Processing的说明:在可修改图片中的每个像素都必须是完全模糊的,在不可修改图片中的每个像素可以是完全透明的,完全模糊的或者介于两者之间的,也就是半透明...
[/quote]
所以有了上面的Alpha channel。
[quote]
数组中的数值形式为0xAARRGGBB,其中AA代表透明度,后面的代表颜色值。
[/quote]
也就是说,AA就是Alpha,RR就是Red,GG就是Green,BB就是Blue。
还有一句话概括的:
[quote]444,表示图形格式,好像Nokia S40的机器都是采用444格式表示RGB颜色的。就是红,绿,蓝各用4位表示,至于可以表示透明色ARGB的4444格式,应该是机器硬件实现的。[/quote]”
---zhengyun
|