本例的代码取自我写的上阳图像处理演示程序(ImageAlbum),
大家可以下载(包括所有源代码)看看实际效果
http://blog.csdn.net/the3gwireless/archive/2006/04/27/679608.aspx
/*
*
*
* 图像反转的方法
*/
public static Image reverseRGB(Image image) {
// System.out.println("reverseRGB IN");
int width = image.getWidth();
int height = image.getHeight();
// 获得图像的ARGB数据,存储在rawInt里
int[] raw = null;
try {
raw = new int[width * height];
image.getRGB(raw, 0, width, 0, 0, width, height);
int len = raw.length;
// 开始循环,获得图像里每个像素的颜色,然后处理
for (int i = 0; i < len; i++) {
// 获得像素的颜色
int color = raw[i];
// System.out.println(Integer.toHexString(color));
// 获得alpha
int alpha = 0xFF;
// System.out.println(Integer.toHexString(alpha));
// 获得红色
int red = (color & 0x00FF0000) >> 16;
// System.out.println(Integer.toHexString(red));
// 获得绿色
int green = (color & 0x0000FF00) >> 8;
// System.out.println(Integer.toHexString(green));
// 获得蓝色
int blue = (color & 0x000000FF);
// System.out.println(Integer.toHexString(blue));
// 翻转颜色
red = 0xFF - red;
green = 0xFF - green;
blue = 0xFF - blue;
// 生成新颜色
// System.out.println(Integer.toHexString((alpha << 24)+ (red <<
// 16)+(green << 8)));
color = (alpha << 24) + (red << 16) + (green << 8) + blue;
// System.out.println(Integer.toHexString(color));
raw[i] = color;
}
// System.out.println("blendRGB OUT");
return Image.createRGBImage(raw, width, height, true);
} catch (Error e) {
// e.printStackTrace();
ImageAlbum.showAlert("图像尺寸太大,不能完成此操作.");
return image;
} catch (Exception e) {
e.printStackTrace();
return image;
} finally {
raw = null;
}
}