实现原理

  • 截取当前屏幕
  • 读取需要查找的图片
  • 抽取特征
  • 根据特征遍历屏幕找到符合特征的块
  • 把找到的特征块和图片进行精确对比

代码如下

  • 首先定义图片查找接口
        public interface ImageFinder {/*** 查询到匹配的图片* @param image 需要查找的图片* @return*/List<Coordinate> match(BufferedImage image,double percent);}
  • 定义查找到的返回坐标
        public class Coordinate {private int x;private int y;public Coordinate(int x, int y) {this.x = x;this.y = y;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}}
  • 特征类
            public class ColorBlock {int[][] rgbs;private int width;private int height;private Point leftTop;private Point rightTop;private Point leftBottom;private Point rightBottom;private Point center;private int count = 0;public ColorBlock(BufferedImage image){this.width = image.getWidth();this.height = image.getHeight();this.rgbs = new int[this.width][this.height];for(int x=0;x<this.width;x++){for(int y=0; y<this.height;y++){this.rgbs[x][y] = image.getRGB(x,y);count = count+1;}}this.leftTop = Point.ZERO;this.leftTop.setRgb(rgbs);this.rightTop = new Point(this.width-1,0);this.rightTop.setRgb(rgbs);this.leftBottom = new Point(0,this.height-1);this.leftBottom.setRgb(rgbs);this.rightBottom = new Point(this.width-1,this.height-1);this.rightBottom.setRgb(rgbs);this.center = new Point(this.width/2,this.height/2);this.center.setRgb(rgbs);}public static class Point{public static final Point ZERO = new Point(0,0);private int x;private int y;private int rgb;public Point(int x, int y) {this.x = x;this.y = y;}public Point(int x, int y, int rgb) {this(x,y);this.rgb = rgb;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public int getRgb() {return rgb;}public void setRgb(int rgb) {this.rgb = rgb;}public void setRgb(int[][] block){this.rgb = block[this.getX()][this.getY()];}}public Point getLeftTop() {return leftTop;}public void setLeftTop(Point leftTop) {this.leftTop = leftTop;}public Point getRightTop() {return rightTop;}public void setRightTop(Point rightTop) {this.rightTop = rightTop;}public Point getLeftBottom() {return leftBottom;}public void setLeftBottom(Point leftBottom) {this.leftBottom = leftBottom;}public Point getRightBottom() {return rightBottom;}public void setRightBottom(Point rightBottom) {this.rightBottom = rightBottom;}public Point getCenter() {return center;}public void setCenter(Point center) {this.center = center;}public boolean isPointMatch(int leftTop,int rightTop,int leftBottom,int rightBottom,int center){return  this.leftTop.getRgb()==leftTop&&this.rightTop.getRgb()==rightTop&&this.leftBottom.getRgb()==leftBottom&&this.rightBottom.getRgb()==rightBottom&&this.center.getRgb()==center;}public int getWidth() {return width;}public void setWidth(int width) {this.width = width;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}public boolean allMatch(Coordinate coordinate,int[][] screen,double percent){// 检查x中心线int centerX = this.getWidth()/2;int centerY = this.getHeight()/2;for(int x=0;x<this.getWidth();x++){if(this.rgbs[x][centerY]!=screen[x+coordinate.getX()][centerY+coordinate.getY()]) return false;}for(int y=0;y<this.getHeight();y++){if(this.rgbs[centerX][y]!=screen[coordinate.getX()+centerX][y+coordinate.getY()]) return false;}//检查y 中心线int temp=0;for(int x = 0;x<this.getWidth();x++){for(int y=0;y<this.getHeight();y++){if(this.rgbs[x][y]==screen[x+coordinate.getX()][y+coordinate.getY()]) temp++;}}return ((double)temp/(double) count)>percent;}
}
  • 查找实现类
            public class ScreenImageFinder implements ImageFinder {protected   Robot robot ;protected BufferedImage screen;protected  Dimension dimension;public ScreenImageFinder() {try {dimension = Toolkit.getDefaultToolkit().getScreenSize();robot = new Robot();screen = robot.createScreenCapture(new Rectangle(0,0,dimension.width,dimension.height));} catch (AWTException e) {e.printStackTrace();}}public List<Coordinate> match( BufferedImage image,double percent) {//0 0  left top//5 0  right top//0 5  left  bottom//5 5  right bottom//2 2  centerint[][] screenRgbs = getRGB(screen);ColorBlock block = new ColorBlock(image);List<Coordinate> coordinates = new ArrayList<>();for(int x=0;x<screen.getWidth()-block.getWidth();x++){for(int y=0;y<screen.getHeight()-block.getHeight();y++){int topLeft = screenRgbs[x][y];int topRight= screenRgbs[x+ block.getRightTop().getX()][y+block.getRightTop().getY()];int bottomLeft= screenRgbs[x+ block.getLeftBottom().getX()][y+block.getLeftBottom().getY()];int bottomRight= screenRgbs[x+ block.getRightBottom().getX()][y+block.getRightBottom().getY()];int center= screenRgbs[x+ block.getCenter().getX()][y+block.getCenter().getY()];if(!block.isPointMatch(topLeft,topRight,bottomLeft,bottomRight,center)) continue;coordinates.add(new Coordinate(x,y));}}return coordinates.stream().filter(coordinate -> block.allMatch(coordinate,screenRgbs,percent)).collect(toList());}private boolean colorCompare(int source,int compare){return source==compare;}public Robot getRobot() {return robot;}public void setRobot(Robot robot) {this.robot = robot;}public BufferedImage getScreen() {return screen;}public void setScreen(BufferedImage screen) {this.screen = screen;}public Dimension getDimension() {return dimension;}public void setDimension(Dimension dimension) {this.dimension = dimension;}}-  测试代码ImageFinder finder = new ScreenImageFinder();BufferedImage search = ImageIO.read(Application.class.getClassLoader().getResourceAsStream("help.png"));long start = System.currentTimeMillis();List<Coordinate> coordinateList = finder.match(search,0.99);System.out.println("耗时:"+(System.currentTimeMillis()-start));coordinateList.stream().findAny().ifPresent(coordinate -> System.out.println(String.format("find help image x:%d,y:%d",coordinate.getX(),coordinate.getY())));
  • 测试结果
                耗时:219find help image x:551,y:24

用java实现屏幕找图相关推荐

  1. python 屏幕找图 点击,使用Python脚本在windows屏幕找图

    Python实现windows屏幕找图 import win32gui hwnd_title = dict() def get_all_hwnd(hwnd,mouse): if win32gui.Is ...

  2. Python笔记-利用OpenCV的matchTemplate屏幕找图并使用pyautogui点击

    要找的图为计算机,也就是icon.png,对应的图标为: 需要安装的依赖: pip install cv2 pip install PIL pip install pyautogui 代码如下: im ...

  3. python屏幕找图_Python图片识别找坐标(appium通过识别图片点击坐标)

    ***如果只想了解图片相似度识别,直接看第一步即可 ***如果想了解appium根据图片识别点击坐标,需要看第一.二.三步 背景|在做UI测试时,发现iOS自定义的UI控件,appium识别不到.所以 ...

  4. python 屏幕找图 点击_捕获屏幕并查找参考图像

    我尝试在python中执行以下操作:捕捉屏幕 如果屏幕截图包含给定的参考图像(可以是jpg或pgn),则获取该图像在屏幕上的坐标 更多信息:参考图像不会太大(5x5像素就足够了) 它应该尽可能快,因为 ...

  5. python opencv屏幕找图_使用Python+OpenCV进行图像模板匹配(Match Template)实例-找到百度首页按钮并点击...

    意图:准备一张小图,在电脑屏幕上找到小图坐标,并点击. 1  安装 opencv 和 numpy: pip3 install opencv-python 上述命令将 opencv 和 numpy都安装 ...

  6. 【Java】Java实现找图抓色

    改编自博客园,原文链接:https://www.cnblogs.com/jebysun/p/3969352.html 为了适应业务需求,对原来的代码进行了修改,现在支持设置查找图片的精确度,以及用Ja ...

  7. Java找图 (截屏找图 大图找小图)--自己实现“按键精灵”

    最近老板给了个需求是要做一个在安卓模拟器上自动扫码支付的程序,本来以为用Appium就可以直接直接搞定的,但是最后发现Appium只能操作App,在模拟器的实际运行中App要进入扫码功能,还要点击安卓 ...

  8. 网摘-按键精灵屏幕找色原理分析

    一.数据提取 位图其实可以看成是一个由象素组成的矩阵,找图找色可以看成是象素值的比对.很多新手在设计这类的程序时喜欢使用TBitmap.Canvas.Pixels属性,这个属性其实是对API函数Get ...

  9. java开发找你妹_找你妹java版下载-找你妹java版2020最新版下载v1.1.1_MDPDA手机网

    找你妹java版是一款超级好玩的找茬冒险类手机游戏,充满趣味的卡通画风,超多不同游戏关卡,难度可以让玩家来自己选择,很考验玩家的观察能力,丰厚的奖励,玩家们每天登录到游戏当中就可以直接获得,还有超级多 ...

最新文章

  1. 让你分分钟学会Javascript中的闭包
  2. java http get json_java实现Http post(参数json格式)、get 请求
  3. html 拖拽坐标,Html+css实现拖拽导航条
  4. 转 最小生成树(kruskal 算法 和prim算法)
  5. Java坦克大战(四)
  6. appium+python自动化-adb shell模拟点击事件(input tap)
  7. 小学期Deadline之GEC6818点奶茶系统
  8. 目标检测算法YOLO3论文解读
  9. c语言余数求和,C语言实现两数相加2018-09-23(示例代码)
  10. 纯HTML CSS制作导航栏 下拉菜单
  11. 员工满意度调查问卷的设计注意事项
  12. CST软件基本操作—1
  13. 关于视频号主页实现一键添加个人微信功能的思路
  14. 运动耳机品牌推荐,热门六款运动耳机推荐
  15. PaddleX---MobileNetV3_ssld图像分类
  16. 【子桓说】从阿里、百度分析,教你判断一家企业是否适合自己
  17. 1455B.Jumps
  18. Git系列:管理、撤销以及删除
  19. 图形学学习 TOPIC 2 TransformationsSmooth Rotation
  20. 快应用入门:第一个快应用程序

热门文章

  1. 【Ubuntu】postman安装、创建桌面快捷方式
  2. 计算机键盘打字基础知识,电脑打字基础知识,新手自学【入门篇】
  3. 2014年10月25日深圳彩讯科技和北京宇信易诚的笔试记录
  4. 运行patsy 时报错 assert pytype not in (tokenize.NL, tokenize.NEWLINE)
  5. 信息系统项目管理知识--项目立项管理
  6. JDBC 数据库编程基础
  7. Java中高级核心知识全面解析——Redis(集群【概述{主从复制、哨兵、集群化}、数据分区方案、节点通信机制、数据结构简析】)5
  8. .NET高级软件工程师的面试题目
  9. UE4中Pak文件的读取规则
  10. 简师网:公务员这些知识点需要背诵!