diff --git a/src/main/java/com/example/jmacro/wjdr/JMacro.java b/src/main/java/com/example/jmacro/wjdr/JMacro.java index 8eed18f..adf77a1 100644 --- a/src/main/java/com/example/jmacro/wjdr/JMacro.java +++ b/src/main/java/com/example/jmacro/wjdr/JMacro.java @@ -1,15 +1,17 @@ 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.ScreenPoint; import com.example.jmacro.wjdr.base.ScreenRect; import com.example.jmacro.wjdr.util.ColorUtil; import com.example.jmacro.wjdr.util.Imager; +import com.example.jmacro.wjdr.util.Logger; import com.example.jmacro.wjdr.util.TaskUtil; import java.awt.*; import java.awt.event.InputEvent; -import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.io.File; import java.util.concurrent.TimeUnit; @@ -24,9 +26,7 @@ import java.util.concurrent.TimeUnit; public class JMacro { public static void main(String[] args) throws AWTException { - JMacro jMacro = new JMacro(); - jMacro.mouseLeftClick(new ScreenPoint(115, 614)); - jMacro.keyInput(KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_C, KeyEvent.VK_D, KeyEvent.VK_ENTER); + System.out.println("L0,0-A.png".matches("L[0-9]+,[0-9]+-[\\S\\s]+")); } /** @@ -34,12 +34,22 @@ public class JMacro { */ private final Robot robot; + /** + * 图例目录 + */ + private File legend; + public JMacro() throws AWTException { // 机器人初始化 this.robot = new Robot(); this.robot.setAutoDelay(100); } + public JMacro(File legend) throws AWTException { + this(); + this.legend = legend; + } + /** * 键盘按键组 */ @@ -213,11 +223,11 @@ public class JMacro { robot.mouseMove(rect.getCenter()[0], rect.getCenter()[1]); waitTap(); robot.mousePress(InputEvent.BUTTON1_MASK); - waitTap(); + robot.delay(100); robot.mouseRelease(InputEvent.BUTTON1_MASK); - waitTap(); + robot.delay(100); robot.mousePress(InputEvent.BUTTON1_MASK); - waitTap(); + robot.delay(100); robot.mouseRelease(InputEvent.BUTTON1_MASK); } @@ -280,10 +290,10 @@ public class JMacro { // 得到图片左上角范围 int xMin = screenRect.getLeft(); // 因为坐标取像素点是从0开始,因此需要-1 - int xMax = screenRect.getRight() - 1 - pic.getWidth(); + int xMax = screenRect.getRight() - pic.getWidth(); int yMin = screenRect.getTop(); // 因为坐标取像素点是从0开始,因此需要-1 - int yMax = screenRect.getBottom() - 1 - pic.getHeight(); + int yMax = screenRect.getBottom() - pic.getHeight(); for (int y = yMin; y <= yMax; y++) { for (int x = xMin; x <= xMax; x++) { @@ -325,6 +335,17 @@ public class JMacro { robot.delay(i); } + /** + * 等待并查找图片 + * + * @param file 图例 + * @param minSimilar 最低相似度 + * @return 匹配图片区域 + */ + public ScreenRect waitAndFindPic(File file, double minSimilar) { + return waitAndFindPic(getScreenRect(), file, minSimilar, 10, TimeUnit.SECONDS); + } + /** * 等待并查找图片 * @@ -337,6 +358,19 @@ public class JMacro { return waitAndFindPic(rect, file, minSimilar, 10, TimeUnit.SECONDS); } + /** + * 等待并查找图片 + * + * @param file 图例 + * @param minSimilar 最低相似度 + * @param time 最长等待时间 + * @param unit 最长等待时间单位 + * @return 匹配图片区域 + */ + public ScreenRect waitAndFindPic(File file, double minSimilar, long time, TimeUnit unit) { + return waitAndFindPic(getScreenRect(), file, minSimilar, time, unit); + } + /** * 等待并查找图片 * @@ -349,13 +383,61 @@ public class JMacro { */ public ScreenRect waitAndFindPic(ScreenRect rect, File file, double minSimilar, long time, TimeUnit unit) { return TaskUtil.timeTask(() -> { + BufferedImage image = Imager.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) { waitTap(); - ScreenRect pic = findPic(rect, Imager.load(file), minSimilar); + ScreenRect pic = findPic(rect, image, minSimilar); if (pic != null) { return pic; } } }, time, unit); } + + /** + * 匹配图片 + * + * @param point 匹配坐标(左上角) + * @param file 图例 + * @param minSimilar 最低相似度 + * @return 匹配图片 + */ + public ScreenRect matchPic(ScreenPoint point, File file, double minSimilar) { + String name = file.getName(); + int offsetX = 0; + int offsetY = 0; + if (name.matches("L[0-9]+,[0-9]+-[\\S\\s]+")) { + offsetX = Convert.toInt(ReUtil.get("L([0-9]+),[0-9]+-[\\S\\s]+", name, 1), 0); + offsetY = Convert.toInt(ReUtil.get("L[0-9]+,([0-9]+)-[\\S\\s]+", name, 1), 0); + } + ScreenRect screenRect = new ScreenRect(); + screenRect.setLeft(point.getX() + offsetX); + screenRect.setTop(point.getY() + offsetY); + BufferedImage image = Imager.load(file); + screenRect.setRight(screenRect.getLeft() + image.getWidth()); + screenRect.setBottom(screenRect.getTop() + image.getHeight()); + System.out.println(screenRect.toString()); + return findPic(screenRect, image, minSimilar); + } + + /** + * 是否匹配图片 + * + * @param point 匹配坐标(左上角) + * @param file 图例 + * @param minSimilar 最低相似度 + * @return 匹配图片 + */ + public boolean isMatchPic(ScreenPoint point, File file, double minSimilar) { + return matchPic(point, file, minSimilar) != null; + } }