Java实现图片对比功能
之前用按键精灵写过一些游戏辅助,里面有个函数叫FindPic,就上在屏幕范围查找给定的一张图片,返回查找到的坐标位置。
现在,Java来实现这个函数类似的功能。
算法描述:
屏幕截图,得到图A,(查找的目标图片为图B);
遍历图A的像素点,根据图B的尺寸,得到图B四个角映射到图A上的四个点;
得到的四个点与图B的四个角像素点的值比较。如果四个点一样,执行步骤4;否则,回到步骤2继续;
进一步对比,将映射范围内的全部点与图B全部的点比较。如果全部一样,则说明图片已找到;否则,回到步骤2继续;
这里,像素之间的比较是通过BufferedImage对象获取每个像素的RGB值来比较的。如下,将BufferedImage转换为int二维数组:
/**
*根据BufferedImage获取图片RGB数组
*@parambfImage
*@return
*/
publicstaticint[][]getImageGRB(BufferedImagebfImage){
intwidth=bfImage.getWidth();
intheight=bfImage.getHeight();
int[][]result=newint[height][width];
for(inth=0;h<height;h++){
for(intw=0;w<width;w++){
//使用getRGB(w,h)获取该点的颜色值是ARGB,而在实际应用中使用的是RGB,所以需要将ARGB转化成RGB,即bufImg.getRGB(w,h)&0xFFFFFF。
result[h][w]=bfImage.getRGB(w,h)&0xFFFFFF;
}
}
returnresult;
}
比较两个像素点的RGB值是否相同,是通过异或操作比较的(据说比==效率更高),如果异或操作后得到的值为0,说明两个像素点的RGB一样,否则不一样。
下面附上算法完整java代码:
packagecom.jebysun.test.imagefind;
importjava.awt.AWTException;
importjava.awt.Rectangle;
importjava.awt.Robot;
importjava.awt.Toolkit;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjavax.imageio.ImageIO;
/**
*屏幕上查找指定图片
*@authorJebySun
*@date2014-09-13
*@websitehttp://www.jebysun.com
*/
publicclassImageFindDemo{
BufferedImagescreenShotImage;//屏幕截图
BufferedImagekeyImage;//查找目标图片
intscrShotImgWidth;//屏幕截图宽度
intscrShotImgHeight;//屏幕截图高度
intkeyImgWidth;//查找目标图片宽度
intkeyImgHeight;//查找目标图片高度
int[][]screenShotImageRGBData;//屏幕截图RGB数据
int[][]keyImageRGBData;//查找目标图片RGB数据
int[][][]findImgData;//查找结果,目标图标位于屏幕截图上的坐标数据
publicImageFindDemo(StringkeyImagePath){
screenShotImage=this.getFullScreenShot();
keyImage=this.getBfImageFromPath(keyImagePath);
screenShotImageRGBData=this.getImageGRB(screenShotImage);
keyImageRGBData=this.getImageGRB(keyImage);
scrShotImgWidth=screenShotImage.getWidth();
scrShotImgHeight=screenShotImage.getHeight();
keyImgWidth=keyImage.getWidth();
keyImgHeight=keyImage.getHeight();
//开始查找
this.findImage();
}
/**
*全屏截图
*@return返回BufferedImage
*/
publicBufferedImagegetFullScreenShot(){
BufferedImagebfImage=null;
intwidth=(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
intheight=(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
try{
Robotrobot=newRobot();
bfImage=robot.createScreenCapture(newRectangle(0,0,width,height));
}catch(AWTExceptione){
e.printStackTrace();
}
returnbfImage;
}
/**
*从本地文件读取目标图片
*@paramkeyImagePath-图片绝对路径
*@return本地图片的BufferedImage对象
*/
publicBufferedImagegetBfImageFromPath(StringkeyImagePath){
BufferedImagebfImage=null;
try{
bfImage=ImageIO.read(newFile(keyImagePath));
}catch(IOExceptione){
e.printStackTrace();
}
returnbfImage;
}
/**
*根据BufferedImage获取图片RGB数组
*@parambfImage
*@return
*/
publicint[][]getImageGRB(BufferedImagebfImage){
intwidth=bfImage.getWidth();
intheight=bfImage.getHeight();
int[][]result=newint[height][width];
for(inth=0;h<height;h++){
for(intw=0;w<width;w++){
//使用getRGB(w,h)获取该点的颜色值是ARGB,而在实际应用中使用的是RGB,所以需要将ARGB转化成RGB,即bufImg.getRGB(w,h)&0xFFFFFF。
result[h][w]=bfImage.getRGB(w,h)&0xFFFFFF;
}
}
returnresult;
}
/**
*查找图片
*/
publicvoidfindImage(){
findImgData=newint[keyImgHeight][keyImgWidth][2];
//遍历屏幕截图像素点数据
for(inty=0;y<scrShotImgHeight-keyImgHeight;y++){
for(intx=0;x<scrShotImgWidth-keyImgWidth;x++){
//根据目标图的尺寸,得到目标图四个角映射到屏幕截图上的四个点,
//判断截图上对应的四个点与图B的四个角像素点的值是否相同,
//如果相同就将屏幕截图上映射范围内的所有的点与目标图的所有的点进行比较。
if((keyImageRGBData[0][0]^screenShotImageRGBData[y][x])==0
&&(keyImageRGBData[0][keyImgWidth-1]^screenShotImageRGBData[y][x+keyImgWidth-1])==0
&&(keyImageRGBData[keyImgHeight-1][keyImgWidth-1]^screenShotImageRGBData[y+keyImgHeight-1][x+keyImgWidth-1])==0
&&(keyImageRGBData[keyImgHeight-1][0]^screenShotImageRGBData[y+keyImgHeight-1][x])==0){
booleanisFinded=isMatchAll(y,x);
//如果比较结果完全相同,则说明图片找到,填充查找到的位置坐标数据到查找结果数组。
if(isFinded){
for(inth=0;h<keyImgHeight;h++){
for(intw=0;w<keyImgWidth;w++){
findImgData[h][w][0]=y+h;
findImgData[h][w][1]=x+w;
}
}
return;
}
}
}
}
}
/**
*判断屏幕截图上目标图映射范围内的全部点是否全部和小图的点一一对应。
*@paramy-与目标图左上角像素点想匹配的屏幕截图y坐标
*@paramx-与目标图左上角像素点想匹配的屏幕截图x坐标
*@return
*/
publicbooleanisMatchAll(inty,intx){
intbiggerY=0;
intbiggerX=0;
intxor=0;
for(intsmallerY=0;smallerY<keyImgHeight;smallerY++){
biggerY=y+smallerY;
for(intsmallerX=0;smallerX<keyImgWidth;smallerX++){
biggerX=x+smallerX;
if(biggerY>=scrShotImgHeight||biggerX>=scrShotImgWidth){
returnfalse;
}
xor=keyImageRGBData[smallerY][smallerX]^screenShotImageRGBData[biggerY][biggerX];
if(xor!=0){
returnfalse;
}
}
biggerX=x;
}
returntrue;
}
/**
*输出查找到的坐标数据
*/
privatevoidprintFindData(){
for(inty=0;y<keyImgHeight;y++){
for(intx=0;x<keyImgWidth;x++){
System.out.print("("+this.findImgData[y][x][0]+","+this.findImgData[y][x][1]+")");
}
System.out.println();
}
}
publicstaticvoidmain(String[]args){
StringkeyImagePath="D:/key.png";
ImageFindDemodemo=newImageFindDemo(keyImagePath);
demo.printFindData();
}
}
这种算法是精确比较,只要有一个像素点有差异,就会找不到图片。当然,如果想指定一个比较的精确度,我也有个思路,就是在算法步骤4比较映射范围内全部像素点的时候做个统计,如果90%的点都相同,那就是说精确度是0.9。
另外,可能还要考虑效率问题,不过,我在我的应用场景中并不太在意效率。如果有朋友看到这篇文章,对这个话题有更好的想法,请留言。