/*
* http://blog.csdn.net/the3gwireless
*
* 图像灰度显示的方法1
*/
public static Image grayRGB(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));
// 将红黄蓝转换成灰度值,网上找到的算法
int gray = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
// System.out.println(gray);
red = gray;
green = gray;
blue = gray;
// 生成新颜色
// 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;
}
}

/*
*
*
* 图像灰度显示的方法2
*/
public static Image grayRGB2(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));
// 将红黄蓝转换成灰度值,网上找到的算法
int max = 0;
if (red > green) {
max = red;
} else {
max = green;
}
if (max > blue) {
} else {
max = blue;
}
// System.out.println(gray);
red = max;
green = max;
blue = max;
// 生成新颜色
// 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;
}
}