当前位置: 移动技术网 > IT编程>开发语言>Java > Java实现图片对比功能

Java实现图片对比功能

2019年07月22日  | 移动技术网IT编程  | 我要评论

  之前用按键精灵写过一些游戏辅助,里面有个函数叫findpic,就上在屏幕范围查找给定的一张图片,返回查找到的坐标位置。

  现在,java来实现这个函数类似的功能。

  算法描述:

屏幕截图,得到图a,(查找的目标图片为图b);
遍历图a的像素点,根据图b的尺寸,得到图b四个角映射到图a上的四个点;
得到的四个点与图b的四个角像素点的值比较。如果四个点一样,执行步骤4;否则,回到步骤2继续;
进一步对比,将映射范围内的全部点与图b全部的点比较。如果全部一样,则说明图片已找到;否则,回到步骤2继续;
  这里,像素之间的比较是通过bufferedimage对象获取每个像素的rgb值来比较的。如下,将bufferedimage转换为int二维数组:

   /**
   * 根据bufferedimage获取图片rgb数组
   * @param bfimage
   * @return
   */
   public static int[][] getimagegrb(bufferedimage bfimage) {
     int width = bfimage.getwidth();
     int height = bfimage.getheight();
     int[][] result = new int[height][width];
     for (int h = 0; h < height; h++) {
       for (int w = 0; w < width; w++) {
         //使用getrgb(w, h)获取该点的颜色值是argb,而在实际应用中使用的是rgb,所以需要将argb转化成rgb,即bufimg.getrgb(w, h) & 0xffffff。
         result[h][w] = bfimage.getrgb(w, h) & 0xffffff;
       }
     }
     return result;
   }

   比较两个像素点的rgb值是否相同,是通过异或操作比较的(据说比==效率更高),如果异或操作后得到的值为0,说明两个像素点的rgb一样,否则不一样。

  下面附上算法完整java代码:

 package com.jebysun.test.imagefind;
 
 import java.awt.awtexception;
 import java.awt.rectangle;
 import java.awt.robot;
 import java.awt.toolkit;
 import java.awt.image.bufferedimage;
 import java.io.file;
 import java.io.ioexception;
 
 import javax.imageio.imageio;
 /**
 * 屏幕上查找指定图片
 * @author jeby sun
 * @date 2014-09-13
 * @website http://www.jebysun.com
 */
 public class imagefinddemo {
   
   bufferedimage screenshotimage;  //屏幕截图
   bufferedimage keyimage;      //查找目标图片
   
   int scrshotimgwidth;       //屏幕截图宽度
   int scrshotimgheight;       //屏幕截图高度
   
   int keyimgwidth;         //查找目标图片宽度
   int keyimgheight;         //查找目标图片高度
   
   int[][] screenshotimagergbdata;  //屏幕截图rgb数据
   int[][] keyimagergbdata;     //查找目标图片rgb数据
   
   int[][][] findimgdata;      //查找结果,目标图标位于屏幕截图上的坐标数据 
   
   
   public imagefinddemo(string keyimagepath) {
     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
   */
   public bufferedimage getfullscreenshot() {
     bufferedimage bfimage = null;
     int width = (int) toolkit.getdefaulttoolkit().getscreensize().getwidth();
     int height = (int) toolkit.getdefaulttoolkit().getscreensize().getheight();
     try {
       robot robot = new robot();
       bfimage = robot.createscreencapture(new rectangle(0, 0, width, height));
     } catch (awtexception e) {
       e.printstacktrace();
     }
     return bfimage;
   }
   
   /**
   * 从本地文件读取目标图片
   * @param keyimagepath - 图片绝对路径
   * @return 本地图片的bufferedimage对象
   */
   public bufferedimage getbfimagefrompath(string keyimagepath) {
     bufferedimage bfimage = null;
     try {
       bfimage = imageio.read(new file(keyimagepath));
     } catch (ioexception e) {
       e.printstacktrace();
     }
     return bfimage;
   }
   
   /**
   * 根据bufferedimage获取图片rgb数组
   * @param bfimage
   * @return
   */
   public int[][] getimagegrb(bufferedimage bfimage) {
     int width = bfimage.getwidth();
     int height = bfimage.getheight();
     int[][] result = new int[height][width];
     for (int h = 0; h < height; h++) {
       for (int w = 0; w < width; w++) {
         //使用getrgb(w, h)获取该点的颜色值是argb,而在实际应用中使用的是rgb,所以需要将argb转化成rgb,即bufimg.getrgb(w, h) & 0xffffff。
         result[h][w] = bfimage.getrgb(w, h) & 0xffffff;
       }
     }
     return result;
   }
   
   
   /**
   * 查找图片
   */
   public void findimage() {
     findimgdata = new int[keyimgheight][keyimgwidth][2];
     //遍历屏幕截图像素点数据
     for(int y=0; y<scrshotimgheight-keyimgheight; y++) {
       for(int x=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) {
           
           boolean isfinded = ismatchall(y, x);
           //如果比较结果完全相同,则说明图片找到,填充查找到的位置坐标数据到查找结果数组。
           if(isfinded) {
             for(int h=0; h<keyimgheight; h++) {
               for(int w=0; w<keyimgwidth; w++) {
                 findimgdata[h][w][0] = y+h; 
                 findimgdata[h][w][1] = x+w;
               }
             }
             return;
           }
         }
       }
     }
   }
   
   /**
   * 判断屏幕截图上目标图映射范围内的全部点是否全部和小图的点一一对应。
   * @param y - 与目标图左上角像素点想匹配的屏幕截图y坐标
   * @param x - 与目标图左上角像素点想匹配的屏幕截图x坐标
   * @return
   */
   public boolean ismatchall(int y, int x) {
     int biggery = 0;
     int biggerx = 0;
     int xor = 0;
     for(int smallery=0; smallery<keyimgheight; smallery++) {
       biggery = y+smallery;
       for(int smallerx=0; smallerx<keyimgwidth; smallerx++) {
         biggerx = x+smallerx;
         if(biggery>=scrshotimgheight || biggerx>=scrshotimgwidth) {
           return false;
         }
         xor = keyimagergbdata[smallery][smallerx]^screenshotimagergbdata[biggery][biggerx];
         if(xor!=0) {
           return false;
         }
       }
       biggerx = x;
     }
     return true;
   }
   
   /**
   * 输出查找到的坐标数据
   */
   private void printfinddata() {
     for(int y=0; y<keyimgheight; y++) {
       for(int x=0; x<keyimgwidth; x++) {
         system.out.print("("+this.findimgdata[y][x][0]+", "+this.findimgdata[y][x][1]+")");
       }
       system.out.println();
     }
   }
 
   
   public static void main(string[] args) {
     string keyimagepath = "d:/key.png";
     imagefinddemo demo = new imagefinddemo(keyimagepath);
     demo.printfinddata();
   }
 
 }

  这种算法是精确比较,只要有一个像素点有差异,就会找不到图片。当然,如果想指定一个比较的精确度,我也有个思路,就是在算法步骤4比较映射范围内全部像素点的时候做个统计,如果90%的点都相同,那就是说精确度是0.9。

  另外,可能还要考虑效率问题,不过,我在我的应用场景中并不太在意效率。如果有朋友看到这篇文章,对这个话题有更好的想法,请留言。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网