📄 imageeffect.java
字号:
/**
*
* @author Sam Huang
*
*/
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/*
*
* 这个类中有关图像翻转的方法很愚笨,因为是我自己实现的,呵呵。 我想只能作为如何进行数组翻转的参考。
* 另外,由于这些方法是在J2me环境下实现的,没有从底层去解决问题,因此效率非常低,而且很容易造成内存溢出 相反的,MIDP2.0中的Image类有
* createImage(Image image,int x,int y,int width,int height,int transform)
* 使用这个方法可以很容易实现图像翻转,它的执行效率就比较高,大家可以比较一下。
* 因此,我认为,我们在做有关图像开发的时候,一定要尽量使用系统自带的方法,这样效率和稳定性都会高很多
*
*/
class ImageEffect {
/*
* blend方法由下边的作者所写,感谢他所做的工作
*
* By: Oscar Wivall Date: 2004-05-06
*
* Description: Create cool effects by fading in/out the images in MIDP 2.0.
* This midlet shows how to change the alpha value of an Image. This MIDlet
* is tested on Z1010 and K700.
*
* This method changes the alpha value of an image array. All the pixels in
* the image contains Alpha, Red, Green, Blue (ARGB) colors where each of
* the color is a value from 0 to 255. If Alpha is 255 the pixel will be
* opaque, if its 0 the pixel is transparent. 0xFFFF0000 = 11111111 11111111
* 00000000 00000000 - this is a red opaque pixel.
*
* To get the RGB values from the array we can use the AND '&' operator.
* (11111101 & 01111110) = 01111100, only the 1:s where & = 1 will get
* through.
*
* to change 11111111 to 11111111 00000000 00000000 00000000 we can use the
* shift left operator '<<', (00000001 << 7) = 10000000 in dec (1<<7) =
* 128
*
* To change the alpha value we loop through all the pixels in the image.
*
* With the blend method its also possible to mask and dontmask specific
* colors.
*/
// raw是图像数据
public static void blend(int[] raw, int alphaValue, int maskColor,
int dontmaskColor) {
int len = raw.length;
// 开始循环,获得图像里每个像素的颜色,然后处理
for (int i = 0; i < len; i++) {
int a = 0;
int color = (raw[i] & 0x00FFFFFF); // 获得像素的颜色
if (maskColor == color) {
a = 0;
} else if (dontmaskColor == color) {
a = 255;
} else if (alphaValue > 0) {
a = alphaValue; // 设置我们希望的alpha值 0-255.
}
a = (a << 24); // 把alpha左移24位(left shift the alpha value 24 bits)
// if color = 00000000 11111111 11111111 00000000
// and alpha= 01111111 00000000 00000000 00000000
// then c+a = 01111111 11111111 11111111 00000000
// and the pixel will be blended.
color += a;
raw[i] = color;
}
}
public static void blend(int[] raw, int alphaValue) {
blend(raw, alphaValue, 0xFFFFFFFF, 0xFFFFFFFF);
}
/***************************************************************************
*
* 下边的代码是我根据上边的思路添加的对图像RGB进行处理的方法
* 我的思路是,所有的像素的RGB(比如红色值)都加一个固定大小的值,是不是整体颜色就应该平滑地改变呢(比如变红)?大家帮着分析一下
* 执行的时候我发现图片颜色的改变很奇怪,连续点击后有时造成死机,而且有些off值不起作用
* 我自己的分析是模拟器显示图像的分辨率并不支持0-255来显示,因此有些值就不起作用了
* 我没有研究过图像处理,因此可能方法不正确,希望有兴趣的朋友帮忙改进
*/
public static Image blendRGB(Image image, int off, int rgb) {
// System.out.println("blendRGB IN");
int width = image.getWidth();
int height = image.getHeight();
// 获得图像的ARGB数据,存储在rawInt里
int[] raw = new int[width * height];
image.getRGB(raw, 0, width, 0, 0, width, height);
int len = raw.length;
// 开始循环,获得图像里每个像素的颜色,然后处理
try {
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));
// 根据标志位处理颜色
if (rgb == 0) {
red += off;
red %= 255;
// System.out.println(Integer.toHexString(red));
} else if (rgb == 1) {
green += off;
green %= 255;
} else if (rgb == 2) {
blue += off;
blue %= 255;
}
// 生成新颜色
// 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;
}
}
/***************************************************************************
*
* 下边的代码是我根据上边的思路添加的对图像RGB进行处理的方法
* 我的思路是,所有的像素的RGB(比如红色值)都加一个固定大小的值,是不是整体颜色就应该平滑地改变呢(比如变红)?大家帮着分析一下
* 执行的时候我发现图片颜色的改变很奇怪,连续点击后有时造成死机,而且有些off值不起作用
* 我自己的分析是模拟器显示图像的分辨率并不支持0-255来显示,因此有些值就不起作用了
* 我没有研究过图像处理,因此可能方法不正确,希望有兴趣的朋友帮忙改进
*/
public static void blendRGB(int[] raw, int off, int rgb) {
// System.out.println("blendRGB IN");
if (raw == null)
return;
int len = raw.length;
// 开始循环,获得图像里每个像素的颜色,然后处理
try {
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));
// 根据标志位处理颜色
if (rgb == 0) {
red += off;
red %= 255;
// System.out.println(Integer.toHexString(red));
} else if (rgb == 1) {
green += off;
green %= 255;
} else if (rgb == 2) {
blue += off;
blue %= 255;
}
// 生成新颜色
color = (alpha << 24) + (red << 16) + (green << 8) + blue;
// System.out.println(Integer.toHexString(color));
raw[i] = color;
}
// System.out.println("blendRGB OUT");
} catch (Exception e) {
e.printStackTrace();
} finally {
raw = null;
}
}
/*
*
*
* 图像反转的方法
*/
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;
}
}
/*
*
*
* 图像灰度显示的方法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;
}
}
/*
*
*
* 调节图像明暗的方法
*/
public static Image lightenessRGB(Image image, int start, int end) {
// 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 = start + end * red / 255;
green = start + end * green / 255;
blue = start + end * blue / 255;
// 生成新颜色
// System.out.println(Integer.toHexString((alpha << 24)+ (red <<
// 16)+(green << 8)));
color = (alpha << 24) + (red << 16) + (green << 8) + blue;
raw[i] = color;
}
// System.out.println("blendRGB OUT");
return Image.createRGBImage(raw, width, height, true);
} catch (Error e) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -