该方法网上可以找到,我添加了一个重载函数:
public Image getPalettedImage(String path, int[] originalColors,
int[] palettedColors)
三个参数依次为图片路径、需要改变的旧颜色值、新的颜色值,注意这2个参数都为数组,长度必须一致,这样可以一次改变多种颜色。
用法:
PalettedImage p=new PalettedImage();
Image newImage=p.getPalettedImage("/Humans121.png", new int[]{0x0000ff}, new int[]{0x00ff00});
...
g.drawImage(newImage, 0, 0, 0);
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Image;
public class PalettedImage {
public Image getPalettedImage(String path, int[] originalColors,
int[] palettedColors) {
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
is = getClass().getResourceAsStream(path);
baos = new ByteArrayOutputStream();
int ch = 0;
while ((ch = is.read()) != -1) {
baos.write(ch);
}
} catch (IOException ioe) {
System.out.println(ioe);
} finally {
try {
if (baos != null)
baos.close();
if (is != null)
is.close();
}
catch (IOException e) {
System.out.println(e);
}
}
return getPalettedImage(baos.toByteArray(), originalColors,
palettedColors);
}
/*
* 原始图片的byte数组,以及需要替换颜色的颜色值还有目标颜色值就行了。 因为可以同时替换多个颜色,所以输入参数是代表颜色的整形的数组。
* 总之,要保证原始颜色与目标颜色一一对应就好了
*/
public Image getPalettedImage(byte[] data, int[] originalColors,
int[] palettedColors) {
byte[] tempData = new byte[data.length];
System.arraycopy(data, 0, tempData, 0, data.length);
Image img = null;
int[] parameter = { 0, 0, 0 };
analyze(tempData, parameter);
for (int i = 0; i replaceColor(tempData, parameter, originalColors[i],
palettedColors[i]);
}
fillData(tempData, parameter);
try {
img = Image.createImage(tempData, 0, data.length);
} catch (Exception e) {
System.out.println("getPalettedImage && " + e.toString());
}
return img;
}
private void analyze(byte[] data, int[] para) {
int offset = 8;
int chunkLen = 0;
while (data[offset + 4] != 0x50 || data[offset + 5] != 0x4c
|| data[offset + 6] != 0x54 || data[offset + 7] != 0x45) {
chunkLen = readInt(data, offset);
offset += (4 + 4 + chunkLen + 4);
}
chunkLen = readInt(data, offset);
para[2] = chunkLen / 3;
para[0] = offset + 8;
para[1] = offset + 8 + chunkLen;
}
private int readInt(byte[] data, int offset) {
return ((data[offset] & 0xFF) | ((data[offset + 1] & 0xFF) | ((data[offset + 2] & 0xFF) }
private void replaceColor(byte[] data, int[] para, int oldColor,
int newColor) {
byte rr = (byte) ((oldColor >> 16) & 0xff);
byte gg = (byte) ((oldColor >> 8) & 0xff);
byte bb = (byte) (oldColor & 0xff);
for (int i = 0, offset = para[0], temp = 0; i if (rr == data[offset] && gg == data[offset + 1]
&& bb == data[offset + 2]) {
data[offset] = (byte) ((newColor >> 16) & 0xff);
data[offset + 1] = (byte) ((newColor >> 8) & 0xff);
data[offset + 2] = (byte) (newColor & 0xff);
break;
}
}
}
private void fillData(byte[] data, int[] para) {
int checksum = update_crc(data, para[0] - 4, para[2] * 3 + 4);
data[para[1]] = (byte) ((checksum >> 24) & 0xff);
data[para[1] + 1] = (byte) ((checksum >> 16) & 0xff);
data[para[1] + 2] = (byte) ((checksum >> 8) & 0xff);
data[para[1] + 3] = (byte) ((checksum) & 0xff);
}
private int update_crc(byte[] buf, int off, int len) {
int c = 0xffffffff;
int n, k;
int xx;
int[] crc_table = new int[256];
for (n = 0; n xx = n;
for (k = 0; k if ((xx & 1) == 1) {
xx = 0xedb88320 ^ (xx >>> 1);
} else {
xx = xx >>> 1;
}
}
crc_table[n] = xx;
}
for (n = off; n c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >>> 8);
}
return (c ^ 0xffffffff);
}
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net