diff --git a/src/main/java/com/example/jmacro/wjdr/JMacro.java b/src/main/java/com/example/jmacro/wjdr/JMacro.java
index 4731f1b..9ee2935 100644
--- a/src/main/java/com/example/jmacro/wjdr/JMacro.java
+++ b/src/main/java/com/example/jmacro/wjdr/JMacro.java
@@ -2,6 +2,7 @@ package com.example.jmacro.wjdr;
import cn.hutool.core.util.RandomUtil;
import com.example.jmacro.wjdr.base.Legend;
+import com.example.jmacro.wjdr.base.ViewColor;
import com.example.jmacro.wjdr.base.ViewPoint;
import com.example.jmacro.wjdr.base.ViewRect;
import com.example.jmacro.wjdr.util.ColorUtil;
@@ -38,7 +39,7 @@ public abstract class JMacro {
try {
// 机器人初始化
this.robot = new JRoot();
- this.robot.setAutoDelay(100);
+ this.robot.setAutoDelay(30);
this.startFocus();
} catch (AWTException e) {
throw new RuntimeException(e);
@@ -120,7 +121,7 @@ public abstract class JMacro {
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 / 30 + (absZ % 30 > 0 ? 1 : 0));
+ int times = (int) (absZ / 10 + (absZ % 10 > 0 ? 1 : 0));
int interval = Math.min(500 / times, 10);
times = Math.min(times, 10);
@@ -130,7 +131,7 @@ public abstract class JMacro {
int dx = (int) (startX + (endX - startX) * d);
int dy = (int) (startY + (endY - startY) * d);
robot.mouseMove(dx, dy);
- delay(RandomUtil.randomInt(interval - 10, interval + 10));
+ delay(RandomUtil.randomInt(interval - 5, interval + 5));
}
} else {
robot.mouseMove(endX, endY);
@@ -822,6 +823,21 @@ public abstract class JMacro {
return waitAndMatchPic(legend.getFile(), legend.getLocation(), minSimilar, seconds);
}
+ /**
+ * 匹配指定坐标色值
+ *
+ * @param color 色值坐标点
+ * @return 是否匹配
+ */
+ public boolean matchColor(ViewColor color) {
+ ViewRect of = of(color.getX(), color.getY(), color.getX(), color.getY());
+ // 获取实时屏幕
+ BufferedImage pointImage = capture(robot, of);
+ ImageUtil.show(pointImage);
+ int[][] pointData = ImageUtil.getImageRGB(pointImage);
+ return ColorUtil.isSimilar(pointData[0][0], color.getColor());
+ }
+
/**
* 将相对坐标转为绝对坐标
*
@@ -863,4 +879,25 @@ public abstract class JMacro {
public ViewPoint of(ViewPoint relativePoint) {
return of(relativePoint.getX(), relativePoint.getY());
}
+
+ /**
+ * 将相对坐标转为色值坐标
+ *
+ * @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);
+ }
+
+ /**
+ * 将相对坐标转为色值坐标
+ *
+ * @param relativePoint 相对坐标
+ * @return 绝对区域
+ */
+ public ViewColor of(ViewPoint relativePoint, String hexColor) {
+ return of(relativePoint.getX(), relativePoint.getY(), hexColor);
+ }
}
\ No newline at end of file
diff --git a/src/main/java/com/example/jmacro/wjdr/base/ViewColor.java b/src/main/java/com/example/jmacro/wjdr/base/ViewColor.java
new file mode 100644
index 0000000..eb35136
--- /dev/null
+++ b/src/main/java/com/example/jmacro/wjdr/base/ViewColor.java
@@ -0,0 +1,45 @@
+package com.example.jmacro.wjdr.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 argb值 例如#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/com/example/jmacro/wjdr/tool/Location.java b/src/main/java/com/example/jmacro/wjdr/tool/Location.java
index f4a76a9..b71eaa2 100644
--- a/src/main/java/com/example/jmacro/wjdr/tool/Location.java
+++ b/src/main/java/com/example/jmacro/wjdr/tool/Location.java
@@ -99,13 +99,19 @@ public class Location extends JFrame {
super.mouseReleased(e);
x = e.getX();
y = e.getY();
- Logger.info("坐标采集:");
+ Logger.info("=========================坐标采集=========================");
Logger.info("屏幕坐标:[{},{}]", x, y);
- Logger.info("相对坐标:[{},{}],用法:macro.of({},{})", x - originX, y - originY, x - originX, y - originY);
+ Logger.info("相对坐标:[{},{}]", x - originX, y - originY);
String hexColor = Integer.toHexString(capture.getRGB(x, y) & 0xFFFFFF);
Logger.info("坐标色值:[#{}]", hexColor);
+
+ Logger.info("用法示例:");
+ Logger.info("1、坐标点击:mouseLeftClick(of({},{}));", x - originX, y - originY);
+ Logger.info("2、色值比对:matchColor(of({},{},{}));", x - originX, y - originY, hexColor);
+ Logger.info("=========================坐标采集=========================");
+
close();
- JOptionPane.showConfirmDialog(null, StrUtil.format("屏幕坐标:[{},{}]\n相对坐标:[{},{}]\n坐标色值:[#{}]", x, y, x - originX, y - originY, hexColor), "坐标信息", 1);
+ JOptionPane.showMessageDialog(null, StrUtil.format("屏幕坐标:[{},{}]\n相对坐标:[{},{}]\n坐标色值:[#{}]", x, y, x - originX, y - originY, hexColor), "坐标信息", 1);
}
});
diff --git a/src/main/java/com/example/jmacro/wjdr/util/ColorUtil.java b/src/main/java/com/example/jmacro/wjdr/util/ColorUtil.java
index 05e3bf6..9036c20 100644
--- a/src/main/java/com/example/jmacro/wjdr/util/ColorUtil.java
+++ b/src/main/java/com/example/jmacro/wjdr/util/ColorUtil.java
@@ -10,11 +10,17 @@ package com.example.jmacro.wjdr.util;
public class ColorUtil {
/**
- * 计算两个颜色的相似度
+ * 相似都阈值
+ * 因设备差异或截图工具,可能会图片或色值存在色差,对此需要一定的容忍度
+ */
+ private static final double THRESHOLD_OF_SIMILARITY = 0.95D;
+
+ /**
+ * 计算两个argb颜色值的相似度
*
- * @param colorInt1 颜色1
- * @param colorInt2 颜色2
- * @return
+ * @param colorInt1 颜色值1
+ * @param colorInt2 颜色值2
+ * @return 是否相似
*/
public static double calculateSimilarity(int colorInt1, int colorInt2) {
// 计算RGB各分量的平方差
@@ -31,6 +37,13 @@ public class ColorUtil {
return 1 - distance / Math.sqrt(3 * 255 * 255);
}
+ /**
+ * 判断两个argb颜色值是否相似
+ *
+ * @param colorInt1 颜色值1
+ * @param colorInt2 颜色值2
+ * @return 是否相似
+ */
public static boolean isSimilar(int colorInt1, int colorInt2) {
// 透明色认为相似
if ((colorInt1 >> 24 & 0xff) == 0) {
@@ -40,6 +53,6 @@ public class ColorUtil {
if ((colorInt2 >> 24 & 0xff) == 0) {
return true;
}
- return calculateSimilarity(colorInt1, colorInt2) > 0.95d;
+ return calculateSimilarity(colorInt1, colorInt2) > THRESHOLD_OF_SIMILARITY;
}
}
diff --git a/src/main/resources/main.fxml b/src/main/resources/main.fxml
index cb9d9c6..2d5d3af 100644
--- a/src/main/resources/main.fxml
+++ b/src/main/resources/main.fxml
@@ -45,10 +45,15 @@
-
-
-
-
+
+
+
+
+
+
+
+
+