JAVA截图与屏幕截图⽐较查找到坐标点并模拟点击ller;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
三重唱import java.util.LinkedHashMap;
import java.util.Map;
import static java.lang.Thread.sleep;
/**
* Created by long.tang on 2019/4/25.
*/
public class FindRobot {
BufferedImage screenShotImage = FullScreenShot(); //屏幕截图
BufferedImage keyImage; //查找⽬标图⽚
int scrShotImgWidth; //屏幕截图宽度
int scrShotImgHeight; //屏幕截图⾼度
int keyImgWidth; //查找⽬标图⽚宽度
int keyImgHeight; //查找⽬标图⽚⾼度
int[][] screenShotImageRGBData; //屏幕截图RGB数据
int[][] keyImageRGBData; //查找⽬标图⽚RGB数据
int findX; //查找结果,⽬标图标位于屏幕截图上的X坐标数据
int findY; //查找结果,⽬标图标位于屏幕截图上的Y坐标数据
private static Robot robot = getRobot();
private static Map map = new LinkedHashMap<>();
public FindRobot(){
}
/**
* 是否改变⽬标背景画布再次查找
* @param keyImagePath
*/
public FindRobot(String keyImagePath,String screenshot,int x , int y) {
screenShotImage = (screenshot==null || screenshot.equals(""))?FullScreenShot():BfImageFromPath(screenshot);
keyImage = BfImageFromPath(keyImagePath);
screenShotImageRGBData = ImageGRB(screenShotImage);
keyImageRGBData = ImageGRB(keyImage);
scrShotImgWidth = Width();
scrShotImgHeight = Height();
keyImgWidth = Width();
keyImgHeight = Height();
//开始查找
this.findImageXY(x,y);
}
/**
* 查找图⽚
*/
public void findImageXY(int a ,int b) {
//遍历屏幕截图像素点数据
for (int y = b; 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) {
//0
int minY = y;
int maxY = y + keyImgHeight;
findY = ((minY + maxY) / 2);
//1
int minX = x;
int maxX = x + keyImgWidth;
findX = ((minX + maxX) / 2);
//System.out.println("找到第"+findX+"次");
map.put("point_"+findX+"_"+findY,findX+","+findY);
//return;
}
}
}
}
}
/**
* 获取机器⼈
* @return
*/
private static Robot getRobot(){
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
return robot;
}
在职出国留学/**
* 全屏截图
*
* @return 返回BufferedImage
*/
menu怎么读public BufferedImage getFullScreenShot() {
BufferedImage bfImage = null;
int width = (int) DefaultToolkit().getScreenSize().getWidth();
int height = (int) DefaultToolkit().getScreenSize().getHeight();
try {
Robot robot = new Robot();
bfImage = ateScreenCapture(new Rectangle(0, 0, width, height));
} catch (AWTException e) {
e.printStackTrace();
}
return bfImage;
}
/**
* 输出图⽚⽂件
* @param bi
*/
public static String writeImageFile(BufferedImage bi) {
String path = "";
String path = "";
try {
path = "/files/screen_"+ System.currentTimeMillis()+".png";
File outputfile = new File(path);
摘抄好词好句好段ImageIO.write(bi, "png", outputfile);
} catch (IOException e) {
如何脱敏
}
return path;
}
/**
* 从本地⽂件读取⽬标图⽚
*
* @param keyImagePath - 图⽚绝对路径
* @return 本地图⽚的BufferedImage对象
*/
public BufferedImage getBfImageFromPath(String keyImagePath) {
BufferedImage bfImage = null;
try {
bfImage = ad(new File(keyImagePath));
} catch (IOException e) {
e.printStackTrace();
}
return bfImage;
}
/**
* 根据BufferedImage获取图⽚RGB数组
*
* @param bfImage
* @return
*/
public int[][] getImageGRB(BufferedImage bfImage) {
int width = Width();
int height = Height();
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,即RGB(w, h) & 0xFFFFFF。 result[h][w] = RGB(w, h) & 0xFFFFFF;
}
}
海底两万里内容简介
return result;
}
/**
* 判断屏幕截图上⽬标图映射范围内的全部点是否全部和⼩图的点⼀⼀对应。
*
* @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 fal;
}
xor = keyImageRGBData[smallerY][smallerX] ^ screenShotImageRGBData[biggerY][biggerX];
xor = keyImageRGBData[smallerY][smallerX] ^ screenShotImageRGBData[biggerY][biggerX]; if (xor != 0) {
//hitMiss++;
return fal;
}
}
//biggerX = x;
}
return true;
}
/*****
* //移动⿏标
* uMove(500, 500);
* //点击⿏标
* //⿏标左键
* System.out.println("单击");
* uPress(InputEvent.BUTTON1_MASK);
* uRelea(InputEvent.BUTTON1_MASK);
* //⿏标右键
* System.out.println("右击");
* uPress(InputEvent.BUTTON3_MASK);
* uRelea(InputEvent.BUTTON3_MASK);
* //按下ESC,退出右键状态
* System.out.println("按下ESC");
* robot.keyPress(KeyEvent.VK_ESCAPE);
* robot.keyRelea(KeyEvent.VK_ESCAPE);
* //滚动⿏标滚轴
* System.out.println("滚轴");
* uWheel(5);
* <p>
* <p>
* //按下Alt+TAB键(切换桌⾯窗⼝)
* robot.keyPress(KeyEvent.VK_ALT);
* for(int i=1;i<=2;i++)
* {
* robot.keyPress(KeyEvent.VK_TAB);
* robot.keyRelea(KeyEvent.VK_TAB);
* }
* robot.keyRelea(KeyEvent.VK_ALT);
* uWheel 滚动⿏标
* @param path
*/
public static boolean touchPic(String path) {
//移动到定位
boolean res = findPoint(path);
System.out.println("移动到定位:"+res);
if(!res){
return fal;
}
//1.click
robot.tAutoDelay(100);
System.out.println("移动点击完成...");
return true;
}
/**
* 找到定位并移动到定位点
* @param path
* @return
*/
public static boolean findPoint(String path){
return findPoint(path,true);
}
public static boolean findPoint(String path,boolean move){
map.clear();
FindRobot demo = new FindRobot(path,"",0,0);
if(map==null||map.size()==0){
return fal;
} el {
System.out.println("找到:"+demo.findX+","+demo.findY);
if(move){
}
return true;
}
}
/**
* 查找是否唯⼀解
* @param path
* @return
*/
public static boolean findAllSamePoint(String path,String screen,int x ,int y){ FindRobot demo = new FindRobot(path,screen,x,y);
if(demo.findX==0 && demo.findY==0){
return fal;
} el {
//System.out.println("找到:"+demo.findX+","+demo.findY);
findAllSamePoint(path,screen,demo.findX,demo.findY);
}
return true;
}立德树人心得体会
/**
* 输⼊键盘对应值
* KeyEvent.VK_*
* @param keyPress
*/
public static boolean inputVaule(int keyPress){
downKeyPress(keyPress);
tAutoDelay(100);
upKeyPress(keyPress);
return true;
}
/**
* 设置休息时间,毫秒
* @param time
* @return
*/
public static void tAutoDelay(int time){
robot.tAutoDelay(time);
}
/**
* 按下键位
* @param keyPress