十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
SPAN id=imgObj
成都创新互联公司专注骨干网络服务器租用10年,服务更有保障!服务器租用,南充服务器托管 成都服务器租用,成都服务器托管,骨干网络带宽,享受低延迟,高速访问。灵活、实现低成本的共享或公网数据中心高速带宽的专属高性能服务器。
onmouseleave='doTrans("gray","imgObj")'
style="FILTER:progid:DXImageTransform.Microsoft.Fade(Overlap=1.00); WIDTH: 1px"
onmouseenter='doTrans("","imgObj")'
IMG id=oImg style="FILTER: gray" src="readme.gif"
/SPAN
另一个图片要有不同的id:如
SPAN id=img
onmouseleave='doTrans("gray","img")'
style="FILTER:progid:DXImageTransform.Microsoft.Fade(Overlap=1.00); WIDTH: 1px"
onmouseenter='doTrans("","img")'
IMG id=oImg style="FILTER: gray" src="readme.gif"
/SPAN
function doTrans(filterCode,id)
{
id.filters[0].apply();
oImg.style.filter = filterCode
id.filters[0].play();
}
多传递个参数,标示图片!
代码如下:
package com.baidu.demo019;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class App extends JFrame {
private static final long serialVersionUID = 1L;
public App() {
this.setSize(500, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Box box = Box.createVerticalBox();
this.add(box);
// 源图像路径
String imageFile = "images/demo019.bmp";
// 源图像
BufferedImage image1 = getImage(imageFile);
JLabel label1 = new JLabel(new ImageIcon(image1));
JPanel panel1 = new JPanel(new BorderLayout());
panel1.add(label1);
box.add(panel1);
// 转换后的图像
Image image2 = translateImage(image1);
JLabel label2 = new JLabel(new ImageIcon(image2));
JPanel panel2 = new JPanel(new BorderLayout());
panel2.add(label2);
box.add(panel2);
}
BufferedImage getImage(String imageFile) {
BufferedImage image = null;
try {
image = ImageIO.read(new File(imageFile));
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
// 转换图像 黑底白字转换为白底黑字,白色设置为透明色
private Image translateImage(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
BufferedImage target = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
for (int i = 0; i width; i++) {
for (int j = 0; j height; j++) {
int val = image.getRGB(i, j);
int red = (val 16) 0xff;
int green = (val 8) 0xff;
int blue = val 0xff;
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
int alpha = 0xff;
if ((red + green + blue) / 3 = 0xff) {
alpha = 0x00;
}
int pixel = (alpha 24) | (red 16) | (green 8) | (blue);
target.setRGB(i, j, pixel);
}
}
return target;
}
public static void main(String[] args) {
new App().setVisible(true);
}
}
运行结果:
BufferedImage 把图片转换成二维数组, 然后把指定颜色,修改成你想要的颜色。
需要注意 背景色和其他颜色交叉的地方, 需要有一个容差的范围. 让指定的颜色在某个范围内
或者用算法处理边缘部分的颜色.
//功能已实现
//没什么好注释的,都是API,请君自行查看
import java.awt.Image;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImageUtil {
public static void main(String[] args) {
File file=new File("C:\\Documents and Settings\\Administrator\\桌面\\cd.jpg");
changeImge(file);
}
/**
* * 转换图片 * *
*/
public static void changeImge(File img) {
try {
Image image = ImageIO.read(img);
int srcH = image.getHeight(null);
int srcW = image.getWidth(null);
BufferedImage bufferedImage = new BufferedImage(srcW, srcH,BufferedImage.TYPE_3BYTE_BGR);
bufferedImage.getGraphics().drawImage(image, 0,0, srcW, srcH, null);
bufferedImage=new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY),null).filter(bufferedImage,null);
FileOutputStream fos = new FileOutputStream(img);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
encoder.encode(bufferedImage);
fos.close();
// System.out.println("转换成功...");
} catch (IOException e) {
e.printStackTrace();
throw new IllegalStateException("图片转换出错!", e);
}
}
}