上传备份

wjdr
王兵 1 year ago
parent 53d8b66e21
commit a041f12ccf

Before

Width:  |  Height:  |  Size: 6.5 KiB

After

Width:  |  Height:  |  Size: 6.5 KiB

Before

Width:  |  Height:  |  Size: 5.0 KiB

After

Width:  |  Height:  |  Size: 5.0 KiB

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 2.7 KiB

Before

Width:  |  Height:  |  Size: 570 KiB

After

Width:  |  Height:  |  Size: 570 KiB

Before

Width:  |  Height:  |  Size: 3.1 KiB

After

Width:  |  Height:  |  Size: 3.1 KiB

Before

Width:  |  Height:  |  Size: 7.3 KiB

After

Width:  |  Height:  |  Size: 7.3 KiB

Before

Width:  |  Height:  |  Size: 3.1 KiB

After

Width:  |  Height:  |  Size: 3.1 KiB

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Before

Width:  |  Height:  |  Size: 2.3 KiB

After

Width:  |  Height:  |  Size: 2.3 KiB

Before

Width:  |  Height:  |  Size: 3.3 KiB

After

Width:  |  Height:  |  Size: 3.3 KiB

Before

Width:  |  Height:  |  Size: 6.5 KiB

After

Width:  |  Height:  |  Size: 6.5 KiB

Before

Width:  |  Height:  |  Size: 3.4 KiB

After

Width:  |  Height:  |  Size: 3.4 KiB

Before

Width:  |  Height:  |  Size: 2.4 KiB

After

Width:  |  Height:  |  Size: 2.4 KiB

Before

Width:  |  Height:  |  Size: 3.4 KiB

After

Width:  |  Height:  |  Size: 3.4 KiB

Before

Width:  |  Height:  |  Size: 6.3 KiB

After

Width:  |  Height:  |  Size: 6.3 KiB

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

Before

Width:  |  Height:  |  Size: 3.3 KiB

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 KiB

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

