原理很简单:主要是通过屏幕截图获取两张图片,然后通过将两组图片一个像素一个像素的进行比较,从而找出不同之处~
附上使用效果:
关键代码:
[java] public void getPic() {
locationP = this.getLocation();
this.setLocation(1400, 800);
try {
Thread.sleep(200);
} catch (InterruptedException e1) {
e1.printStackTrace();
}//这里得到三个BufferedImage对象。bi1为左边的图片,bi2为右边的图片,bi3是bi1的副本用于修改。
bi1 = r.createScreenCapture(new Rectangle(left1, top1, width, height));
bi2 = r.createScreenCapture(new Rectangle(left2, top2, width, height));
bi3= r.createScreenCapture(new Rectangle(left1, top1, width, height));
/* 更改图3实际上就是更改图1.
bi3=bi1.getSubimage(0, 0, bi1.getWidth(), bi1.getHeight());*/
for (int i = 5; i < bi1.getHeight() - 5; i += 1) {
for (int j = 5; j < bi1.getWidth() - 5; j += 1) {
if (bi3.getRGB(j, i) != bi2.getRGB(j, i)) {
bi3.setRGB(j, i, Color.BLACK.getRGB());
}
}
}//这个循环就是找出颜色不同的像素并在bi3上进行颜色修改,这里我们将不同颜色的点设置为黑色
if (model == 1) {
p.setIcon(new ImageIcon(bi3));
}else if(model==2){
flag=true;
}//这里提供了两种模式的切换
this.setLocation(locationP);
}
public void getPic() {
locationP = this.getLocation();
this.setLocation(1400, 800);
try {
Thread.sleep(200);
} catch (InterruptedException e1) {
e1.printStackTrace();
}//这里得到三个BufferedImage对象。bi1为左边的图片,bi2为右边的图片,bi3是bi1的副本用于修改。
bi1 = r.createScreenCapture(new Rectangle(left1, top1, width, height));
bi2 = r.createScreenCapture(new Rectangle(left2, top2, width, height));
bi3= r.createScreenCapture(new Rectangle(left1, top1, width, height));
/* 更改图3实际上就是更改图1.
bi3=bi1.getSubimage(0, 0, bi1.getWidth(), bi1.getHeight());*/
for (int i = 5; i < bi1.getHeight() - 5; i += 1) {
for (int j = 5; j < bi1.getWidth() - 5; j += 1) {
if (bi3.getRGB(j, i) != bi2.getRGB(j, i)) {
bi3.setRGB(j, i, Color.BLACK.getRGB());
}
}
}//这个循环就是找出颜色不同的像素并在bi3上进行颜色修改,这里我们将不同颜色的点设置为黑色
if (model == 1) {
p.setIcon(new ImageIcon(bi3));
}else if(model==2){
flag=true;
}//这里提供了两种模式的切换
this.setLocation(locationP);
}
附上可以运行的jar文件和源码下载地址:
http://up.2cto.com/2012/0218/20120218092717789.zip
摘自 EaSy的专栏