1.图像缩放
1.1 使用midp2.0的getRGB()函数
public static Image ZoomImage(Image src, int desW, int desH) {
Image desImg = null;
int srcW = src.getWidth(); // 原始图像宽
int srcH = src.getHeight(); // 原始图像高
int[] srcBuf = new int[srcW * srcH]; // 原始图片像素信息缓存
src.getRGB(srcBuf, 0, srcW, 0, 0, srcW, srcH);
// 计算插值表
int[] tabY = new int[desH];
int[] tabX = new int[desW];
int sb = 0;
int db = 0;
int tems = 0;
int temd = 0;
int distance = srcH > desH ? srcH : desH;
for (int i = 0; i <= distance; i++) { /* 垂直方向 */
tabY[db] = sb;
tems += srcH;
temd += desH;
if (tems > distance) {
tems -= distance;
sb++;
}
if (temd > distance) {
temd -= distance;
db++;
}
}
sb = 0;
db = 0;
tems = 0;
temd = 0;
distance = srcW > desW ? srcW : desW;
for (int i = 0; i <= distance; i++) { /* 水平方向 */
tabX[db] = (short) sb;
tems += srcW;
temd += desW;
if (tems > distance) {
tems -= distance;
sb++;
}
if (temd > distance) {
temd -= distance;
db++;
}
}
// 生成放大缩小后图形像素buf
int[] desBuf = new int[desW * desH];
int dx = 0;
int dy = 0;
int sy = 0;
int oldy = -1;
for (int i = 0; i < desH; i++) {
if (oldy == tabY[i]) {
System.arraycopy(desBuf, dy - desW, desBuf, dy, desW);
} else {
dx = 0;
for (int j = 0; j < desW; j++) {
desBuf[dy + dx] = srcBuf[sy + tabX[j]];
dx++;
}
sy += (tabY[i] - oldy) * srcW;
}
oldy = tabY[i];
dy += desW;
}
// 生成图片
desImg = Image.createRGBImage(desBuf, desW, desH, false);
return desImg;
}
1.2 midp1.0下可用的缩放函数
public static Image scaleImage (Image src, int dstW, int dstH) {
int srcW = src.getWidth();
int srcH = src.getHeight();
Image tmp = Image.createImage(dstW, srcH);
Graphics g = tmp.getGraphics();
int delta = (srcW << 16) / dstW;
int pos = delta/2;
for (int x = 0; x < dstW; x++) {
g.setClip(x, 0, 1, srcH);
g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
pos += delta;
}
Image dst = Image.createImage(dstW, dstH);
g = dst.getGraphics();
delta = (srcH << 16) / dstH;
pos = delta/2;
for (int y = 0; y < dstH; y++) {
g.setClip(0, y, dstW, 1);
g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
pos += delta;
}
return dst;
}
}
2.其他图像的处理,如图像的颜色/翻转
下面提供了源代码, 点击这里下载