/**
* 将pixels[]里的数据,生成一张透明图片,图片宽为w,高为h
* 这里只能用在透明背景图
*/
public static Image drawPixels(int[] pixels, int w, int h) {
if (pixels != null) {
int temp = (0 << 24) | 0x00ffffff;
for (int i = 0; i < pixels.length; i++) {
if (pixels[i] == pixels[0]) {
pixels[i] &= temp;
}
}
}
Image image = Image.createRGBImage(pixels, w, h, true);
pixels = null;
return image;
}
/*
*透明背景 图片镜像特效
*/
public static Image effect_mirror(Image src) {
int srcW = src.getWidth();
int srcH = src.getHeight();
int[] srcPixels = getPixels(src);
int len;
int temp;
for (int i = 0; i < srcH; i++) {
len = (i + 1) * srcW;
for (int ii = 0; ii < srcW / 2; ii++) {
temp = srcPixels[i * srcW + ii];
srcPixels[i * srcW + ii] = srcPixels[len - 1 - ii];
srcPixels[len - 1 - ii] = temp;
}
}
return drawPixels(srcPixels, srcW, srcH);
}
/** 透明背景图片放大缩小函数 */
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] = 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 = drawPixels(desBuf, desW, desH);
return desImg;
}
/**
* 图片剪切,cut_xpos,cut_ypos 切割框的起始位置坐标,cut_width,cut_height 切割框的宽与高
*/
public static Image ClipImage(Image src, int cut_xpos, int cut_ypos, int cut_width, int cut_height) {
int srcW = src.getWidth();
int srcH = src.getHeight();
int[] srcPixels = getPixels(src);
int[] desPixels = new int[cut_width * cut_height];
int num = 0;
for (int i = cut_ypos; i < srcH; i++) {
if (i < cut_height + cut_ypos) {
for (int ii = cut_xpos; ii < srcW; ii++) {
if (ii < cut_width + cut_xpos) {
desPixels[num] = srcPixels[i * srcW + ii];
num++;
}
}
}
}
return drawPixels(desPixels, cut_width, cut_height);
}