上传备份

wjdr
王兵 1 year ago
parent db389cb76d
commit 53d8b66e21

@ -57,6 +57,10 @@ public abstract class JMacro {
this.legend = legend;
}
public Robot getRobot() {
return robot;
}
public File getLegend() {
return legend;
}
@ -401,11 +405,30 @@ public abstract class JMacro {
//中心点
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) {
// 进行全像素匹配
double similar = ImageUtil.calcSimilar(x - xMin, y - yMin, pic.getHeight(), pic.getWidth(), screenData, picData);
// 统计相似点数
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;
}
}
}
}
@ -575,13 +598,13 @@ public abstract class JMacro {
*/
public ViewRect waitAndMatchPic(ViewRect rect, File file, double minSimilar, long time, TimeUnit unit) {
return TaskUtil.timeTask(() -> {
// TODO: 2024/9/1 因为另起线程,导致发起线程已停止,但此线程未停止
while (true) {
while (JMainService.getInstance().run) {
ViewRect matchPic = matchPic(rect, file, minSimilar);
if (matchPic != null) {
return matchPic;
}
}
return null;
}, time, unit);
}

@ -33,7 +33,7 @@ public class JMainApplication extends Application {
mainController = mainLoader.getController();
stage.setScene(new Scene(mainLoader.getRoot()));
stage.centerOnScreen();
stage.show();
stage.setResizable(false);
stage.setOnCloseRequest(event -> {
stage.close();
System.exit(0);

@ -3,7 +3,7 @@ package com.example.jmacro.wjdr;
import com.example.jmacro.wjdr.base.ViewRect;
import com.example.jmacro.wjdr.tool.Capture;
import com.example.jmacro.wjdr.tool.Location;
import com.example.jmacro.wjdr.util.Dialog;
import com.example.jmacro.wjdr.util.DialogUtil;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
@ -37,7 +37,7 @@ public class JMainController {
ViewRect screen = JMainService.getInstance().getMacro().getFocusRect();
File legend = JMainService.getInstance().getMacro().getLegend();
if (screen == null) {
Dialog.alert("未定位到视口,请稍后再试!");
DialogUtil.alert("未定位到视口,请稍后再试!");
return;
}
try {
@ -55,7 +55,7 @@ public class JMainController {
ViewRect screen = JMainService.getInstance().getMacro().getFocusRect();
File legend = JMainService.getInstance().getMacro().getLegend();
if (screen == null) {
Dialog.alert("未定位到视口,请稍后再试!");
DialogUtil.alert("未定位到视口,请稍后再试!");
return;
}
try {

@ -2,7 +2,7 @@ package com.example.jmacro.wjdr;
import cn.hutool.core.thread.ThreadUtil;
import com.example.jmacro.wjdr.demo.MacroForWJDR;
import com.example.jmacro.wjdr.util.Dialog;
import com.example.jmacro.wjdr.util.DialogUtil;
import com.example.jmacro.wjdr.util.Logger;
import java.io.File;
@ -71,7 +71,7 @@ public class JMainService {
public static boolean start() {
if (JMainService.getInstance().macro.getFocusRect() == null) {
JMainService.getInstance().macro.startFocus();
Dialog.alert("未聚焦到视口,请稍后再试!");
DialogUtil.alert("未聚焦到视口,请稍后再试!");
return false;
}
if (JMainService.getInstance().run) {

@ -2,7 +2,7 @@ package com.example.jmacro.wjdr.tool;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.StaticLog;
import com.example.jmacro.wjdr.util.Dialog;
import com.example.jmacro.wjdr.util.DialogUtil;
import com.example.jmacro.wjdr.util.Logger;
import javax.swing.*;
@ -97,7 +97,7 @@ public class Location extends JFrame {
Logger.info("屏幕坐标:[{},{}]", x, y);
Logger.info("相对坐标:[{},{}]", x - originX, y - originY);
close();
Dialog.confirm(StrUtil.format("屏幕坐标:[{},{}]\n相对坐标[{},{}]", x, y, x - originX, y - originY));
DialogUtil.confirm(StrUtil.format("屏幕坐标:[{},{}]\n相对坐标[{},{}]", x, y, x - originX, y - originY));
}
});

@ -30,6 +30,4 @@ public class FXMLUtil {
fxmlLoader.load();
return fxmlLoader;
}
}

@ -42,25 +42,4 @@ public class ColorUtil {
}
return calculateSimilarity(colorInt1, colorInt2) > 0.95d;
}
public static void main(String[] args) {
// BufferedImage legend = Imager.load(new File("legend", "未标题-1.png"));
// int[][] imageRGB = Imager.getImageRGB(legend);
// for (int[] ints : imageRGB) {
//
// for (int anInt : ints) {
//
// int alpha = anInt>>24 & 0xff;
// System.out.println("alpha="+alpha);
// System.out.println(Integer.toHexString(anInt));
// }
// }
//
// int color1 = 0xffffff; // 白色
// int color2 = 0xfefefe; // 非常接近白色的颜色
//
// double similarity = calculateSimilarity(color1, color2);
// System.out.println("Similarity: " + similarity); // 输出相似度
}
}

@ -32,7 +32,7 @@ import java.util.Optional;
import java.util.Timer;
import java.util.TimerTask;
public class Dialog {
public class DialogUtil {
private static Stage popup;

@ -1,16 +1,11 @@
package com.example.jmacro.wjdr.util;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.thread.ThreadUtil;
import com.example.jmacro.wjdr.JMainApplication;
import com.example.jmacro.wjdr.JMainService;
import com.example.jmacro.wjdr.base.ViewRect;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@ -115,32 +110,17 @@ public class ImageUtil {
return (double) similar / (smallWidth * smallHeight);
}
public static void main(String[] args) {
try {
Robot robot = new Robot();
BufferedImage screenCapture = robot.createScreenCapture(new Rectangle(0, 0, 1920, 1080));
show(screenCapture);
} catch (AWTException e) {
e.printStackTrace();
}
}
/**
*
*
* @param rect
* @param rect
*/
public static void show(ViewRect rect) {
try {
Robot robot = new Robot();
BufferedImage screenCapture = robot.createScreenCapture(new Rectangle(rect.getLeft(), rect.getTop(), rect.getWidth(), rect.getHeight()));
show(screenCapture);
} catch (AWTException e) {
e.printStackTrace();
}
Robot robot = JMainService.getInstance().getMacro().getRobot();
BufferedImage screenCapture = robot.createScreenCapture(new Rectangle(rect.getLeft(), rect.getTop(), rect.getWidth(), rect.getHeight()));
show(screenCapture);
}
/**
*
*
@ -158,7 +138,7 @@ public class ImageUtil {
}
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImgUtil.write(showImage,"PNG",outputStream);
ImgUtil.write(showImage, "PNG", outputStream);
javafx.scene.image.Image image1 = new javafx.scene.image.Image(new ByteArrayInputStream(outputStream.toByteArray()));
JMainApplication.mainController.getPreview().setImage(image1);
}

@ -3,6 +3,7 @@ package com.example.jmacro.wjdr.util;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.cron.CronUtil;
import com.example.jmacro.wjdr.base.ViewRect;
import java.util.ArrayList;
import java.util.List;

Loading…
Cancel
Save

Powered by TurnKey Linux.