From 3af8a1681658f69d51129caa9594d11fe7ea0d68 Mon Sep 17 00:00:00 2001 From: wangbing Date: Thu, 25 Sep 2025 14:11:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=A4=87=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/compiler.xml | 2 +- pom.xml | 13 +- src/main/java/xyz/wbsite/jmacro/JMacro.java | 1094 ++--------------- .../xyz/wbsite/jmacro/JMainController.java | 18 +- .../java/xyz/wbsite/jmacro/JMainService.java | 2 +- .../xyz/wbsite/jmacro/base/ColorLocation.java | 46 + .../java/xyz/wbsite/jmacro/base/Legend.java | 7 +- .../xyz/wbsite/jmacro/base/ViewColor.java | 45 - .../xyz/wbsite/jmacro/base/ViewPoint.java | 71 -- .../java/xyz/wbsite/jmacro/base/ViewRect.java | 105 -- .../xyz/wbsite/jmacro/util/ImageUtil.java | 13 - src/main/java/xyz/wbsite/jtask/TaskImpl.java | 18 +- 12 files changed, 170 insertions(+), 1264 deletions(-) create mode 100644 src/main/java/xyz/wbsite/jmacro/base/ColorLocation.java delete mode 100644 src/main/java/xyz/wbsite/jmacro/base/ViewColor.java delete mode 100644 src/main/java/xyz/wbsite/jmacro/base/ViewPoint.java delete mode 100644 src/main/java/xyz/wbsite/jmacro/base/ViewRect.java diff --git a/.idea/compiler.xml b/.idea/compiler.xml index bbf545a..a4be46d 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -7,11 +7,11 @@ - + diff --git a/pom.xml b/pom.xml index 621ac24..b1eee1f 100644 --- a/pom.xml +++ b/pom.xml @@ -3,12 +3,11 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 xyz.wbsite - jmacro + starter-jmacro 0.1 jar - - jmacro - project for jmacro + starter-jmacro + project for starter-jmacro UTF-8 @@ -55,6 +54,12 @@ jintellitype 1.4.0 + + + com.sikulix + sikulixapi + 2.0.5 + diff --git a/src/main/java/xyz/wbsite/jmacro/JMacro.java b/src/main/java/xyz/wbsite/jmacro/JMacro.java index 84aaa74..6fdc8b9 100644 --- a/src/main/java/xyz/wbsite/jmacro/JMacro.java +++ b/src/main/java/xyz/wbsite/jmacro/JMacro.java @@ -1,27 +1,20 @@ package xyz.wbsite.jmacro; -import cn.hutool.core.convert.Convert; -import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.RandomUtil; +import org.sikuli.script.FindFailed; +import org.sikuli.script.Image; +import org.sikuli.script.KeyModifier; +import org.sikuli.script.Location; +import org.sikuli.script.Match; +import org.sikuli.script.Mouse; +import org.sikuli.script.Pattern; +import org.sikuli.script.Region; +import org.sikuli.script.Screen; +import xyz.wbsite.jmacro.base.ColorLocation; import xyz.wbsite.jmacro.base.Legend; -import xyz.wbsite.jmacro.base.ViewColor; -import xyz.wbsite.jmacro.base.ViewPoint; -import xyz.wbsite.jmacro.base.ViewRect; -import xyz.wbsite.jmacro.util.ColorUtil; -import xyz.wbsite.jmacro.util.ImageUtil; import xyz.wbsite.jmacro.util.Logger; import xyz.wbsite.jmacro.util.TaskUtil; -import xyz.wbsite.jmacro.util.ValueUtil; -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.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; import java.util.concurrent.TimeUnit; /** @@ -44,24 +37,17 @@ public abstract class JMacro { private int defaultTimeOut = 10; /** - * 机器人操作实例 + * 工作区域 */ - private final Robot robot; + protected Region workRegion; /** - * 视口区域 + * 屏幕对象 */ - protected ViewRect focusRect; + protected Screen screen = new Screen(); public JMacro() { - try { - // 机器人初始化 - this.robot = new JRoot(); - this.robot.setAutoDelay(30); - this.startFocus(); - } catch (AWTException e) { - throw new RuntimeException(e); - } + this.startFocus(); } public boolean isRun() { @@ -80,17 +66,13 @@ public abstract class JMacro { this.defaultTimeOut = defaultTimeOut; } - public Robot getRobot() { - return robot; - } - /** * 开始聚焦 */ public void startFocus() { TaskUtil.asyncTask(() -> { - focusRect = TaskUtil.timeTask(this::focus, defaultTimeOut, TimeUnit.SECONDS); - if (focusRect != null) { + workRegion = TaskUtil.timeTask(this::focus, defaultTimeOut, TimeUnit.SECONDS); + if (workRegion != null) { Logger.info("聚焦成功"); } else { Logger.error("聚焦失败"); @@ -99,11 +81,11 @@ public abstract class JMacro { } /** - * 获取视口区域 + * 获取工作区域 * - * @return 视口区域 + * @return 工作区域 */ - public abstract ViewRect focus(); + public abstract Region focus(); /** * 执行脚本 @@ -137,316 +119,138 @@ public abstract class JMacro { /** * 键盘按键组 */ - public void keyInput(int... keycodes) { - for (int keycode : keycodes) { - keyPress(keycode); - } + public void keyInput(String keys) { + this.screen.type(keys); } /** * 键盘按键 * keycode Key to press (e.g. KeyEvent.VK_A) */ - public void keyPress(int keycode) { - this.robot.keyPress(keycode); + public void keyPress(String key) { + this.screen.type(key); } /** * 鼠标移动 * - * @param point 坐标点 + * @param location 坐标点 */ - public void mouseMove(ViewPoint point) { - mouseMove(point, false); - } - - /** - * 鼠标移动 - * - * @param point 坐标点 - * @param smooth 平滑移动 - */ - public void mouseMove(ViewPoint point, boolean smooth) { - int endX = point.getX(); - int endY = point.getY(); - if (smooth) { - // 获取当前鼠标位置 - Point mousePoint = MouseInfo.getPointerInfo().getLocation(); - int startX = mousePoint.x; - int startY = mousePoint.y; - - // 求两点距离 - double absX = Math.abs(startX - endX); - double absY = Math.abs(startY - endY); - double absZ = Math.sqrt(Math.pow(absX, 2) + Math.pow(absY, 2)); - int times = (int) (absZ / 15 + (absZ % 15 > 0 ? 1 : 0)); - int interval = (int) Math.max((absZ / 4) / times, 10); - - times = Math.min(times, 10); - // 分times次移动到指定点 - for (int i = 1; i <= times; i++) { - double d = ValueUtil.easeOut(i * 1.0D / times); - int dx = (int) (startX + (endX - startX) * d); - int dy = (int) (startY + (endY - startY) * d); - robot.mouseMove(dx, dy); - delay(RandomUtil.randomInt(interval - 3, interval + 3)); - } - } else { - robot.mouseMove(endX, endY); - delay(10); - } + public void mouseMove(Location location) { + Mouse.move(location); } /** * 鼠标左键单击 - * - * @param rect 矩形区域 */ - public void mouseLeftClick(ViewRect rect) { - mouseLeftClick(rect, 100); - } - - /** - * 鼠标左键单击 - * - * @param rect 矩形区域 - */ - public void mouseLeftClick(ViewRect rect, long delay) { - mouseLeftClick(new ViewPoint(rect.getCenter().getX(), rect.getCenter().getY()), delay); - } - - /** - * 鼠标左键单击 - * - * @param rect 点 - */ - public void mouseLeftClick(ViewPoint rect) { - mouseLeftClick(rect, 100); + public void mouseLeftClick() { + Mouse.at().click(); } /** * 鼠标左键单击 * - * @param rect 点 + * @param region 区域 */ - public void mouseLeftClick(ViewPoint rect, long delay) { - // 获取当前鼠标位置 - Point mousePoint = MouseInfo.getPointerInfo().getLocation(); - if (mousePoint.x != rect.getX() || mousePoint.y != rect.getY()) { - Logger.info("鼠标移动至[{},{}]", rect.getX(), rect.getY()); - mouseMove(rect); - } - mouseLeftClick(delay); + public void mouseLeftClick(Region region) { + Mouse.move(new Location(region.getX(), region.getY())); + Mouse.at().click(); } /** * 鼠标左键单击 * - * @param delay 延迟 + * @param location 坐标点 */ - public void mouseLeftClick(long delay) { - robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); - delay(delay); - robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); + public void mouseLeftClick(Location location) { + Mouse.move(location); + Mouse.at().click(); } /** - * 鼠标左键单击 - */ - public void mouseLeftClick() { - mouseLeftClick(100); - } - - /** - * 鼠标右键单击 - * - * @param rect 矩形区域 + * 鼠标左键双击 */ - public void mouseRightClick(ViewRect rect) { - mouseRightClick(rect, 100); + public void mouseLeftDoubleClick() { + Mouse.at().doubleClick(); } /** - * 鼠标右键单击 - * - * @param rect 矩形区域 + * 鼠标左键双击 */ - public void mouseRightClick(ViewRect rect, long delay) { - mouseRightClick(new ViewPoint(rect.getCenter().getX(), rect.getCenter().getY()), delay); + public void mouseLeftDoubleClick(Region region) { + Mouse.move(new Location(region.getX(), region.getY())); + Mouse.at().click(); } /** - * 鼠标右键单击 - * - * @param rect 点 + * 鼠标左键双击 */ - public void mouseRightClick(ViewPoint rect) { - mouseRightClick(rect, 100); + public void mouseLeftDoubleClick(Location location) { + Mouse.move(location); + Mouse.at().click(); } /** * 鼠标右键单击 * - * @param rect 点 + * @param region 区域 */ - public void mouseRightClick(ViewPoint rect, long delay) { - // 获取当前鼠标位置 - Point mousePoint = MouseInfo.getPointerInfo().getLocation(); - if (mousePoint.x != rect.getX() || mousePoint.y != rect.getY()) { - Logger.info("鼠标移动至[{},{}]", rect.getX(), rect.getY()); - mouseMove(rect); - } - mouseRightClick(delay); + public void mouseRightClick(Region region) { + region.getCenter().rightClick(); } /** * 鼠标右键单击 * - * @param delay 延迟 + * @param location 坐标点 */ - public void mouseRightClick(long delay) { - robot.mousePress(InputEvent.BUTTON3_DOWN_MASK); - delay(delay); - robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK); + public void mouseRightClick(Location location) { + location.rightClick(); } /** * 鼠标右键单击 */ public void mouseRightClick() { - mouseLeftClick(100); - } - - /** - * 鼠标左键拖拽 - * - * @param start 开始位置 - * @param end 结束位置 - */ - public void mouseLeftDrag(ViewPoint start, ViewPoint end) { - mouseLeftDrag(start, end, false); - } - - /** - * 鼠标左键拖拽 - * - * @param start 开始位置 - * @param end 结束位置 - * @param smooth 平滑移动 - */ - public void mouseLeftDrag(ViewPoint start, ViewPoint end, boolean smooth) { - mouseMove(start, smooth); - delay(); - robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); - delay(); - mouseMove(end, smooth); - delay(); - robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); - } - - /** - * 鼠标右键拖拽 - * - * @param start 开始位置 - * @param end 结束位置 - */ - public void mouseRightDrag(ViewPoint start, ViewPoint end) { - mouseRightDrag(start, end, false); - } - - /** - * 鼠标右键拖拽 - * - * @param start 开始位置 - * @param end 结束位置 - * @param smooth 平滑移动 - */ - public void mouseRightDrag(ViewPoint start, ViewPoint end, boolean smooth) { - mouseMove(start, smooth); - delayUnstable(); - robot.mousePress(InputEvent.BUTTON3_DOWN_MASK); - delayUnstable(); - mouseMove(end, smooth); - delayUnstable(); - robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK); - } - - /** - * 鼠标滚轮单击 - * - * @param rect 矩形区域 - */ - public void mouseWheelClick(ViewRect rect) { - delayUnstable(); - robot.mouseMove(rect.getCenter().getX(), rect.getCenter().getY()); - delayUnstable(); - robot.mousePress(InputEvent.BUTTON2_DOWN_MASK); - delayUnstable(); - robot.mouseRelease(InputEvent.BUTTON2_DOWN_MASK); - } - - /** - * 鼠标左键双击 - * - * @param rect 矩形区域 - */ - public void mouseLeftDoubleClick(ViewRect rect) { - delayUnstable(); - robot.mouseMove(rect.getCenter().getX(), rect.getCenter().getY()); - delayUnstable(); - robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); - delay(60); - robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); - delay(100); - robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); - delay(60); - robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); + mouseRightClick(Mouse.at()); } /** * 执行复制命令 */ public void sendCopyCommand() { - robot.keyPress(KeyEvent.VK_CONTROL); - robot.keyPress(KeyEvent.VK_C); - robot.keyRelease(KeyEvent.VK_C); - robot.keyRelease(KeyEvent.VK_CONTROL); + screen.type("c", KeyModifier.CTRL); } /** * 执行粘贴命令 */ public void sendPasteCommand() { - robot.keyPress(KeyEvent.VK_CONTROL); - robot.keyPress(KeyEvent.VK_V); - robot.keyRelease(KeyEvent.VK_V); - robot.keyRelease(KeyEvent.VK_CONTROL); + screen.type("v", KeyModifier.CTRL); } /** * 捕获指定区域屏幕 */ - public BufferedImage capture(Robot robot, ViewRect viewRect) { - return robot.createScreenCapture(new Rectangle(viewRect.getLeft(), viewRect.getTop(), viewRect.getWidth(), viewRect.getHeight())); + public Image capture(Region region) { + return region.getImage(); } /** * 获取聚焦区域 */ - public ViewRect getFocusRect() { - return focusRect; + public Region getWorkRegion() { + return workRegion; } /** * 获取聚焦区域 */ - public ViewRect getScreenRect() { - if (focusRect != null) { - return focusRect; + public Region getScreenRect() { + if (workRegion != null) { + return workRegion; } - // 返回屏幕区域 - Toolkit tk = Toolkit.getDefaultToolkit(); - return new ViewRect(0, 0, tk.getScreenSize().width, tk.getScreenSize().height); + return screen; } /** @@ -485,252 +289,31 @@ public abstract class JMacro { delay(RandomUtil.randomLong(millis - 100, millis + 100)); } - /** - * 全屏查找图片 - * - * @param pic 图片 - * @param minSimilar 最低相似度 - * @return 匹配区域 - */ - public ViewRect findPic(File pic, double minSimilar) { - return findPic(getFocusRect(), pic, minSimilar); - } - /** * 查找图片 * - * @param viewRect 查找范围(不设时,取全屏) - * @param pic 待查找图片 + * @param region 区域范围(不设时,取全屏) * @param minSimilar 相似度 * @return 匹配区域 */ - public ViewRect findPic(ViewRect viewRect, File pic, double minSimilar) { - if (!pic.exists()) { - Logger.info("[{}] does not exist!", pic.getAbsolutePath()); - return null; - } - if (!pic.isFile()) { - Logger.info("[{}] is not a file!", pic.getAbsolutePath()); - return null; - } - Logger.info("查找图片:{}", pic.getAbsolutePath()); - BufferedImage bufferedImage = ImageUtil.load(pic); - return findPic(viewRect, bufferedImage, minSimilar, true); + public Match findPic(Region region, String legend, double minSimilar) { + Pattern pattern = new Pattern(legend).similar(minSimilar); + return region.findBest(pattern); } /** * 查找图片 * - * @param viewRect 查找范围(不设时,取全屏) - * @param pic 待查找图片 + * @param region 区域范围(不设时,取全屏) * @param minSimilar 相似度 - * @param fast 是否快速查找(快速查找通过优先定位四个顶点+中心,因此图的边缘和中心不能存在半透明等情况) - * @return 匹配区域 - */ - public ViewRect findPic(ViewRect viewRect, BufferedImage pic, double minSimilar, boolean fast) { - // 当聚焦区域为null时,默认全屏查找 - if (viewRect == null) { - viewRect = getFocusRect() != null ? getFocusRect() : getScreenRect(); - } - // 当查找区域比图片还小时,直接返回失败 - if (viewRect.getWidth() < pic.getWidth() || viewRect.getHeight() < pic.getHeight()) { - Logger.info("视口尺寸小于图片"); - return null; - } - if (minSimilar > 1) { - throw new RuntimeException("this minSimilar must be less than 1"); - } - - // 获取实时屏幕 - BufferedImage screen = capture(robot, viewRect); - ImageUtil.show(screen); - int[][] screenData = ImageUtil.getImageRGB(screen); - int[][] picData = ImageUtil.getImageRGB(pic); - - // 得到图片左上角范围 - int xMin = viewRect.getLeft(); - // 因为坐标取像素点是从0开始,因此需要-1 - int xMax = viewRect.getRight() - pic.getWidth(); - int yMin = viewRect.getTop(); - // 因为坐标取像素点是从0开始,因此需要-1 - int yMax = viewRect.getBottom() - pic.getHeight(); - - double maxSimilar = 0; - for (int y = yMin; y <= yMax; y++) { - for (int x = xMin; x <= xMax; x++) { - if (fast) { - // 对关键点进行先期匹配,降低运算复杂度。如果关键点本身就不匹配,就没必要再去匹配小图的每一个像素点 - // 左上角 - boolean lt = ColorUtil.isSimilar(screenData[x - xMin][y - yMin], picData[0][0]); - // 右上角 - boolean rt = ColorUtil.isSimilar(screenData[x - xMin + pic.getWidth() - 1][y - yMin], picData[pic.getWidth() - 1][0]); - // 左下角 - boolean lb = ColorUtil.isSimilar(screenData[x - xMin][y - yMin + pic.getHeight() - 1], picData[0][pic.getHeight() - 1]); - // 右下角 - boolean rb = ColorUtil.isSimilar(screenData[x - xMin + pic.getWidth() - 1][y - yMin + pic.getHeight() - 1], picData[pic.getWidth() - 1][pic.getHeight() - 1]); - //中心点 - boolean cc = ColorUtil.isSimilar(screenData[x - xMin + pic.getWidth() / 2][y - yMin + pic.getHeight() / 2], picData[pic.getWidth() / 2][pic.getHeight() / 2]); - // 五个关键点不一致,则没必要继续比下去 - // 但要注意四个顶点+中心存在半透明情况,会导致关键点定位不准 - if (!lt || !rt || !lb || !rb || !cc) { - continue; - } - } - - // 统计相似点数 - int samePixels = 0; - for (int smallY = 0; smallY < pic.getHeight(); smallY++) { - for (int smallX = 0; smallX < pic.getWidth(); smallX++) { - if (ColorUtil.isSimilar(screenData[x + smallX - xMin][y + smallY - yMin], picData[smallX][smallY])) { - samePixels++; - } - } - } - // 计算相似度 - double similar = (double) samePixels / (pic.getWidth() * pic.getHeight()); - maxSimilar = Math.max(maxSimilar, similar); - if (similar >= minSimilar) { - return new ViewRect(x, y, x + pic.getWidth(), y + pic.getHeight(), similar); - } - } - } - if (fast) { - - } - Logger.info("最大相似度{}", NumberUtil.formatPercent(maxSimilar, 0)); - return null; - } - - /** - * 等待并查找图片 - * - * @param rect 查找区域 - * @param pic 图片 - * @param minSimilar 最低相似度 - * @return 匹配区域 - */ - public ViewRect waitAndFindPic(ViewRect rect, File pic, double minSimilar) { - return waitAndFindPic(rect, pic, minSimilar, defaultTimeOut); - } - - /** - * 等待并查找图片 - * - * @param pic 图片 - * @param minSimilar 最低相似度 - * @return 匹配区域 - */ - public ViewRect waitAndFindPic(File pic, double minSimilar) { - return waitAndFindPic(getFocusRect(), pic, minSimilar, defaultTimeOut); - } - - /** - * 等待并查找图片 - * - * @param pic 图片 - * @param minSimilar 最低相似度 - * @param seconds 最长等待秒数 - * @return 匹配区域 - */ - public ViewRect waitAndFindPic(File pic, double minSimilar, long seconds) { - return waitAndFindPic(getFocusRect(), pic, minSimilar, seconds); - } - - /** - * 等待并查找图片 - * - * @param rect 查找区域 - * @param picFile 图片 - * @param minSimilar 最低相似度 - * @param seconds 最长等待秒数 - * @return 匹配区域 - */ - public ViewRect waitAndFindPic(ViewRect rect, File picFile, double minSimilar, long seconds) { - Logger.info("等待并查找图片:{}", picFile.getAbsolutePath()); - BufferedImage pic = ImageUtil.load(picFile); - return TaskUtil.timeTask(() -> { - while (JMainService.getInstance().run) { - ViewRect result = findPic(rect, pic, minSimilar, true); - if (result != null) { - return result; - } - } - return null; - }, seconds, TimeUnit.SECONDS); - } - - /** - * 聚焦区域查找图例并点击 - * - * @param legend 图例 - * @param minSimilar 最低相似度 - * @return 匹配区域 - */ - public boolean findLegendAndClick(String legend, double minSimilar) { - ViewRect matchLegend = matchLegend(legend, minSimilar); - if (matchLegend != null) { - Logger.info("点击【{}】", legend); - mouseLeftClick(matchLegend); - delay(500); - return true; - } else { - return false; - } - } - - /** - * 聚焦区域查找图例并点击 - * - * @param legend 图例 - * @param minSimilar 最低相似度 - * @return 匹配区域 - */ - public boolean findLegendAndClick(Legend legend, double minSimilar) { - ViewRect matchLegend = matchLegend(legend, minSimilar); - if (matchLegend != null) { - Logger.info("点击【{}】", legend); - mouseLeftClick(matchLegend); - delay(500); - return true; - } else { - return false; - } - } - - /** - * 指定区域查找图例并点击 - * - * @param legend 图例 - * @param minSimilar 最低相似度 - * @return 匹配区域 - */ - public boolean findLegendAndClick(ViewRect rect, String legend, double minSimilar) { - ViewRect matchLegend = findLegend(rect, legend, minSimilar); - if (matchLegend != null) { - Logger.info("点击【{}】", legend); - mouseLeftClick(matchLegend); - delay(500); - return true; - } else { - return false; - } - } - - /** - * 指定区域查找图例并点击 - * - * @param legend 图例 - * @param minSimilar 最低相似度 * @return 匹配区域 */ - public boolean findLegendAndClick(ViewRect rect, Legend legend, double minSimilar) { - ViewRect matchLegend = findLegend(rect, legend, minSimilar); - if (matchLegend != null) { - Logger.info("点击【{}】", legend); - mouseLeftClick(matchLegend); - delay(500); + public boolean clickPic(Region region, String legend, double minSimilar) { + try { + Pattern pattern = new Pattern(legend).similar(minSimilar); + region.click(pattern); return true; - } else { + } catch (FindFailed e) { return false; } } @@ -742,488 +325,14 @@ public abstract class JMacro { * @param minSimilar 最低相似度 * @return 匹配区域 */ - public ViewRect findLegend(String legend, double minSimilar) { - return findLegend(Legend.inflate(legend), minSimilar); - } - - /** - * 查找图片 - * - * @param legend 图例 - * @param minSimilar 最低相似度 - * @return 匹配区域 - */ - public ViewRect findLegend(Legend legend, double minSimilar) { - return findLegend(getFocusRect(), legend, minSimilar, true); - } - - /** - * 查找图例 - * - * @param rect 范围 - * @param legend 图例 - * @param minSimilar 最低相似度 - * @return 匹配区域 - */ - public ViewRect findLegend(ViewRect rect, String legend, double minSimilar) { - return findLegend(rect, Legend.inflate(legend), minSimilar); - } - - /** - * 查找图例 - * - * @param rect 范围 - * @param legend 图例 - * @param minSimilar 最低相似度 - * @return 匹配区域 - */ - public ViewRect findLegend(ViewRect rect, Legend legend, double minSimilar) { - return findLegend(rect, legend, minSimilar, false); - } - - /** - * 查找图例 - * - * @param legend 图例 - * @param minSimilar 最低相似度 - * @return 匹配区域 - */ - public ViewRect findLegend(ViewRect rect, String legend, double minSimilar, boolean fast) { - return findLegend(rect, Legend.inflate(legend), minSimilar, fast); - } - - /** - * 查找图例 - * - * @param legend 图例 - * @param minSimilar 最低相似度 - * @return 匹配区域 - */ - public ViewRect findLegend(ViewRect rect, Legend legend, double minSimilar, boolean fast) { - Logger.info("查找图例:{}", legend.getName()); - return findPic(rect, legend.getImage(), minSimilar, fast); - } - - /** - * 等待并查找图例 - * - * @param legend 图例 - * @param minSimilar 最低相似度 - * @return 匹配区域 - */ - public ViewRect waitAndFindLegend(ViewRect rect, Legend legend, double minSimilar, long seconds) { - Logger.info("等待并查找图例:{}", legend.getName()); - return TaskUtil.timeTask(() -> { - while (JMainService.getInstance().run) { - ViewRect result = findPic(rect, legend.getImage(), minSimilar, true); - if (result != null) { - return result; - } - } - return null; - }, seconds, TimeUnit.SECONDS); - } - - /** - * 等待并查找图例 - * - * @param legend 图例 - * @param minSimilar 最低相似度 - * @return 匹配区域 - */ - public ViewRect waitAndFindLegend(ViewRect rect, String legend, double minSimilar, long seconds) { - return waitAndFindLegend(rect, Legend.inflate(legend), minSimilar, seconds); - } - - /** - * 等待并查找图例 - * - * @param legend 图例 - * @param minSimilar 最低相似度 - * @return 匹配区域 - */ - public ViewRect waitAndFindLegend(Legend legend, double minSimilar, long seconds) { - return waitAndFindLegend(getFocusRect(), legend, minSimilar, seconds); - } - - /** - * 等待并查找图例 - * - * @param legend 图例 - * @param minSimilar 最低相似度 - * @return 匹配区域 - */ - public ViewRect waitAndFindLegend(String legend, double minSimilar, long seconds) { - return waitAndFindLegend(getFocusRect(), Legend.inflate(legend), minSimilar, seconds); - } - - /** - * 等待并查找图例 - * - * @param legend 图例 - * @param minSimilar 最低相似度 - * @return 匹配区域 - */ - public ViewRect waitAndFindLegend(Legend legend, double minSimilar) { - return waitAndFindLegend(getFocusRect(), legend, minSimilar, defaultTimeOut); - } - - /** - * 等待并查找图例 - * - * @param legend 图例 - * @param minSimilar 最低相似度 - * @return 匹配区域 - */ - public ViewRect waitAndFindLegend(String legend, double minSimilar) { - return waitAndFindLegend(getFocusRect(), Legend.inflate(legend), minSimilar, defaultTimeOut); - } - - /** - * 匹配图例和点击 - * - * @param legend 图例 - * @param minSimilar 最低相似度 - * @return 是否成功 - */ - public boolean matchLegendAndClick(String legend, double minSimilar) { - ViewRect matchLegend = matchLegend(legend, minSimilar); - if (matchLegend != null) { - Logger.info("点击【{}】", legend); - mouseLeftClick(matchLegend); - delay(500); - return true; - } else { - return false; - } - } - - /** - * 匹配图例和点击 - * - * @param legend 图例 - * @param minSimilar 最低相似度 - * @return 是否成功 - */ - public boolean matchLegendAndClick(Legend legend, double minSimilar) { - ViewRect matchLegend = matchLegend(legend, minSimilar); - if (matchLegend != null) { - Logger.info("点击【{}】", legend); - mouseLeftClick(matchLegend); - delay(500); - return true; - } else { - return false; - } - } - - /** - * 匹配图例 - * - * @param legend 图例 - * @param minSimilar 最低相似度 - * @return 匹配图片 - */ - public ViewRect matchLegend(String legend, double minSimilar) { - return matchLegend(Legend.inflate(legend), minSimilar); - } - - /** - * 匹配图例 - * - * @param legend 图例 - * @param minSimilar 最低相似度 - * @return 匹配图片 - */ - public ViewRect matchLegend(Legend legend, double minSimilar) { - BufferedImage image = ImageUtil.load(legend.getFile()); - // 获取图例相对坐标 - int offsetX = legend.getLocation().getX(); - int offsetY = legend.getLocation().getY(); - // 根据原点计算图例绝对坐标 - ViewRect viewRect = new ViewRect(); - viewRect.setLeft(getFocusRect().getLeft() + offsetX); - viewRect.setTop(getFocusRect().getTop() + offsetY); - viewRect.setRight(viewRect.getLeft() + image.getWidth()); - viewRect.setBottom(viewRect.getTop() + image.getHeight()); - Logger.info("匹配图例:{},区域:{}", legend.getName(), viewRect); - return findPic(viewRect, image, minSimilar, false); - } - - /** - * 等待并匹配图例 - * - * @param legend 图例 - * @param minSimilar 最低相似度 - * @param seconds 最长等待秒数 - * @return 匹配区域 - */ - public ViewRect waitAndMatchLegend(Legend legend, double minSimilar, long seconds) { - BufferedImage image = ImageUtil.load(legend.getFile()); - // 获取图例相对坐标 - int offsetX = legend.getLocation().getX(); - int offsetY = legend.getLocation().getY(); - // 根据原点计算图例绝对坐标 - ViewRect viewRect = new ViewRect(); - viewRect.setLeft(getFocusRect().getLeft() + offsetX); - viewRect.setTop(getFocusRect().getTop() + offsetY); - viewRect.setRight(viewRect.getLeft() + image.getWidth()); - viewRect.setBottom(viewRect.getTop() + image.getHeight()); - Logger.info("等待并匹配图例:{},区域:{}", legend.getName(), viewRect); - return TaskUtil.timeTask(() -> { - while (JMainService.getInstance().run) { - ViewRect result = findPic(viewRect, image, minSimilar, false); - if (result != null) { - return result; - } - } + public Region findLegend(String legend, double minSimilar) { + Pattern pattern = new Pattern(of(legend).getFile().getAbsolutePath()) + .similar(minSimilar); + Match match = workRegion.findBest(pattern); + if (match == null) { return null; - }, seconds, TimeUnit.SECONDS); - } - - /** - * 等待并匹配图例 - * - * @param legend 图例 - * @param minSimilar 最低相似度 - * @param seconds 最长等待秒数 - * @return 匹配区域 - */ - public ViewRect waitAndMatchLegend(String legend, double minSimilar, long seconds) { - return waitAndMatchLegend(Legend.inflate(legend), minSimilar, seconds); - } - - /** - * 等待并匹配图例 - * - * @param legend 图例 - * @param minSimilar 最低相似度 - * @return 匹配区域 - */ - public ViewRect waitAndMatchLegend(String legend, double minSimilar) { - return waitAndMatchLegend(legend, minSimilar, defaultTimeOut); - } - - /** - * 等待并匹配图例 - * - * @param legend 图例 - * @param minSimilar 最低相似度 - * @return 匹配区域 - */ - public ViewRect waitAndMatchLegend(Legend legend, double minSimilar) { - return waitAndMatchLegend(legend, minSimilar, defaultTimeOut); - } - - /** - * 图例组合匹配,图例区域结果和入参长度一致,任意图例检测到后则会返回,其他未检测到的区域则为null - *

- * 说明:一个操作之后可能会有多个预期结果,以发起【进攻】操作为例,可能会提示多种结果如下: - * 1.成功发起 - * 2.敌人已逃走 - * 3.兵力不足 - * 不同的操作返回结果,会有不同的后续操作;如果以获取某预期结果去检测,考虑处理时间延迟等需要加上等待时间 - * 不同结果的获取就会依次串行,这样检查肯定会浪费大量检测时间。因此正对此情况做了并联检测机制。 - * - * @param legends 图例 - * @param minSimilar 最低相似度 - * @return 匹配图片 - */ - public ViewRect[] matchLegends(String[] legends, double minSimilar) { - ViewRect[] viewRects = new ViewRect[legends.length]; - for (int i = 0; i < legends.length; i++) { - String legend = legends[i]; - viewRects[i] = matchLegend(legend, minSimilar); - if (viewRects[i] != null) { - return viewRects; - } - } - return viewRects; - } - - /** - * 等待并匹配图例组合 - * - * @param legends 图例组 - * @param minSimilar 最低相似度 - * @param seconds 最长等待秒数 - * @return 匹配区域 - */ - public ViewRect[] waitAndMatchLegends(String[] legends, double minSimilar, long seconds) { - if (legends.length == 0) { - return new ViewRect[0]; - } - Logger.info("等待并匹配图例组合:{}", String.join(",", legends)); - ViewRect[] result = TaskUtil.timeTask(() -> { - while (JMainService.getInstance().run) { - ViewRect[] viewRects = matchLegends(legends, minSimilar); - for (ViewRect viewRect : viewRects) { - if (viewRect != null) { - return viewRects; - } - } - } - return new ViewRect[legends.length]; - }, seconds, TimeUnit.SECONDS); - if (result == null) { - result = new ViewRect[legends.length]; - } - return result; - } - - /** - * 等待并匹配图例组合 - * - * @param legends 图例组 - * @param minSimilar 最低相似度 - * @return 匹配区域 - */ - public ViewRect[] waitAndMatchLegends(String[] legends, double minSimilar) { - return waitAndMatchLegends(legends, minSimilar, defaultTimeOut); - } - - /** - * 匹配指定坐标色值 - * - * @param color 色值坐标点 - * @return 是否匹配 - */ - public boolean matchColor(ViewColor color) { - ViewRect rect = new ViewRect(color.getX(), color.getY(), color.getX(), color.getY()); - // 获取实时屏幕 - BufferedImage pointImage = capture(robot, rect); - ImageUtil.show(pointImage); - int[][] pointData = ImageUtil.getImageRGB(pointImage); - boolean similar = ColorUtil.isSimilar(pointData[0][0], color.getColor()); - Logger.info("比较色值:{},{} ==> {}", Integer.toHexString(pointData[0][0]), Integer.toHexString(color.getColor()), similar ? "相似" : "不同"); - return similar; - } - - /** - * 查找颜色值占比 0~1 - * - * @param color 颜色值例,如:#ffffff - * @return 占比 - */ - public double findColor(int left, int top, int right, int bottom, String color) { - return findColor(of(left, top, right, bottom), color); - } - - /** - * 查找颜色值占比 0~1 - * - * @param rect 查找区域 - * @param color 颜色值例,如:#ffffff - * @return 占比 - */ - public double findColor(ViewRect rect, String color) { - // 获取实时屏幕 - BufferedImage capture = capture(robot, rect); - ImageUtil.show(capture); - List colors = new ArrayList<>(); - for (int y = 0; y < capture.getHeight(); y++) { - for (int x = 0; x < capture.getWidth(); x++) { - colors.add("#" + Integer.toHexString(capture.getRGB(x, y) & 0xFFFFFF)); - } - } - Map countMap = new HashMap<>(); - for (String c : colors) { - countMap.put(c, countMap.getOrDefault(c, 0) + 1); - } - - double has = countMap.getOrDefault(color, 0); - return has / colors.size(); - } - - /** - * 指定区域查找颜色值,查到后点击 - * - * @param color 颜色值例,如:#ffffff - */ - public boolean hasColorAndClick(int left, int top, int right, int bottom, String... color) { - ViewRect viewRect = of(left, top, right, bottom); - boolean hasColor = hasColor(viewRect, color); - if (hasColor) { - Logger.info("点击【{}】", viewRect); - mouseLeftClick(viewRect); - delay(500); - return true; - } else { - return false; } - } - - /** - * 指定区域查找颜色 - * - * @param color 颜色值例,如:#ffffff - * @return 占比 - */ - public boolean hasColor(int left, int top, int right, int bottom, String... color) { - return hasColor(of(left, top, right, bottom), color); - } - - /** - * 指定区域查找颜色值,查到后点击 - * - * @param rect 查找区域 - * @param color 颜色值例,如:#ffffff - * @return 操作结果 - */ - public boolean hasColorAndClick(ViewRect rect, String... color) { - boolean hasColor = hasColor(rect, color); - if (hasColor) { - Logger.info("点击【{}】", rect); - mouseLeftClick(rect); - delay(500); - return true; - } else { - return false; - } - } - - /** - * 指定区域查找颜色值 - * - * @param rect 查找区域 - * @param color 颜色值例,如:#ffffff - * @return 占比 - */ - public boolean hasColor(ViewRect rect, String... color) { - // 获取实时屏幕 - BufferedImage capture = capture(robot, rect); - ImageUtil.show(capture); - for (int y = 0; y < capture.getHeight(); y++) { - for (int x = 0; x < capture.getWidth(); x++) { - String col = "#" + Integer.toHexString(capture.getRGB(x, y) & 0xFFFFFF); - for (String s : color) { - if (ColorUtil.isSimilar(ColorUtil.hexToColor(s), ColorUtil.hexToColor(col))) { - return true; - } - } - } - } - return false; - } - - /** - * 等待并匹配图例 - * - * @param color 色值坐标点 - * @param seconds 最长等待秒数 - * @return 匹配区域 - */ - public boolean waitAndMatchColor(ViewColor color, long seconds) { - Boolean result = TaskUtil.timeTask(() -> { - while (JMainService.getInstance().run) { - if (matchColor(color)) { - return true; - } - delayUnstable(); - } - return null; - }, seconds, TimeUnit.SECONDS); - return Convert.toBool(result, false); + return new Region(match.getRect()); } /** @@ -1235,36 +344,15 @@ public abstract class JMacro { return Legend.inflate(legend); } - /** - * 将相对坐标转为绝对坐标 - * - * @return 绝对区域 - */ - public ViewRect of(int left, int top, int right, int bottom) { - int ox = getFocusRect().getLeft(); - int oy = getFocusRect().getTop(); - return new ViewRect(left + ox, top + oy, right + ox, bottom + oy); - } - - /** - * 将相对区域转为绝对区域 - * - * @param relativeRect 相对区域 - * @return 绝对区域 - */ - public ViewRect of(ViewRect relativeRect) { - return new ViewRect(relativeRect.getLeft(), relativeRect.getTop(), relativeRect.getRight(), relativeRect.getBottom()); - } - /** * 将相对坐标转为绝对坐标 * * @return 绝对坐标 */ - public ViewPoint of(int x, int y) { - int ox = getFocusRect().getLeft(); - int oy = getFocusRect().getTop(); - return new ViewPoint(x + ox, y + oy); + public Location of(int x, int y) { + int ox = getWorkRegion().getX(); + int oy = getWorkRegion().getY(); + return new Location(x + ox, y + oy); } /** @@ -1273,7 +361,7 @@ public abstract class JMacro { * @param relativePoint 相对坐标 * @return 绝对区域 */ - public ViewPoint of(ViewPoint relativePoint) { + public Location of(Location relativePoint) { return of(relativePoint.getX(), relativePoint.getY()); } @@ -1282,10 +370,10 @@ public abstract class JMacro { * * @return 绝对坐标 */ - public ViewColor of(int x, int y, String hexColor) { - int ox = getFocusRect().getLeft(); - int oy = getFocusRect().getTop(); - return new ViewColor(x + ox, y + oy, hexColor); + public ColorLocation of(int x, int y, String hexColor) { + int ox = getWorkRegion().getX(); + int oy = getWorkRegion().getY(); + return new ColorLocation(x + ox, y + oy, hexColor); } /** @@ -1303,7 +391,7 @@ public abstract class JMacro { * @param relativePoint 相对坐标 * @return 绝对区域 */ - public ViewColor of(ViewPoint relativePoint, String hexColor) { + public ColorLocation of(Location relativePoint, String hexColor) { return of(relativePoint.getX(), relativePoint.getY(), hexColor); } } \ No newline at end of file diff --git a/src/main/java/xyz/wbsite/jmacro/JMainController.java b/src/main/java/xyz/wbsite/jmacro/JMainController.java index a2a8af7..71c320c 100644 --- a/src/main/java/xyz/wbsite/jmacro/JMainController.java +++ b/src/main/java/xyz/wbsite/jmacro/JMainController.java @@ -17,8 +17,8 @@ import javafx.scene.control.Tooltip; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.util.Duration; +import org.sikuli.script.Region; import xyz.wbsite.jmacro.base.Legend; -import xyz.wbsite.jmacro.base.ViewRect; import xyz.wbsite.jmacro.tool.Measure; import xyz.wbsite.jmacro.tool.PickLegend; import xyz.wbsite.jmacro.tool.PickPoint; @@ -99,13 +99,13 @@ public class JMainController implements Initializable { */ @FXML public void pickPoint() { - ViewRect screen = JMainService.getInstance().getMacro().getFocusRect(); + Region screen = JMainService.getInstance().getMacro().getWorkRegion(); if (screen == null) { DialogUtil.alert("未定位到视口,请稍后再试!"); return; } try { - new PickPoint(screen.getLeft(), screen.getTop()); + new PickPoint(screen.getX(), screen.getY()); } catch (AWTException awtException) { awtException.printStackTrace(); } @@ -116,13 +116,13 @@ public class JMainController implements Initializable { */ @FXML public void pickRect() { - ViewRect screen = JMainService.getInstance().getMacro().getFocusRect(); + Region screen = JMainService.getInstance().getMacro().getWorkRegion(); if (screen == null) { DialogUtil.alert("未定位到视口,请稍后再试!"); return; } try { - new PickRect(screen.getLeft(), screen.getTop()); + new PickRect(screen.getX(), screen.getY()); } catch (AWTException awtException) { awtException.printStackTrace(); } @@ -133,13 +133,13 @@ public class JMainController implements Initializable { */ @FXML public void pickLegend() { - ViewRect screen = JMainService.getInstance().getMacro().getFocusRect(); + Region screen = JMainService.getInstance().getMacro().getWorkRegion(); if (screen == null) { DialogUtil.alert("未定位到视口,请稍后再试!"); return; } try { - new PickLegend(screen.getLeft(), screen.getTop(), Legend.getDefaultBase()); + new PickLegend(screen.getX(), screen.getY(), Legend.getDefaultBase()); } catch (AWTException awtException) { awtException.printStackTrace(); } @@ -150,13 +150,13 @@ public class JMainController implements Initializable { */ @FXML public void measure() { - ViewRect screen = JMainService.getInstance().getMacro().getFocusRect(); + Region screen = JMainService.getInstance().getMacro().getWorkRegion(); if (screen == null) { DialogUtil.alert("未定位到视口,请稍后再试!"); return; } try { - new Measure(screen.getLeft(), screen.getTop()); + new Measure(screen.getX(), screen.getY()); } catch (AWTException awtException) { awtException.printStackTrace(); } diff --git a/src/main/java/xyz/wbsite/jmacro/JMainService.java b/src/main/java/xyz/wbsite/jmacro/JMainService.java index 7bb2035..a9cf746 100644 --- a/src/main/java/xyz/wbsite/jmacro/JMainService.java +++ b/src/main/java/xyz/wbsite/jmacro/JMainService.java @@ -69,7 +69,7 @@ public class JMainService { } public static boolean start() { - if (JMainService.getInstance().macro.getFocusRect() == null) { + if (JMainService.getInstance().macro.getWorkRegion() == null) { JMainService.getInstance().macro.startFocus(); DialogUtil.alert("未聚焦到视口,请稍后再试!"); return false; diff --git a/src/main/java/xyz/wbsite/jmacro/base/ColorLocation.java b/src/main/java/xyz/wbsite/jmacro/base/ColorLocation.java new file mode 100644 index 0000000..9256b2b --- /dev/null +++ b/src/main/java/xyz/wbsite/jmacro/base/ColorLocation.java @@ -0,0 +1,46 @@ +package xyz.wbsite.jmacro.base; + +import cn.hutool.core.util.StrUtil; +import org.sikuli.script.Location; + +/** + * 色值坐标点 + * + * @author wangbing + * @version 0.0.1 + * @since 1.8 + */ +public class ColorLocation extends Location { + + /** + * 颜色值 + */ + private String hexColor; + + /** + * @param x 坐标 + * @param y 坐标 + * @param hexColor rgb值 例如#FFFFFF + */ + public ColorLocation(int x, int y, String hexColor) { + super(x, y); + this.hexColor = hexColor; + } + + public ColorLocation(int x, int y, int hexColor) { + super(x, y); + this.hexColor = "#" + Integer.toHexString(hexColor); + } + + public String getHexColor() { + return hexColor; + } + + public void setHexColor(String hexColor) { + this.hexColor = hexColor; + } + + public int getHexColorInt() { + return Integer.parseInt(StrUtil.removePrefix(hexColor, "#"), 16); + } +} diff --git a/src/main/java/xyz/wbsite/jmacro/base/Legend.java b/src/main/java/xyz/wbsite/jmacro/base/Legend.java index fc10a76..2c002b0 100644 --- a/src/main/java/xyz/wbsite/jmacro/base/Legend.java +++ b/src/main/java/xyz/wbsite/jmacro/base/Legend.java @@ -5,6 +5,7 @@ import cn.hutool.cache.CacheUtil; import cn.hutool.core.convert.Convert; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.ReUtil; +import org.sikuli.script.Location; import xyz.wbsite.jmacro.util.ImageUtil; import xyz.wbsite.jmacro.util.Logger; @@ -56,7 +57,7 @@ public class Legend { /** * 图例坐标 */ - private ViewPoint location; + private Location location; public static void setDefaultBase(File base) { defaultBase = base; @@ -98,7 +99,7 @@ public class Legend { } else { int x = Convert.toInt(ReUtil.get("[\\S\\s]+#L([0-9]+),[0-9]+\\.png", file.getName(), 1), 0); int y = Convert.toInt(ReUtil.get("[\\S\\s]+#L[0-9]+,([0-9]+)\\.png", file.getName(), 1), 0); - newLegend.location = new ViewPoint(x, y); + newLegend.location = new Location(x, y); } fileCache.put(name, newLegend); @@ -121,7 +122,7 @@ public class Legend { return name; } - public ViewPoint getLocation() { + public Location getLocation() { return location; } } diff --git a/src/main/java/xyz/wbsite/jmacro/base/ViewColor.java b/src/main/java/xyz/wbsite/jmacro/base/ViewColor.java deleted file mode 100644 index 96028dc..0000000 --- a/src/main/java/xyz/wbsite/jmacro/base/ViewColor.java +++ /dev/null @@ -1,45 +0,0 @@ -package xyz.wbsite.jmacro.base; - -import cn.hutool.core.util.StrUtil; - -/** - * 色值坐标点 - * - * @author wangbing - * @version 0.0.1 - * @since 1.8 - */ -public class ViewColor extends ViewPoint { - - /** - * 颜色值 - */ - private int color; - - /** - * @param x 坐标 - * @param y 坐标 - * @param hexColor rgb值 例如#FFFFFF - */ - public ViewColor(int x, int y, String hexColor) { - super(x, y); - this.color = Integer.parseInt(StrUtil.removePrefix(hexColor, "#"), 16); - } - - public ViewColor(int x, int y, int color) { - super(x, y); - this.color = color; - } - - public int getColor() { - return color; - } - - public void setColor(int color) { - this.color = color; - } - - public String getHexColor() { - return "#" + Integer.toHexString(color); - } -} diff --git a/src/main/java/xyz/wbsite/jmacro/base/ViewPoint.java b/src/main/java/xyz/wbsite/jmacro/base/ViewPoint.java deleted file mode 100644 index 65c6dae..0000000 --- a/src/main/java/xyz/wbsite/jmacro/base/ViewPoint.java +++ /dev/null @@ -1,71 +0,0 @@ -package xyz.wbsite.jmacro.base; - -/** - * 可视坐标点 - * - * @author wangbing - * @version 0.0.1 - * @since 1.8 - */ -public class ViewPoint { - - /** - * 相对原点(左上)的x轴距离 - */ - private int x; - /** - * 相对原点(左上)的y轴距离 - */ - private int y; - - private ViewPoint() { - - } - - /** - * 默认相对坐标,需要计算 - * - * @param x 坐标x - * @param y 坐标y - */ - public ViewPoint(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 ViewPoint offset(int offsetX, int offsetY) { - return new ViewPoint(this.x + offsetX, this.y + offsetY); - } - - public ViewPoint offsetX(int offsetX) { - return new ViewPoint(this.x + offsetX, this.y); - } - - public ViewPoint offsetY(int offsetY) { - return new ViewPoint(this.x, this.y + offsetY); - } - - @Override - public String toString() { - return "ViewPoint{" + - "x=" + x + - ", y=" + y + - '}'; - } -} diff --git a/src/main/java/xyz/wbsite/jmacro/base/ViewRect.java b/src/main/java/xyz/wbsite/jmacro/base/ViewRect.java deleted file mode 100644 index 2e3687a..0000000 --- a/src/main/java/xyz/wbsite/jmacro/base/ViewRect.java +++ /dev/null @@ -1,105 +0,0 @@ -package xyz.wbsite.jmacro.base; - -/** - * 视口区域 - * - * @author wangbing - * @version 0.0.1 - * @since 1.8 - */ -public class ViewRect { - - private int left; - private int top; - private int right; - private int bottom; - - /** - * 相似度 - */ - private double similar; - - public ViewRect() { - } - - public ViewRect(int left, int top, int right, int bottom) { - this.left = left; - this.top = top; - this.right = right; - this.bottom = bottom; - } - - public ViewRect(int left, int top, int right, int bottom, double similar) { - this.left = left; - this.top = top; - this.right = right; - this.bottom = bottom; - this.similar = similar; - } - - public int getLeft() { - return left; - } - - public void setLeft(int left) { - this.left = left; - } - - public int getTop() { - return top; - } - - public void setTop(int top) { - this.top = top; - } - - public int getRight() { - return right; - } - - public void setRight(int right) { - this.right = right; - } - - public int getBottom() { - return bottom; - } - - public void setBottom(int bottom) { - this.bottom = bottom; - } - - public int getWidth() { - return this.right - this.left + 1; - } - - public int getHeight() { - return this.bottom - this.top + 1; - } - - public ViewPoint getCenter() { - return new ViewPoint((left + right) / 2, (top + bottom) / 2); - } - - public ViewRect offset(int offsetX, int offsetY) { - return new ViewRect(this.left + offsetX, this.top + offsetY, this.right + offsetX, this.bottom + offsetY); - } - - public ViewRect offsetX(int offsetX) { - return new ViewRect(this.left + offsetX, this.top, this.right + offsetX, this.bottom); - } - - public ViewRect offsetY(int offsetY) { - return new ViewRect(this.left, this.top + offsetY, this.right, this.bottom + offsetY); - } - - @Override - public String toString() { - return "ViewRect{" + - "left=" + left + - ", top=" + top + - ", right=" + right + - ", bottom=" + bottom + - '}'; - } -} diff --git a/src/main/java/xyz/wbsite/jmacro/util/ImageUtil.java b/src/main/java/xyz/wbsite/jmacro/util/ImageUtil.java index a05077b..1efe92b 100644 --- a/src/main/java/xyz/wbsite/jmacro/util/ImageUtil.java +++ b/src/main/java/xyz/wbsite/jmacro/util/ImageUtil.java @@ -3,8 +3,6 @@ package xyz.wbsite.jmacro.util; import cn.hutool.core.img.ImgUtil; import cn.hutool.core.io.IoUtil; import xyz.wbsite.jmacro.JMainApplication; -import xyz.wbsite.jmacro.JMainService; -import xyz.wbsite.jmacro.base.ViewRect; import java.awt.*; import java.awt.image.BufferedImage; @@ -109,17 +107,6 @@ public class ImageUtil { return (double) similar / (smallWidth * smallHeight); } - /** - * 展示屏幕指定区域图片 - * - * @param rect 区域 - */ - public static void show(ViewRect rect) { - Robot robot = JMainService.getInstance().getMacro().getRobot(); - BufferedImage screenCapture = robot.createScreenCapture(new Rectangle(rect.getLeft(), rect.getTop(), rect.getWidth(), rect.getHeight())); - show(screenCapture); - } - /** * 终端程序并展示图片 * diff --git a/src/main/java/xyz/wbsite/jtask/TaskImpl.java b/src/main/java/xyz/wbsite/jtask/TaskImpl.java index ff38e7d..af7ad2f 100644 --- a/src/main/java/xyz/wbsite/jtask/TaskImpl.java +++ b/src/main/java/xyz/wbsite/jtask/TaskImpl.java @@ -1,7 +1,7 @@ package xyz.wbsite.jtask; +import org.sikuli.script.Region; import xyz.wbsite.jmacro.JMacro; -import xyz.wbsite.jmacro.base.ViewRect; import xyz.wbsite.jmacro.util.Logger; /** @@ -14,11 +14,11 @@ import xyz.wbsite.jmacro.util.Logger; public class TaskImpl extends JMacro { @Override - public synchronized ViewRect focus() { + public synchronized Region focus() { Logger.info("定位工作窗口"); - ViewRect rect = getScreenRect(); + Region rect = getScreenRect(); Logger.info("聚焦窗口:" + rect.toString()); - Logger.info("聚焦窗口大小:{}x{}", rect.getRight() - rect.getLeft(), rect.getBottom() - rect.getTop()); + Logger.info("聚焦窗口大小:{}x{}", rect.getW(), rect.getH()); return rect; } @@ -29,7 +29,7 @@ public class TaskImpl extends JMacro { Logger.info("启动任务"); Logger.info("查找我的电脑图标"); - ViewRect launch = findLegend("我的电脑", 0.9); + Region launch = findLegend("我的电脑", 0.9); if (launch == null) { Logger.error("未找到我的电脑图标"); return; @@ -37,24 +37,24 @@ public class TaskImpl extends JMacro { Logger.info("启动图标坐标:", launch.toString()); Logger.info("移动鼠标"); - mouseMove(launch.getCenter(), true); + mouseMove(launch.getCenter()); Logger.info("双击我的电脑"); mouseLeftDoubleClick(launch); Logger.info("等待程序启动中,请稍等..."); delay(3 * 1000); - ViewRect windows = findLegend("我的电脑窗口", 0.9); + Region windows = findLegend("我的电脑窗口", 0.9); if (windows == null) { Logger.error("我的电脑启动失败"); return; } Logger.info("定位到我的电脑窗口"); Logger.info("移动鼠标"); - mouseMove(windows.getCenter().offsetX(100), true); + mouseMove(windows.getCenter().offset(100,0)); Logger.info("等待1秒后自动关闭"); delay(1000); - mouseLeftClick(windows.getCenter().offsetX(100)); + mouseLeftClick(windows.getCenter().offset(100,0)); Logger.info("结束任务"); }