大二的時候寫的一款小軟件。
原理很簡單:主要是通過屏幕截圖獲取兩張圖片,然後通過將兩組圖片一個像素一個像素的進行比較,從而找出不同之處~
附上使用效果:
關鍵代碼:
[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.aiwalls.com/2012/0218/20120218092717789.zip
摘自 EaSy的專欄