|
|
|
@ -1,8 +1,7 @@
|
|
|
|
|
package com.example.jmacro.wjdr;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.convert.Convert;
|
|
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
|
|
import cn.hutool.core.util.ReUtil;
|
|
|
|
|
import com.example.jmacro.wjdr.base.Legend;
|
|
|
|
|
import com.example.jmacro.wjdr.base.ViewPoint;
|
|
|
|
|
import com.example.jmacro.wjdr.base.ViewRect;
|
|
|
|
|
import com.example.jmacro.wjdr.util.ColorUtil;
|
|
|
|
@ -35,48 +34,32 @@ public abstract class JMacro {
|
|
|
|
|
*/
|
|
|
|
|
protected ViewRect focusRect;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 图例目录
|
|
|
|
|
*/
|
|
|
|
|
private File legend;
|
|
|
|
|
|
|
|
|
|
public JMacro() {
|
|
|
|
|
try {
|
|
|
|
|
// 机器人初始化
|
|
|
|
|
this.robot = new Robot();
|
|
|
|
|
this.robot.setAutoDelay(100);
|
|
|
|
|
this.legend = new File("legend");
|
|
|
|
|
this.robot.setAutoDelay(30);
|
|
|
|
|
this.startFocus();
|
|
|
|
|
} catch (AWTException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public JMacro(File legend) {
|
|
|
|
|
this();
|
|
|
|
|
this.legend = legend;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Robot getRobot() {
|
|
|
|
|
return robot;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public File getLegend() {
|
|
|
|
|
return legend;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setLegend(File legend) {
|
|
|
|
|
this.legend = legend;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取视口区域
|
|
|
|
|
*
|
|
|
|
|
* @return 视口区域
|
|
|
|
|
* 开始聚焦
|
|
|
|
|
*/
|
|
|
|
|
public void startFocus() {
|
|
|
|
|
TaskUtil.asyncTask(() -> {
|
|
|
|
|
focusRect = TaskUtil.timeTask(this::focus, 10, TimeUnit.SECONDS);
|
|
|
|
|
if (focusRect != null) {
|
|
|
|
|
Logger.info("聚焦成功");
|
|
|
|
|
} else {
|
|
|
|
|
Logger.error("聚焦失败");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -145,7 +128,7 @@ public abstract class JMacro {
|
|
|
|
|
int dx = (int) (startX + (point.getX() - startX) * d);
|
|
|
|
|
int dy = (int) (startY + (point.getY() - startY) * d);
|
|
|
|
|
robot.mouseMove(dx, dy);
|
|
|
|
|
robot.delay(RandomUtil.randomInt(interval - 10, interval + 10));
|
|
|
|
|
delay(RandomUtil.randomInt(interval - 10, interval + 10));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
robot.mouseMove(point.getX(), point.getY());
|
|
|
|
@ -160,7 +143,7 @@ public abstract class JMacro {
|
|
|
|
|
public void mouseLeftClick(ViewPoint rect) {
|
|
|
|
|
mouseMove(rect);
|
|
|
|
|
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
|
|
|
|
|
robot.delay(100);
|
|
|
|
|
delay(100);
|
|
|
|
|
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -265,11 +248,11 @@ public abstract class JMacro {
|
|
|
|
|
robot.mouseMove(rect.getCenter()[0], rect.getCenter()[1]);
|
|
|
|
|
delayUnstable();
|
|
|
|
|
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
|
|
|
|
|
robot.delay(100);
|
|
|
|
|
delay(100);
|
|
|
|
|
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
|
|
|
|
|
robot.delay(100);
|
|
|
|
|
delay(100);
|
|
|
|
|
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
|
|
|
|
|
robot.delay(100);
|
|
|
|
|
delay(100);
|
|
|
|
|
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -284,86 +267,88 @@ public abstract class JMacro {
|
|
|
|
|
* 获取聚焦区域
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect getFocusRect() {
|
|
|
|
|
return focusRect;
|
|
|
|
|
if (focusRect != null) {
|
|
|
|
|
return focusRect;
|
|
|
|
|
}
|
|
|
|
|
// 返回屏幕区域
|
|
|
|
|
return getScreenRect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取屏幕范围
|
|
|
|
|
* 获取聚焦区域
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect getScreenRect() {
|
|
|
|
|
if (focusRect != null) {
|
|
|
|
|
return focusRect;
|
|
|
|
|
}
|
|
|
|
|
// 返回屏幕区域
|
|
|
|
|
Toolkit tk = Toolkit.getDefaultToolkit();
|
|
|
|
|
return new ViewRect(0, 0, tk.getScreenSize().width, tk.getScreenSize().height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 全屏查找图片
|
|
|
|
|
*
|
|
|
|
|
* @param legendName 图例名称
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
* 延迟
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect findPic(String legendName, double minSimilar) {
|
|
|
|
|
if (!legendName.endsWith(".png")) {
|
|
|
|
|
legendName = legendName + ".png";
|
|
|
|
|
public void delay() {
|
|
|
|
|
delay(100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 延迟
|
|
|
|
|
*/
|
|
|
|
|
public void delay(long millis) {
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(millis);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
return findPic(new File(legend, legendName), minSimilar);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 全屏查找图片
|
|
|
|
|
*
|
|
|
|
|
* @param pic 图片
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
* 抖动延迟
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect findPic(File pic, double minSimilar) {
|
|
|
|
|
return findPic(getFocusRect(), ImageUtil.load(pic), minSimilar);
|
|
|
|
|
public void delayUnstable() {
|
|
|
|
|
delayUnstable(500);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 全屏查找图片
|
|
|
|
|
*
|
|
|
|
|
* @param pic 图片
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
* 抖动延迟
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect findPic(BufferedImage pic, double minSimilar) {
|
|
|
|
|
return findPic(getFocusRect(), pic, minSimilar);
|
|
|
|
|
public void delayUnstable(long millis) {
|
|
|
|
|
if (millis < 200) {
|
|
|
|
|
delay(millis);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
delay(RandomUtil.randomLong(millis - 100, millis + 100));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 应用视口查找图例
|
|
|
|
|
* 全屏查找图片
|
|
|
|
|
*
|
|
|
|
|
* @param viewRect 应用视口
|
|
|
|
|
* @param legendName 图例名称
|
|
|
|
|
* @param pic 图片
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect findPic(ViewRect viewRect, String legendName, double minSimilar) {
|
|
|
|
|
if (!legendName.endsWith(".png")) {
|
|
|
|
|
legendName = legendName + ".png";
|
|
|
|
|
}
|
|
|
|
|
return findPic(viewRect, new File(legend, legendName), minSimilar);
|
|
|
|
|
public ViewRect findPic(File pic, double minSimilar) {
|
|
|
|
|
return findPic(getFocusRect(), ImageUtil.load(pic), minSimilar);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 应用视口查找图例
|
|
|
|
|
* 全屏查找图片
|
|
|
|
|
*
|
|
|
|
|
* @param viewRect 应用视口
|
|
|
|
|
* @param legend 图例文件
|
|
|
|
|
* @param pic 图片
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect findPic(ViewRect viewRect, File legend, double minSimilar) {
|
|
|
|
|
return findPic(viewRect, ImageUtil.load(legend), minSimilar);
|
|
|
|
|
public ViewRect findPic(BufferedImage pic, double minSimilar) {
|
|
|
|
|
return findPic(getFocusRect(), pic, minSimilar);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取定位
|
|
|
|
|
* 查找图片
|
|
|
|
|
*
|
|
|
|
|
* @param pic 参考图
|
|
|
|
|
* @param viewRect 查找范围
|
|
|
|
|
* @param viewRect 查找范围(不设时,取全屏)
|
|
|
|
|
* @param pic 待查找图片
|
|
|
|
|
* @param minSimilar 相似度
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
@ -377,6 +362,9 @@ public abstract class JMacro {
|
|
|
|
|
Logger.error("视口尺寸小于图片");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (minSimilar > 1) {
|
|
|
|
|
throw new RuntimeException("this minSimilar must be less than 1");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取实时屏幕
|
|
|
|
|
BufferedImage screen = capture(robot, viewRect);
|
|
|
|
@ -407,281 +395,297 @@ public abstract class JMacro {
|
|
|
|
|
if (lt && rt && lb && rb && cc) {
|
|
|
|
|
// 统计相似点数
|
|
|
|
|
int samePixels = 0;
|
|
|
|
|
// 统计不相似点数
|
|
|
|
|
int diffPixels = 0;
|
|
|
|
|
for (int smallY = 0; smallY < pic.getHeight(); smallY++) {
|
|
|
|
|
for (int smallX = 0; smallX < pic.getWidth(); smallX++) {
|
|
|
|
|
if (ColorUtil.isSimilar(screenData[x + smallX][y + smallY], picData[smallX][smallY])) {
|
|
|
|
|
samePixels++;
|
|
|
|
|
} else {
|
|
|
|
|
diffPixels++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 计算目前相似度
|
|
|
|
|
// 计算相似度
|
|
|
|
|
double similar = (double) samePixels / (pic.getWidth() * pic.getHeight());
|
|
|
|
|
if (similar >= minSimilar) {
|
|
|
|
|
return new ViewRect(x, y, x + pic.getWidth(), y + pic.getHeight(), similar);
|
|
|
|
|
}
|
|
|
|
|
// 计算目前最大相似度
|
|
|
|
|
// 根据不相似像素点数推断本次可能达到的最大相似度,如果可能达到的最大相似度都小于预取相似度就不用比下去了
|
|
|
|
|
double maxSimilar = 1.0d - (double) diffPixels / (pic.getWidth() * pic.getHeight());
|
|
|
|
|
if (maxSimilar < minSimilar) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void delay() {
|
|
|
|
|
delay(100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void delay(long millis) {
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(millis);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void delayUnstable() {
|
|
|
|
|
delayUnstable(500);
|
|
|
|
|
/**
|
|
|
|
|
* 等待并查找图片
|
|
|
|
|
*
|
|
|
|
|
* @param pic 图片
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect waitAndFindPic(File pic, double minSimilar) {
|
|
|
|
|
return waitAndFindPic(getFocusRect(), pic, minSimilar, 10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void delayUnstable(long millis) {
|
|
|
|
|
if (millis < 200) {
|
|
|
|
|
delay(millis);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
delay(RandomUtil.randomLong(millis - 100, millis + 100));
|
|
|
|
|
/**
|
|
|
|
|
* 等待并查找图片
|
|
|
|
|
*
|
|
|
|
|
* @param pic 图片
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect waitAndFindPic(BufferedImage pic, double minSimilar) {
|
|
|
|
|
return waitAndFindPic(getFocusRect(), pic, minSimilar, 10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 等待并查找图片
|
|
|
|
|
*
|
|
|
|
|
* @param file 图例
|
|
|
|
|
* @param pic 图片
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @param seconds 最长等待秒数
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect waitAndFindPic(File file, double minSimilar) {
|
|
|
|
|
return waitAndFindPic(getFocusRect(), file, minSimilar, 10, TimeUnit.SECONDS);
|
|
|
|
|
public ViewRect waitAndFindPic(File pic, double minSimilar, long seconds) {
|
|
|
|
|
return waitAndFindPic(getFocusRect(), pic, minSimilar, seconds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 等待并查找图片
|
|
|
|
|
*
|
|
|
|
|
* @param rect 查找区域
|
|
|
|
|
* @param file 图例
|
|
|
|
|
* @param pic 图片
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @param seconds 最长等待秒数
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect waitAndFindPic(ViewRect rect, File file, double minSimilar) {
|
|
|
|
|
return waitAndFindPic(rect, file, minSimilar, 10, TimeUnit.SECONDS);
|
|
|
|
|
public ViewRect waitAndFindPic(BufferedImage pic, double minSimilar, long seconds) {
|
|
|
|
|
return waitAndFindPic(getFocusRect(), pic, minSimilar, seconds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 等待并查找图片
|
|
|
|
|
*
|
|
|
|
|
* @param rect 查找区域
|
|
|
|
|
* @param legendName 图例名称
|
|
|
|
|
* @param pic 图片
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect waitAndFindPic(ViewRect rect, String legendName, double minSimilar) {
|
|
|
|
|
if (!legendName.endsWith(".png")) {
|
|
|
|
|
legendName = legendName + ".png";
|
|
|
|
|
}
|
|
|
|
|
return waitAndFindPic(rect, new File(legend, legendName), minSimilar, 10, TimeUnit.SECONDS);
|
|
|
|
|
public ViewRect waitAndFindPic(ViewRect rect, File pic, double minSimilar) {
|
|
|
|
|
return waitAndFindPic(rect, pic, minSimilar, 10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 等待并查找图片
|
|
|
|
|
*
|
|
|
|
|
* @param file 图例
|
|
|
|
|
* @param rect 查找区域
|
|
|
|
|
* @param pic 图片
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @param time 最长等待时间
|
|
|
|
|
* @param unit 最长等待时间单位
|
|
|
|
|
* @param seconds 最长等待秒数
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect waitAndFindPic(File file, double minSimilar, long time, TimeUnit unit) {
|
|
|
|
|
return waitAndFindPic(getFocusRect(), file, minSimilar, time, unit);
|
|
|
|
|
public ViewRect waitAndFindPic(ViewRect rect, File pic, double minSimilar, long seconds) {
|
|
|
|
|
return waitAndFindPic(rect, ImageUtil.load(pic), minSimilar, seconds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 等待并查找图片
|
|
|
|
|
*
|
|
|
|
|
* @param rect 查找区域
|
|
|
|
|
* @param legendName 图例名称
|
|
|
|
|
* @param pic 图片
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @param time 最长等待时间
|
|
|
|
|
* @param unit 最长等待时间单位
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect waitAndFindPic(ViewRect rect, String legendName, double minSimilar, long time, TimeUnit unit) {
|
|
|
|
|
if (!legendName.endsWith(".png")) {
|
|
|
|
|
legendName = legendName + ".png";
|
|
|
|
|
}
|
|
|
|
|
return waitAndFindPic(rect, new File(legend, legendName), minSimilar, time, unit);
|
|
|
|
|
public ViewRect waitAndFindPic(ViewRect rect, BufferedImage pic, double minSimilar) {
|
|
|
|
|
return waitAndFindPic(rect, pic, minSimilar, 10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 等待并查找图片
|
|
|
|
|
*
|
|
|
|
|
* @param rect 查找区域
|
|
|
|
|
* @param file 图例
|
|
|
|
|
* @param pic 图片
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @param time 最长等待时间
|
|
|
|
|
* @param unit 最长等待时间单位
|
|
|
|
|
* @param seconds 最长等待秒数
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect waitAndFindPic(ViewRect rect, File file, double minSimilar, long time, TimeUnit unit) {
|
|
|
|
|
public ViewRect waitAndFindPic(ViewRect rect, BufferedImage pic, double minSimilar, long seconds) {
|
|
|
|
|
if (rect.getWidth() < pic.getWidth()) {
|
|
|
|
|
Logger.error("查找图片区域宽度{}小于图片宽度{}", rect.getWidth(), pic.getWidth());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (rect.getHeight() < pic.getHeight()) {
|
|
|
|
|
Logger.error("查找图片区域宽度{}小于图片宽度{}", rect.getHeight(), pic.getHeight());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return TaskUtil.timeTask(() -> {
|
|
|
|
|
BufferedImage image = ImageUtil.load(file);
|
|
|
|
|
if (rect.getWidth() < image.getWidth()) {
|
|
|
|
|
Logger.error("查找图片区域宽度{}小于图片宽度{}", rect.getWidth(), image.getWidth());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (rect.getHeight() < image.getHeight()) {
|
|
|
|
|
Logger.error("查找图片区域宽度{}小于图片宽度{}", rect.getHeight(), image.getHeight());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
while (JMainService.getInstance().run) {
|
|
|
|
|
delayUnstable();
|
|
|
|
|
ViewRect pic = findPic(rect, image, minSimilar);
|
|
|
|
|
if (pic != null) {
|
|
|
|
|
return pic;
|
|
|
|
|
ViewRect result = findPic(rect, pic, minSimilar);
|
|
|
|
|
if (result != null) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, time, unit);
|
|
|
|
|
return null;
|
|
|
|
|
}, seconds, TimeUnit.SECONDS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 等待并匹配图例
|
|
|
|
|
* 匹配图片
|
|
|
|
|
*
|
|
|
|
|
* @param rect 参照区域
|
|
|
|
|
* @param legendName 图例名称
|
|
|
|
|
* @param pic 图片
|
|
|
|
|
* @param location 定位
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
* @return 匹配图片
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect waitAndMatchPic(ViewRect rect, String legendName, double minSimilar) {
|
|
|
|
|
if (!legendName.endsWith(".png")) {
|
|
|
|
|
legendName = legendName + ".png";
|
|
|
|
|
public ViewRect matchPic(File pic, ViewPoint location, double minSimilar) {
|
|
|
|
|
if (!pic.exists()) {
|
|
|
|
|
Logger.error("file [{}] not exist", pic.getAbsolutePath());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return waitAndMatchPic(rect, new File(legend, legendName), minSimilar, 3, TimeUnit.SECONDS);
|
|
|
|
|
return matchPic(ImageUtil.load(pic), location, minSimilar);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ViewRect matchPic(BufferedImage pic, ViewPoint location, double minSimilar) {
|
|
|
|
|
ViewRect focusRect = getFocusRect();
|
|
|
|
|
int offsetX = location.getX();
|
|
|
|
|
int offsetY = location.getY();
|
|
|
|
|
ViewRect viewRect = new ViewRect();
|
|
|
|
|
viewRect.setLeft(focusRect.getLeft() + offsetX);
|
|
|
|
|
viewRect.setTop(focusRect.getTop() + offsetY);
|
|
|
|
|
viewRect.setRight(viewRect.getLeft() + pic.getWidth());
|
|
|
|
|
viewRect.setBottom(viewRect.getTop() + pic.getHeight());
|
|
|
|
|
ImageUtil.show(viewRect);
|
|
|
|
|
return findPic(viewRect, pic, minSimilar);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 等待并匹配图例
|
|
|
|
|
* 等待并匹配图片
|
|
|
|
|
*
|
|
|
|
|
* @param rect 参照区域
|
|
|
|
|
* @param file 图例
|
|
|
|
|
* @param pic 图片
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect waitAndMatchPic(ViewRect rect, File file, double minSimilar) {
|
|
|
|
|
return waitAndMatchPic(rect, file, minSimilar, 3, TimeUnit.SECONDS);
|
|
|
|
|
public ViewRect waitAndMatchPic(File pic, ViewPoint location, double minSimilar) {
|
|
|
|
|
return waitAndMatchPic(ImageUtil.load(pic), location, minSimilar);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 等待并匹配图例
|
|
|
|
|
* 等待并匹配图片
|
|
|
|
|
*
|
|
|
|
|
* @param pic 图片
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect waitAndMatchPic(BufferedImage pic, ViewPoint location, double minSimilar) {
|
|
|
|
|
return waitAndMatchPic(pic, location, minSimilar, 10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 等待并匹配图片
|
|
|
|
|
*
|
|
|
|
|
* @param rect 参照区域
|
|
|
|
|
* @param file 图例
|
|
|
|
|
* @param pic 图片
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @param time 最长等待时间
|
|
|
|
|
* @param unit 最长等待时间单位
|
|
|
|
|
* @param seconds 最长等待秒数
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect waitAndMatchPic(ViewRect rect, File file, double minSimilar, long time, TimeUnit unit) {
|
|
|
|
|
public ViewRect waitAndMatchPic(File pic, ViewPoint location, double minSimilar, long seconds) {
|
|
|
|
|
return waitAndMatchPic(ImageUtil.load(pic), location, minSimilar, seconds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 等待并匹配图片
|
|
|
|
|
*
|
|
|
|
|
* @param pic 图片
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @param seconds 最长等待秒数
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect waitAndMatchPic(BufferedImage pic, ViewPoint location, double minSimilar, long seconds) {
|
|
|
|
|
return TaskUtil.timeTask(() -> {
|
|
|
|
|
while (JMainService.getInstance().run) {
|
|
|
|
|
ViewRect matchPic = matchPic(rect, file, minSimilar);
|
|
|
|
|
ViewRect matchPic = matchPic(pic, location, minSimilar);
|
|
|
|
|
if (matchPic != null) {
|
|
|
|
|
return matchPic;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}, time, unit);
|
|
|
|
|
}, seconds, TimeUnit.SECONDS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 匹配图例
|
|
|
|
|
*
|
|
|
|
|
* @param legend 图例
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @return 匹配图片
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect matchLegend(String legend, double minSimilar) {
|
|
|
|
|
return matchLegend(Legend.inflate(legend), minSimilar);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 匹配图片
|
|
|
|
|
*
|
|
|
|
|
* @param rect 程序窗口(参照系)
|
|
|
|
|
* @param file 图例
|
|
|
|
|
* @param legend 图例
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @return 匹配图片
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect matchPic(ViewRect rect, File file, double minSimilar) {
|
|
|
|
|
public ViewRect matchLegend(Legend legend, double minSimilar) {
|
|
|
|
|
if (minSimilar > 1) {
|
|
|
|
|
throw new RuntimeException("this minSimilar must be less than 1");
|
|
|
|
|
}
|
|
|
|
|
if (!file.exists()) {
|
|
|
|
|
Logger.error("file [{}] not exist", file.getAbsolutePath());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
String name = file.getName();
|
|
|
|
|
int offsetX = 0;
|
|
|
|
|
int offsetY = 0;
|
|
|
|
|
if (name.matches("[\\S\\s]+_L[0-9]+,[0-9]+\\.png")) {
|
|
|
|
|
offsetX = Convert.toInt(ReUtil.get("[\\S\\s]+_L([0-9]+),[0-9]+\\.png", name, 1), 0);
|
|
|
|
|
offsetY = Convert.toInt(ReUtil.get("[\\S\\s]+_L[0-9]+,([0-9]+)\\.png", name, 1), 0);
|
|
|
|
|
}
|
|
|
|
|
int offsetX = legend.getLocation().getX();
|
|
|
|
|
int offsetY = legend.getLocation().getY();
|
|
|
|
|
ViewRect viewRect = new ViewRect();
|
|
|
|
|
viewRect.setLeft(rect.getLeft() + offsetX);
|
|
|
|
|
viewRect.setTop(rect.getTop() + offsetY);
|
|
|
|
|
BufferedImage image = ImageUtil.load(file);
|
|
|
|
|
viewRect.setLeft(getFocusRect().getLeft() + offsetX);
|
|
|
|
|
viewRect.setTop(getFocusRect().getTop() + offsetY);
|
|
|
|
|
BufferedImage image = ImageUtil.load(legend.getFile());
|
|
|
|
|
viewRect.setRight(viewRect.getLeft() + image.getWidth());
|
|
|
|
|
viewRect.setBottom(viewRect.getTop() + image.getHeight());
|
|
|
|
|
|
|
|
|
|
ViewRect pic = findPic(viewRect, image, minSimilar);
|
|
|
|
|
ImageUtil.show(viewRect);
|
|
|
|
|
return pic;
|
|
|
|
|
return findPic(viewRect, image, minSimilar);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 匹配图片
|
|
|
|
|
* 等待并匹配图例
|
|
|
|
|
*
|
|
|
|
|
* @param rect 程序窗口(参照系)
|
|
|
|
|
* @param legendName 图例名称
|
|
|
|
|
* @param legend 图例
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @return 匹配图片
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect matchPic(ViewRect rect, String legendName, double minSimilar) {
|
|
|
|
|
if (!legendName.endsWith(".png")) {
|
|
|
|
|
legendName = legendName + ".png";
|
|
|
|
|
}
|
|
|
|
|
return matchPic(rect, new File(legend, legendName), minSimilar);
|
|
|
|
|
public ViewRect waitAndMatchLegend(String legend, double minSimilar) {
|
|
|
|
|
return waitAndMatchLegend(Legend.inflate(legend), minSimilar);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 是否匹配图片
|
|
|
|
|
* 等待并匹配图例
|
|
|
|
|
*
|
|
|
|
|
* @param rect 程序窗口(参照系)
|
|
|
|
|
* @param file 图例
|
|
|
|
|
* @param legend 图例
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @return 匹配图片
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public boolean isMatchPic(ViewRect rect, File file, double minSimilar) {
|
|
|
|
|
return matchPic(rect, file, minSimilar) != null;
|
|
|
|
|
public ViewRect waitAndMatchLegend(Legend legend, double minSimilar) {
|
|
|
|
|
return waitAndMatchLegend(legend, minSimilar, 5);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 是否匹配图片
|
|
|
|
|
* 等待并匹配图例
|
|
|
|
|
*
|
|
|
|
|
* @param rect 程序窗口(参照系)
|
|
|
|
|
* @param legendName 图例名称
|
|
|
|
|
* @param legend 图例
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @return 匹配图片
|
|
|
|
|
* @param seconds 最长等待秒数
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public boolean isMatchPic(ViewRect rect, String legendName, double minSimilar) {
|
|
|
|
|
if (!legendName.endsWith(".png")) {
|
|
|
|
|
legendName = legendName + ".png";
|
|
|
|
|
}
|
|
|
|
|
return matchPic(rect, new File(legend, legendName), minSimilar) != null;
|
|
|
|
|
public ViewRect waitAndMatchLegend(String legend, double minSimilar, long seconds) {
|
|
|
|
|
return waitAndMatchLegend(Legend.inflate(legend), minSimilar, seconds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 等待并匹配图例
|
|
|
|
|
*
|
|
|
|
|
* @param legend 图例
|
|
|
|
|
* @param minSimilar 最低相似度
|
|
|
|
|
* @param seconds 最长等待秒数
|
|
|
|
|
* @return 匹配图片区域
|
|
|
|
|
*/
|
|
|
|
|
public ViewRect waitAndMatchLegend(Legend legend, double minSimilar, long seconds) {
|
|
|
|
|
return waitAndMatchPic(legend.getFile(), legend.getLocation(), minSimilar, seconds);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|