@ -1,15 +1,19 @@
package com.example.jmacro.wjdr;
import com.example.jmacro.wjdr.demo.MacroForWJDR;
import com.example.jmacro.wjdr.base.ViewRect;
import com.example.jmacro.wjdr.ui.FXMLUtil;
import com.example.jmacro.wjdr.util.Logger;
import com.example.jmacro.wjdr.util.ResourceUtil;
import com.melloware.jintellitype.JIntellitype;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import java.awt.event.KeyEvent;
import java.io.File;
/**
* UI
*
@ -19,6 +23,9 @@ import javafx.stage.Stage;
*/
public class JMainApplication extends Application {
public static final int F1_SHORTCUT = 1; // 开始快捷键
public static final int F2_SHORTCUT = 2; // 结束快捷键
public static Stage primaryStage;
public static JMainController mainController;
@ -44,8 +51,35 @@ public class JMainApplication extends Application {
// 展示UI
stage.show();
JMainApplication.primaryStage = stage;
JIntellitype.getInstance().registerHotKey(F1_SHORTCUT, 0, KeyEvent.VK_F1);
JIntellitype.getInstance().registerHotKey(F2_SHORTCUT, 0, KeyEvent.VK_F2);
JIntellitype.getInstance().addHotKeyListener(identifier -> {
switch (identifier) {
case F1_SHORTCUT:
mainController.onStart();
break;
case F2_SHORTCUT:
mainController.onStop();
break;
}
});
// 服务初始化
JMainService.init(new MacroForWJDR());
JMainService.init(new JMacro() {
@Override
public ViewRect focus() {
return new ViewRect(0, 0, 200, 200);
}
@Override
public void run() {
ViewRect pic = matchLegend("回收站", 0.9);
if (pic != null) {
mouseLeftDoubleClick(pic);
}
}
}, new File("legend"));
}
public static void main(String[] args) {

@ -1,5 +1,6 @@
package com.example.jmacro.wjdr;
import com.example.jmacro.wjdr.base.Legend;
import com.example.jmacro.wjdr.base.ViewRect;
import com.example.jmacro.wjdr.tool.Capture;
import com.example.jmacro.wjdr.tool.Location;
@ -9,7 +10,6 @@ import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import java.awt.*;
import java.io.File;
/**
* UI
@ -33,15 +33,14 @@ public class JMainController {
*
*/
@FXML
private void onCapture() {
public void onCapture() {
ViewRect screen = JMainService.getInstance().getMacro().getFocusRect();
File legend = JMainService.getInstance().getMacro().getLegend();
if (screen == null) {
DialogUtil.alert("未定位到视口,请稍后再试!");
return;
}
try {
new Capture(screen.getLeft(), screen.getTop(), legend);
new Capture(screen.getLeft(), screen.getTop(), Legend.getDefaultBase());
} catch (AWTException awtException) {
awtException.printStackTrace();
}
@ -51,9 +50,8 @@ public class JMainController {
*
*/
@FXML
private void onLocation() {
public void onLocation() {
ViewRect screen = JMainService.getInstance().getMacro().getFocusRect();
File legend = JMainService.getInstance().getMacro().getLegend();
if (screen == null) {
DialogUtil.alert("未定位到视口,请稍后再试!");
return;
@ -69,7 +67,7 @@ public class JMainController {
*
*/
@FXML
private void onStart() {
public void onStart() {
boolean start = JMainService.start();
this.start.setDisable(start);
this.stop.setDisable(!start);
@ -79,7 +77,7 @@ public class JMainController {
*
*/
@FXML
private void onStop() {
public void onStop() {
boolean stop = JMainService.stop();
this.start.setDisable(!stop);
this.stop.setDisable(stop);

@ -1,6 +1,7 @@
package com.example.jmacro.wjdr;
import cn.hutool.core.thread.ThreadUtil;
import com.example.jmacro.wjdr.base.Legend;
import com.example.jmacro.wjdr.demo.MacroForWJDR;
import com.example.jmacro.wjdr.util.DialogUtil;
import com.example.jmacro.wjdr.util.Logger;
@ -54,8 +55,9 @@ public class JMainService {
return macro;
}
public static void init(JMacro macro) {
public static void init(JMacro macro, File legendDir) {
getInstance().macro = macro;
Legend.setDefaultBase(legendDir);
}
public void createDaemon() {
@ -99,17 +101,6 @@ public class JMainService {
return true;
}
/**
*
*
* @param legend
*/
public static void setLegend(File legend) {
if (getInstance().macro != null) {
getInstance().macro.setLegend(legend);
}
}
/**
* 线
*/
@ -123,9 +114,10 @@ public class JMainService {
}
while (run) {
try {
macro.run();
ThreadUtil.sleep(1000);
macro.run();
} catch (Exception e) {
e.printStackTrace();
Logger.error("异常中断");
}
}
@ -137,7 +129,7 @@ public class JMainService {
*
*/
public static void main(String[] args) {
JMainService.init(new MacroForWJDR());
JMainService.init(new MacroForWJDR(), new File("legend"));
JMainService.start();
}
}

@ -0,0 +1,119 @@
package com.example.jmacro.wjdr.base;
import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import java.io.File;
import java.util.List;
/**
*
* <p>
*
* <p>
*
* something#L0,0.png
* something
* #L0,0location
*
* @author wangbing
* @version 0.0.1
* @since 1.8
*/
public class Legend {
/**
*
*/
private static File defaultBase = new File("legend");
/**
*
*/
public static Cache<String, Legend> fileCache = CacheUtil.newLFUCache(999);
/**
*
*/
private File base;
/**
*
*/
private File file;
/**
*
*/
private String name;
/**
*
*/
private ViewPoint location;
public static void setDefaultBase(File base) {
defaultBase = base;
}
public static File getDefaultBase() {
return defaultBase;
}
private Legend() {
}
/**
*
*
* @param name
* @return
*/
public static Legend inflate(String name) {
Legend legend = fileCache.get(name);
if (legend != null) {
return legend;
}
List<File> files = FileUtil.loopFiles(defaultBase, pathname -> pathname.getName().startsWith(name));
if (files.size() == 0) {
throw new RuntimeException("Not found the legend of " + name);
}
File file = files.get(0);
Legend newLegend = new Legend();
newLegend.base = defaultBase;
newLegend.file = file;
newLegend.name = name;
if (!name.matches("[\\S\\s]+#L[0-9]+,[0-9]+\\.png")) {
newLegend.location = null;
} 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);
}
fileCache.put(name, newLegend);
return newLegend;
}
public File getBase() {
return base;
}
public File getFile() {
return file;
}
public String getName() {
return name;
}
public ViewPoint getLocation() {
return location;
}
}

@ -4,13 +4,15 @@ import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.example.jmacro.wjdr.JMacro;
import com.example.jmacro.wjdr.JMainService;
import com.example.jmacro.wjdr.base.Legend;
import com.example.jmacro.wjdr.base.ViewRect;
import com.example.jmacro.wjdr.demo.task.*;
import com.example.jmacro.wjdr.demo.task.Task_;
import com.example.jmacro.wjdr.demo.task.Task_;
import com.example.jmacro.wjdr.demo.task.Task_线;
import com.example.jmacro.wjdr.demo.task.Task_;
import com.example.jmacro.wjdr.util.Logger;
import com.example.jmacro.wjdr.util.TaskUtil;
import java.util.concurrent.TimeUnit;
/**
*
*
@ -39,7 +41,7 @@ public class MacroForWJDR extends JMacro {
public synchronized ViewRect focus() {
// 定位窗口
Logger.info("定位窗口");
ViewRect mumu = findPic("慕慕_工具栏", 0.95d);
ViewRect mumu = findPic(Legend.inflate("慕慕_工具栏").getFile(), 0.95d);
if (mumu == null) {
Logger.error("定位窗口失败");
return null;
@ -63,7 +65,7 @@ public class MacroForWJDR extends JMacro {
Logger.info("任务线程ID={}", Thread.currentThread().getId());
// 获取启动图标
Logger.info("定位启动图标");
ViewRect launch = waitAndFindPic(focusRect, "启动图标", 0.9, 10, TimeUnit.SECONDS);
ViewRect launch = waitAndFindPic(Legend.inflate("启动图标").getFile(), 0.9);
if (launch != null) {
Logger.info("启动图标坐标:", launch.toString());
Logger.info("启动程序");
@ -78,14 +80,14 @@ public class MacroForWJDR extends JMacro {
// 定位城镇
Boolean inMain = TaskUtil.retryTask(() -> {
{ // 定位弹框,关闭弹框
ViewRect rect = matchPic(focusRect, "城镇_充值_L448,36", 0.8d);
ViewRect rect = waitAndMatchLegend("城镇_充值", 0.8d);
if (rect != null) {
mouseLeftClick(rect);
Logger.info("检测到充值广告弹框,关闭弹框");
}
}
{// 定位离线收益
ViewRect rect = matchPic(focusRect, "城镇_离线收益_L204,734", 0.9);
ViewRect rect = waitAndMatchLegend("城镇_离线收益", 0.9);
if (rect != null) {
mouseLeftClick(rect);
Logger.info("检测到离线收益弹框,关闭弹框");
@ -93,7 +95,7 @@ public class MacroForWJDR extends JMacro {
}
// 定位城镇
{
ViewRect rect = matchPic(focusRect, "城镇_L456,887", 0.9);
ViewRect rect = waitAndMatchLegend("城镇", 0.9);
if (rect != null) {
Logger.info("当前区域【野外】");
Logger.info("返回区域【城镇】");
@ -102,7 +104,7 @@ public class MacroForWJDR extends JMacro {
}
}
{// 定位野外
ViewRect rect = matchPic(focusRect, "野外_L444,888", 0.9);
ViewRect rect = waitAndMatchLegend("野外", 0.9);
if (rect != null) {
Logger.info("当前区域【城镇】");
return true;

@ -13,7 +13,7 @@ public class Task_探险领取 extends BaseTask {
@Override
public void task(JMacro macro, ViewRect viewRect) {
Logger.info("定位【探险】按钮");
ViewRect TAB_ = macro.waitAndMatchPic(this.viewRect, "城镇_探险_L35,881", 0.9);
ViewRect TAB_ = macro.waitAndMatchLegend("城镇_探险", 0.9);
if (TAB_ == null) {
return;
}
@ -21,7 +21,7 @@ public class Task_探险领取 extends BaseTask {
Logger.info("点击【探险】按钮");
macro.mouseLeftClick(TAB_);
ViewRect _ = macro.waitAndMatchPic(this.viewRect, "探险_按钮_L240,854", 0.9);
ViewRect _ = macro.waitAndMatchLegend("探险_按钮", 0.9);
if (_ == null) {
Logger.info("进入探险界面失败");
return;
@ -29,26 +29,26 @@ public class Task_探险领取 extends BaseTask {
Logger.info("进入探险界面成功");
Logger.info("定位领取按钮");
ViewRect _ = macro.waitAndMatchPic(this.viewRect, "探险_不可领取_L420,667", 0.8);
ViewRect _ = macro.waitAndMatchLegend("探险_不可领取", 0.8);
if (_ != null) {
Logger.info("探险不可领取");
} else {
Logger.info("定位领取按钮");
ViewRect _ = macro.waitAndMatchPic(this.viewRect, "探险_领取_L426,667", 0.8);
ViewRect _ = macro.waitAndMatchLegend("探险_领取", 0.8);
if (_ == null) {
Logger.error("未检测到【探险_领取】领取终止");
return;
}
Logger.info("定位领取按钮成功,准备领取:{}", _.toString());
macro.mouseLeftClick(_);
ViewRect _2 = macro.waitAndMatchPic(this.viewRect, "探险_领取2_L230,664", 0.8);
ViewRect _2 = macro.waitAndMatchLegend("探险_领取2", 0.8);
if (_2 == null) {
Logger.error("任务终止");
return;
}
macro.mouseLeftClick(_2);
ViewRect _ = macro.waitAndMatchPic(this.viewRect, "探险_获得奖励_L196,218", 0.8);
ViewRect _ = macro.waitAndMatchLegend("探险_获得奖励", 0.8);
if (_ == null) {
Logger.error("任务终止");
return;
@ -56,7 +56,7 @@ public class Task_探险领取 extends BaseTask {
macro.mouseLeftClick(_);
}
ViewRect _ = macro.waitAndMatchPic(this.viewRect, "探险_返回_L4,4", 0.8);
ViewRect _ = macro.waitAndMatchLegend("探险_返回", 0.8);
if (_ == null) {
Logger.error("任务终止");
return;

@ -12,7 +12,7 @@ public class Task_收留避难者 extends BaseTask {
@Override
public void task(JMacro macro, ViewRect viewRect) {
ViewRect rect = macro.waitAndMatchPic(viewRect, "城镇_避难者_L3,174", 0.9);
ViewRect rect = macro.waitAndMatchLegend("城镇_避难者", 0.9);
if (rect == null) {
Logger.info("未发现避难者");
return;
@ -23,7 +23,7 @@ public class Task_收留避难者 extends BaseTask {
macro.mouseLeftClick(rect);
macro.delayUnstable();
ViewRect rect1 = macro.waitAndMatchPic(viewRect, "城镇_欢迎新成员_L199,773", 0.9);
ViewRect rect1 = macro.waitAndMatchLegend("城镇_欢迎新成员", 0.9);
if (rect1 == null) {
Logger.info("收留避难者异常");
return;

@ -14,7 +14,7 @@ public class Task_离线收益 extends BaseTask {
public void task(JMacro macro, ViewRect viewRect) {
macro.delay();
// 定位离线收益弹框,关闭弹框
ViewRect rect = macro.matchPic(viewRect, "城镇_离线收益_L204,734", 0.9);
ViewRect rect = macro.waitAndMatchLegend("城镇_离线收益", 0.9);
if (rect != null) {
macro.mouseLeftClick(rect);
Logger.info("检测到离线收益弹框,关闭弹框");

@ -17,12 +17,12 @@ public class Task_自动采矿 extends BaseTask {
@Override
public void task(JMacro macro, ViewRect viewRect) {
Logger.info("判断是否在野外");
ViewRect = macro.waitAndMatchPic(this.viewRect, "城镇_L449,887", 0.9);
ViewRect = macro.waitAndMatchLegend( "城镇", 0.9);
if ( != null) {
Logger.info("当前界面【野外】");
} else {
Logger.info("定位【野外】");
ViewRect = macro.waitAndMatchPic(this.viewRect, "野外_L444,888", 0.99);
ViewRect = macro.waitAndMatchLegend( "野外", 0.99);
if ( == null) {
Logger.error("定位【野外】失败,采矿终止");
return;
@ -36,7 +36,7 @@ public class Task_自动采矿 extends BaseTask {
Logger.info("定位资源搜索按钮");
// 因为搜索按钮透明,不好比对,通过定位其下面的任务图标定位
ViewRect _ = macro.waitAndMatchPic(this.viewRect, "野外_任务_L16,759", 0.9);
ViewRect _ = macro.waitAndMatchLegend( "野外_任务", 0.9);
if (_ == null) {
Logger.error("未检测到【资源搜索按钮】,采矿终止");
return;
@ -49,14 +49,13 @@ public class Task_自动采矿 extends BaseTask {
// 因为搜索按钮透明,不好比对,通过定位其下面的任务图标定位
ViewRect _ = macro.waitAndMatchPic(this.viewRect, "野外_搜索_L226,879", 0.9);
ViewRect _ = macro.waitAndMatchLegend( "野外_搜索#L226,879.png", 0.9);
if (_ == null) {
Logger.error("未检测到【野外_搜索】采矿终止");
return;
}
// todo
String[] types = new String[]{
"生肉", "木材", "煤矿", "铁矿"
};
@ -69,13 +68,13 @@ public class Task_自动采矿 extends BaseTask {
}
// 检测是否是搜索界面
ViewRect _2 = macro.waitAndMatchPic(this.viewRect, "野外_任务_L16,759", 9);
ViewRect _2 = macro.waitAndMatchLegend( "野外_任务", 9);
if (_2 != null) {
Logger.info("退出资源搜索界面");
macro.mouseLeftClick(this.viewRect);
}
ViewRect 1 = macro.waitAndMatchPic(this.viewRect, "城镇_L456,887", 0.9);
ViewRect 1 = macro.waitAndMatchLegend( "城镇", 0.9);
if (1 == null) {
Logger.error("未检测到【城镇】,采矿终止");
return;

@ -1,15 +1,26 @@
package com.example.jmacro.wjdr.tool;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.StaticLog;
import com.example.jmacro.wjdr.base.Legend;
import com.example.jmacro.wjdr.util.DialogUtil;
import com.example.jmacro.wjdr.util.Logger;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;
import java.util.prefs.Preferences;
/**
@ -40,23 +51,26 @@ public class Capture extends JFrame {
/**
*
*/
private Color mask = new Color(0, 0, 0, 0.2f);
private final Color mask = new Color(0, 0, 0, 0.2f);
/**
* 线
*/
private Stroke focusWindow = new BasicStroke(1.0f);
private final Stroke focusWindow = new BasicStroke(1.0f);
/**
*
*/
private Point start = new Point(0, 0);
private final Point start = new Point(0, 0);
/**
*
*/
private Point end = new Point(0, 0);
private final Point end = new Point(0, 0);
/**
*
*/
private Preferences preferences;
/**
@ -192,8 +206,8 @@ public class Capture extends JFrame {
// 上一次保存目录
String lastPath = preferences.get(LAST_PATH, "");
// 默认文件名
String location = "_L" + (getCaptureX() - originX) + "," + (getCaptureY() - originY);
String filename = System.currentTimeMillis() + location + ".png";
String location = "L" + (getCaptureX() - originX) + "," + (getCaptureY() - originY);
String filename = StrUtil.format("{}#{}.png", System.currentTimeMillis(), location);
JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setFileFilter(new FileNameExtensionFilter("png", "png"));
@ -204,8 +218,23 @@ public class Capture extends JFrame {
if (returnVal == JFileChooser.APPROVE_OPTION) {
// 默认文件
File file = jFileChooser.getSelectedFile();
// 修正坐标
filename = file.getName().replaceAll("(_L[0-9]+,[0-9]+)?.png", "") + location + ".png";
String name = file.getName();
String legendName = name.split("#")[0];
String prefix = legendName + "#";
List<File> files = FileUtil.loopFiles(Legend.getDefaultBase(), pathname -> pathname.getName().startsWith(prefix));
if (files.size() > 0) {
DialogUtil.confirm("提示", "存在相同图例,是否旧的删除?", result -> {
if (result) {
for (File file1 : files) {
file1.deleteOnExit();
}
}
});
}
// 图例文件名
filename = StrUtil.format("{}#{}.png", legendName, location);
file = new File(file.getParent(), filename);
Logger.info("保存路径" + file);

@ -17,8 +17,8 @@
</Label>
<HBox alignment="CENTER_LEFT" layoutX="10.0" layoutY="46.0" prefHeight="35.0" prefWidth="200.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="35.0">
<children>
<Button fx:id="start" focusTraversable="false" mnemonicParsing="false" onMouseClicked="#onStart" prefHeight="30.0" prefWidth="60.0" text="开始" />
<Button fx:id="stop" disable="true" focusTraversable="false" mnemonicParsing="false" onMouseClicked="#onStop" prefHeight="30.0" prefWidth="60.0" text="停止">
<Button fx:id="start" focusTraversable="false" mnemonicParsing="false" onMouseClicked="#onStart" prefHeight="30.0" prefWidth="60.0" text="开始F1" />
<Button fx:id="stop" disable="true" focusTraversable="false" mnemonicParsing="false" onMouseClicked="#onStop" prefHeight="30.0" prefWidth="60.0" text="停止F2">
<HBox.margin>
<Insets left="10.0" />
</HBox.margin>

Loading…
Cancel
Save

Powered by TurnKey Linux.