上传备份

wjdr
王兵 1 year ago
parent 60ea0ac2d4
commit af32fd67c4

@ -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;
}
}

Loading…
Cancel
Save

Powered by TurnKey Linux.