diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml
new file mode 100644
index 0000000..8a7375f
--- /dev/null
+++ b/.idea/inspectionProfiles/Project_Default.xml
@@ -0,0 +1,66 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml
index 1cd7508..e7b50fd 100644
--- a/.idea/jarRepositories.xml
+++ b/.idea/jarRepositories.xml
@@ -11,6 +11,11 @@
+
+
+
+
+
diff --git a/.idea/misc.xml b/.idea/misc.xml
index d5cd614..911d29d 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -1,4 +1,3 @@
-
diff --git a/.idea/smartfox_info.xml b/.idea/smartfox_info.xml
new file mode 100644
index 0000000..1c2584f
--- /dev/null
+++ b/.idea/smartfox_info.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/anchor/主页.png b/anchor/主页.png
deleted file mode 100644
index b157aaa..0000000
Binary files a/anchor/主页.png and /dev/null differ
diff --git a/anchor/野外.png b/legend/anchor/野外.png
similarity index 100%
rename from anchor/野外.png
rename to legend/anchor/野外.png
diff --git a/legend/main/主页.png b/legend/main/主页.png
new file mode 100644
index 0000000..ca9c28f
Binary files /dev/null and b/legend/main/主页.png differ
diff --git a/src/main/java/com/example/jmacro/CutImageTool.java b/src/main/java/com/example/jmacro/CutImageTool.java
deleted file mode 100644
index 5286ea2..0000000
--- a/src/main/java/com/example/jmacro/CutImageTool.java
+++ /dev/null
@@ -1,58 +0,0 @@
-//package com.example.jmacro;
-//
-//import java.awt.AWTException;
-//import java.awt.Point;
-//import java.awt.Rectangle;
-//import java.awt.Toolkit;
-//import java.awt.image.BufferedImage;
-//
-//public class CutImageTool {
-//
-// /**
-// * 截取指定左上角坐标,右下角坐标的屏幕区域图像
-// */
-// public static BufferedImage getScreeImage(int topLeftX, int topLeftY, int bottomRightX, int bottomRightY) throws AWTException {
-// int width = bottomRightX - topLeftX;
-// int height = bottomRightY - topLeftY;
-// Rectangle rectangle = new Rectangle(topLeftX, topLeftY, width, height);
-// return CommonUtil.getRobot().createScreenCapture(rectangle);
-// }
-//
-// /**
-// * 截取指定左上角坐标,右下角坐标的屏幕区域图像
-// * @param topLeft
-// * @param bottomRight
-// * @return
-// * @throws AWTException
-// */
-// public static BufferedImage getScreeImage(Point topLeft, Point bottomRight) throws AWTException {
-// int X = topLeft.x;
-// int Y = topLeft.y;
-// int width = bottomRight.x - topLeft.x;
-// int height = bottomRight.y - topLeft.y;
-// Rectangle rectangle = new Rectangle(X, Y, width, height);
-// return CommonUtil.getRobot().createScreenCapture(rectangle);
-// }
-//
-// /**
-// * 截取指定屏幕区域图像
-// * @param topLeft
-// * @param bottomRight
-// * @return
-// * @throws AWTException
-// */
-// public static BufferedImage getScreeImage(Rectangle rectangle) throws AWTException {
-// return CommonUtil.getRobot().createScreenCapture(rectangle);
-// }
-//
-// /**
-// * 截取全屏幕图像
-// * @return
-// * @throws AWTException
-// */
-// public static BufferedImage getFullScreeImage() throws AWTException {
-// Rectangle rectangle = new Rectangle(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
-// return CommonUtil.getRobot().createScreenCapture(rectangle);
-// }
-//}
-//
diff --git a/src/main/java/com/example/jmacro/ImageMatchTools.java b/src/main/java/com/example/jmacro/ImageMatchTools.java
deleted file mode 100644
index df962e8..0000000
--- a/src/main/java/com/example/jmacro/ImageMatchTools.java
+++ /dev/null
@@ -1,297 +0,0 @@
-//package com.example.jmacro;
-//
-//import java.awt.AWTException;
-//import java.awt.Point;
-//import java.awt.image.BufferedImage;
-//import java.io.File;
-//import java.io.FileInputStream;
-//import java.io.FileNotFoundException;
-//import java.io.IOException;
-//import java.io.ObjectInputStream;
-//import java.util.ArrayList;
-//import java.util.List;
-//
-//
-///**
-// * 图像匹配
-// * @author Jiyh
-// *
-// */
-//public class ImageMatchTools {
-//
-// /**
-// * 全屏幕查找指定区域的图像
-// * @param topLeft
-// * @param bottomRight
-// * @return
-// * @throws AWTException
-// */
-// public static int[] findImageInFullScreen(Point topLeft, Point bottomRight) throws AWTException {
-// BufferedImage smallImage = CutImageTool.getScreeImage(topLeft, bottomRight);
-// BufferedImage screencapture = CutImageTool.getFullScreeImage();
-//
-// return findImage(screencapture, smallImage);
-// }
-//
-// /**
-// * 全屏幕区域查找指定文件代表的图像
-// * @param bigImage
-// * @param smallImageFileName
-// * @return
-// * @throws AWTException
-// */
-// public static int[] findImage( String smallImageFileName) throws AWTException {
-// BufferedImage screenImage = CutImageTool.getFullScreeImage();
-// return findImage(screenImage, ImageMatchTools.readImageRGB2Object(smallImageFileName));
-// }
-//
-// /**
-// * 在指定屏幕区域查找指定文件代表的图像
-// * @param bigImage
-// * @param smallImageFileName
-// * @return
-// */
-// public static int[] findImage(BufferedImage creenImage, String smallImageFileName) {
-// return findImage(creenImage, ImageMatchTools.readImageRGB2Object(smallImageFileName));
-// }
-//
-// /**
-// * 在游戏屏幕区域查找指定文件代表的图像
-// * @param gameScreenTopLeft
-// * @param gameScreenBottomRight
-// * @param smallImageFileName
-// * @return
-// * @throws AWTException
-// */
-// public static int[] findImageInGameScreen( String smallImageFileName) throws AWTException {
-// BufferedImage gameScreenImage = CutImageTool.getScreeImage(GameScreen.topLeft, GameScreen.bottomRight);
-// return findImage(gameScreenImage, ImageMatchTools.readImageRGB2Object(smallImageFileName));
-// }
-//
-// /**
-// * 在指定屏幕区域查找指定二位数组代表的图像
-// * @param bigImage
-// * @param smallRgbArray
-// * @return
-// */
-// public static int[] findImage(BufferedImage bigImage, int[][] smallRgbArray) {
-// int bigWidth = bigImage.getWidth();
-//
-// int bigHeight = bigImage.getHeight();
-//
-// int smallWidth = smallRgbArray.length;
-//
-// int smallHeight = smallRgbArray[0].length;
-//
-// int[][] bigData = getImageRGB(bigImage);
-//
-// int[][] smallData = smallRgbArray;
-//
-// int[] target = { -1, -1 };
-//
-// int yEnd = bigHeight - smallHeight;
-// int xEnd = bigWidth - smallWidth;
-//
-// for (int y = 0; y < yEnd; y++) {
-// for (int x = 0; x < xEnd; x++) {
-// // 对关键点进行先期匹配,降低运算复杂度。如果关键点本身就不匹配,就没必要再去匹配小图的每一个像素点
-// if (bigData[x][y] == smallData[0][0] // 左上角
-// && bigData[x + smallWidth - 1][y]
-// == smallData[smallWidth - 1][0] // 右上角
-// && bigData[x][y + smallHeight - 1] == smallData[0][smallHeight - 1] // 左下角
-// && bigData[x + smallWidth - 1][y + smallHeight - 1] == smallData[smallWidth - 1][smallHeight
-// - 1] // 右下角
-// && bigData[x + smallWidth / 2][y + smallHeight / 2] == smallData[smallWidth / 2][smallHeight
-// / 2]) {
-//
-// // 进行全像素匹配
-// boolean isMatched = checkAllMatch(x, y, smallHeight, smallWidth, bigData, smallData);
-// if (isMatched) {
-// System.out.println("像素点X" + x + " : Y" + y + ",对应的值为:" + bigData[x][y]);
-//
-// // 获取小图的中心位置的点
-// int centerX = x + smallWidth/2;
-// int centerY = y + smallHeight/2;
-// target[0] = centerX;
-// target[1] = centerY;
-// return target;
-// }
-// }
-//
-// }
-// }
-//
-// return target;
-// }
-//
-// /**
-// * 在指定屏幕区域查找指定图像
-// * @param bigImage
-// * @param smallImage
-// * @return
-// */
-// public static int[] findImage(BufferedImage bigImage, BufferedImage smallImage) {
-//
-// int bigWidth = bigImage.getWidth();
-//
-// int bigHeight = bigImage.getHeight();
-//
-// int smallWidth = smallImage.getWidth();
-//
-// int smallHeight = smallImage.getHeight();
-//
-// int[][] bigData = getImageRGB(bigImage);
-//
-// int[][] smallData = getImageRGB(smallImage);
-//
-// int[] target = { -1, -1 };
-//
-// int yEnd = bigHeight - smallHeight;
-// int xEnd = bigWidth - smallWidth;
-//
-// for (int y = 0; y < yEnd; y++) {
-// for (int x = 0; x < xEnd; x++) {
-// // 对关键点进行先期匹配,降低运算复杂度。如果关键点本身就不匹配,就没必要再去匹配小图的每一个像素点
-// if (bigData[x][y] == smallData[0][0] // 左上角
-// && bigData[x + smallWidth - 1][y] == smallData[smallWidth - 1][0] // 右上角
-// && bigData[x][y + smallHeight - 1] == smallData[0][smallHeight - 1] // 左下角
-// && bigData[x + smallWidth - 1][y + smallHeight - 1] == smallData[smallWidth - 1][smallHeight
-// - 1] // 右下角
-// && bigData[x + smallWidth / 2][y + smallHeight / 2] == smallData[smallWidth / 2][smallHeight
-// / 2]) {
-//
-// // 进行全像素匹配
-// boolean isMatched = checkAllMatch(x, y, smallHeight, smallWidth, bigData, smallData);
-// if (isMatched) {
-// System.out.println("像素点X" + x + " : Y" + y + ",对应的值为:" + bigData[x][y]);
-//
-// // 获取小图的中心位置的点
-// int centerX = x + smallWidth/2;
-// int centerY = y + smallHeight/2;
-// target[0] = centerX;
-// target[1] = centerY;
-// return target;
-// }
-// }
-//
-// }
-// }
-//
-// return target;
-// }
-//
-// private static boolean checkAllMatch(int x, int y, int smallHeight, int smallWidth, int[][] bigData,
-// int[][] smallData) {
-// boolean isMatched = true;
-// for (int smallY = 0; smallY < smallHeight; smallY++) {
-// for (int smallX = 0; smallX < smallWidth; smallX++) {
-// // 如果发现有一个像素点,两者的值不一样,则认为不相等,如果不相等,则没必要继续比较其它点.
-// if (bigData[x + smallX][y + smallY] != smallData[smallX][smallY]) {
-// isMatched = false;
-// return isMatched;
-// }
-// }
-// }
-// return isMatched;
-// }
-//
-// public static int[][] getImageRGB(BufferedImage bfImage) {
-// int width = bfImage.getWidth();
-//
-// int height = bfImage.getHeight();
-//
-// int[][] result = new int[width][height];
-//
-// for (int y = 0; y < height; y++) {
-// for (int x = 0; x < width; x++) {
-// // 对某个像素点的RGB编码并存入数据库
-// result[x][y] = bfImage.getRGB(x, y) & 0xFFFFFF;
-//
-// // 单独获取每一个像素点的Red,Green,和Blue的值。
-//
-// // int r = (bfImage.getRGB(x, y) & 0xFF0000) >> 16;
-//
-// // int g = (bfImage.getRGB(x, y) & 0xFF00) >> 8;
-//
-// // int b = bfImage.getRGB(x, y) & 0xFF;
-//
-// }
-//
-// }
-//
-// return result;
-//
-// }
-//
-// public static int[][] readImageRGB(String fileName) {
-// List rgbList = new ArrayList();
-// TxtFileReader tfr = null;
-// try {
-// tfr = new TxtFileReader(fileName);
-// String line = null;
-// while ((line = tfr.readLine()) != null){
-// String[] rgbStringArray = line.split(",");
-// int[] rfgArray = new int[rgbStringArray.length];
-// for (int i = 0; i < rgbStringArray.length; i++) {
-// rfgArray[i] = Integer.parseInt(rgbStringArray[i]);
-// }
-// rgbList.add(rfgArray);
-// }
-// } catch (IOException e) {
-// // TODO Auto-generated catch block
-// e.printStackTrace();
-// }finally {
-// if(tfr != null){
-// tfr.close();
-// }
-// }
-//
-// int[][] rgbArray = new int[rgbList.size()][];
-// for (int i = 0; i < rgbArray.length; i++) {
-// rgbArray[i] = rgbList.get(i);
-// }
-//
-// return rgbArray;
-// }
-// public static int[][] readImageRGB2Object(String filePath) {
-// File file = new File(filePath);
-// FileInputStream fis = null;
-// ObjectInputStream ois = null;
-// try {
-// fis = new FileInputStream(file);
-// ois = new ObjectInputStream(fis);
-// Object data = ois.readObject();
-// return (int[][])data;
-// } catch (FileNotFoundException e) {
-// // TODO Auto-generated catch block
-// e.printStackTrace();
-// } catch (IOException e) {
-// // TODO Auto-generated catch block
-// e.printStackTrace();
-// } catch (ClassNotFoundException e) {
-// // TODO Auto-generated catch block
-// e.printStackTrace();
-// }finally {
-// if(ois != null){
-// try {
-// ois.close();
-// } catch (IOException e) {
-// // TODO Auto-generated catch block
-// e.printStackTrace();
-// }
-// }
-// if(fis != null){
-// try {
-// fis.close();
-// } catch (IOException e) {
-// // TODO Auto-generated catch block
-// e.printStackTrace();
-// }
-// }
-// }
-//
-// return null;
-// }
-//
-//}
-//
diff --git a/src/main/java/com/example/jmacro/JMacro.java b/src/main/java/com/example/jmacro/JMacro.java
index 1b34e10..cad8b00 100644
--- a/src/main/java/com/example/jmacro/JMacro.java
+++ b/src/main/java/com/example/jmacro/JMacro.java
@@ -1,24 +1,199 @@
package com.example.jmacro;
+import cn.hutool.core.util.RandomUtil;
+import com.example.jmacro.base.ScreenPoint;
+import com.example.jmacro.base.ScreenRect;
+import com.example.jmacro.util.ColorUtil;
+import com.example.jmacro.util.Imager;
+
+import java.awt.*;
+import java.awt.event.InputEvent;
+import java.awt.image.BufferedImage;
import java.io.File;
/**
- * 脚本
+ * Java脚本
*/
public class JMacro {
/**
- * 图例位置
+ * 机器人
+ */
+ private Robot robot;
+
+ public JMacro() throws AWTException {
+ this.robot = new Robot();
+ }
+
+ /**
+ * 鼠标左键单击
+ *
+ * @param point 屏幕点
+ */
+ public void mouseMove(ScreenPoint point) {
+ waitTap();
+ robot.mouseMove(point.getX(), point.getY());
+ }
+
+ /**
+ * 鼠标左键单击
+ *
+ * @param rect 矩形区域
+ */
+ public void mouseLeftClick(ScreenRect rect) {
+ waitTap();
+ robot.mouseMove(rect.getCenter()[0], rect.getCenter()[1]);
+ waitTap();
+ robot.mousePress(InputEvent.BUTTON1_MASK);
+ waitTap();
+ robot.mouseRelease(InputEvent.BUTTON1_MASK);
+ }
+
+ /**
+ * 鼠标滚轮单击
+ *
+ * @param rect 矩形区域
+ */
+ public void mouseWheelClick(ScreenRect rect) {
+ waitTap();
+ robot.mouseMove(rect.getCenter()[0], rect.getCenter()[1]);
+ waitTap();
+ robot.mousePress(InputEvent.BUTTON2_MASK);
+ waitTap();
+ robot.mouseRelease(InputEvent.BUTTON2_MASK);
+ }
+
+ /**
+ * 鼠标右键单击
+ *
+ * @param rect 矩形区域
+ */
+ public void mouseRightClick(ScreenRect rect) {
+ waitTap();
+ robot.mouseMove(rect.getCenter()[0], rect.getCenter()[1]);
+ waitTap();
+ robot.mousePress(InputEvent.BUTTON3_MASK);
+ waitTap();
+ robot.mouseRelease(InputEvent.BUTTON3_MASK);
+ }
+
+ /**
+ * 鼠标左键双击
+ *
+ * @param rect 矩形区域
+ */
+ public void mouseLeftDoubleClick(ScreenRect rect) {
+ waitTap();
+ robot.mouseMove(rect.getCenter()[0], rect.getCenter()[1]);
+ waitTap();
+ robot.mousePress(InputEvent.BUTTON1_MASK);
+ waitTap();
+ robot.mouseRelease(InputEvent.BUTTON1_MASK);
+ waitTap();
+ robot.mousePress(InputEvent.BUTTON1_MASK);
+ waitTap();
+ robot.mouseRelease(InputEvent.BUTTON1_MASK);
+ }
+
+
+ /**
+ * 捕获指定区域屏幕
+ */
+ public BufferedImage capture(Robot robot, ScreenRect screenRect) {
+ return robot.createScreenCapture(new Rectangle(screenRect.getLeft(), screenRect.getTop(), screenRect.getWidth(), screenRect.getHeight()));
+ }
+
+ /**
+ * 获取屏幕范围
+ *
+ * @return
*/
- private File legend;
+ public ScreenRect getScreenRect() {
+ Toolkit tk = Toolkit.getDefaultToolkit();
+ return new ScreenRect(0, 0, tk.getScreenSize().width, tk.getScreenSize().height);
+ }
- public JMacro() {
+ /**
+ * 全屏查找图片
+ *
+ * @param pic 图片
+ * @param minSimilar 最低相似度
+ * @return 匹配的图片区域
+ */
+ public ScreenRect findPic(File pic, double minSimilar) {
+ return findPic(getScreenRect(), Imager.load(pic), minSimilar);
}
/**
- * 定位
+ * 全屏查找图片
+ *
+ * @param pic 图片
+ * @param minSimilar 最低相似度
+ * @return 匹配的图片区域
*/
- private boolean location(){
+ public ScreenRect findPic(BufferedImage pic, double minSimilar) {
+ return findPic(getScreenRect(), pic, minSimilar);
+ }
+
+ /**
+ * 获取定位
+ *
+ * @param pic 参考图
+ * @param screenRect 查找范围
+ * @param minSimilar 相似度
+ * @return
+ */
+ public ScreenRect findPic(ScreenRect screenRect, BufferedImage pic, double minSimilar) {
+ // 当查找区域比图片还小时,直接返回失败
+ if (screenRect.getWidth() < pic.getHeight() || screenRect.getHeight() < pic.getHeight()) {
+ return null;
+ }
+
+ // 获取实时屏幕
+ BufferedImage screen = capture(robot, screenRect);
+ int[][] screenData = Imager.getImageRGB(screen);
+ int[][] picData = Imager.getImageRGB(pic);
+
+ // 得到图片左上角范围
+ int xMin = screenRect.getLeft();
+ int xMax = screenRect.getRight() - pic.getWidth();
+ int yMin = screenRect.getTop();
+ int yMax = screenRect.getBottom() - pic.getHeight();
+
+ for (int y = yMin; y <= yMax; y++) {
+ for (int x = xMin; x <= xMax; x++) {
+ // 对关键点进行先期匹配,降低运算复杂度。如果关键点本身就不匹配,就没必要再去匹配小图的每一个像素点
+ // 左上角
+ boolean lt = ColorUtil.isSimilar(screenData[x][y], picData[0][0]);
+ // 右上角
+ boolean rt = ColorUtil.isSimilar(screenData[x + pic.getWidth() - 1][y], picData[pic.getWidth() - 1][0]);
+ // 左下角
+ boolean lb = ColorUtil.isSimilar(screenData[x][y + pic.getHeight() - 1], picData[0][pic.getHeight() - 1]);
+ // 右下角
+ boolean rb = ColorUtil.isSimilar(screenData[x + pic.getWidth() - 1][y + pic.getHeight() - 1], picData[pic.getWidth() - 1][pic.getHeight() - 1]);
+ //中心点
+ boolean cc = ColorUtil.isSimilar(screenData[x + pic.getWidth() / 2][y + pic.getHeight() / 2], picData[pic.getWidth() / 2][pic.getHeight() / 2]);
+ if (lt && rt && lb && rb && cc) {
+ // 进行全像素匹配
+ double similar = Imager.calcSimilar(x, y, pic.getHeight(), pic.getWidth(), screenData, picData);
+ if (similar >= minSimilar) {
+ return new ScreenRect(x, y, x + pic.getWidth(), y + pic.getHeight());
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ public void waitTap() {
+ robot.delay(RandomUtil.randomInt(100, 200));
+ }
+
+ public void waitNormal() {
+ robot.delay(RandomUtil.randomInt(500, 1500));
+ }
+ public void waitLong() {
+ robot.delay(RandomUtil.randomInt(2000, 5000));
}
}
diff --git a/src/main/java/com/example/jmacro/Main.java b/src/main/java/com/example/jmacro/Main.java
index 3e7a8d0..7b03084 100644
--- a/src/main/java/com/example/jmacro/Main.java
+++ b/src/main/java/com/example/jmacro/Main.java
@@ -1,118 +1,40 @@
package com.example.jmacro;
-import cn.hutool.core.img.ImgUtil;
-import cn.hutool.json.JSONUtil;
-import org.w3c.dom.css.Rect;
+import cn.hutool.core.io.FileUtil;
+import com.example.jmacro.base.ScreenRect;
+import com.example.jmacro.util.Imager;
+import com.example.jmacro.util.Logger;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
+import java.util.List;
public class Main {
- static Robot robot;
-
- static {
- try {
- robot = new Robot();
- } catch (AWTException e) {
- throw new RuntimeException(e);
- }
- }
-
public static void main(String[] args) throws AWTException {
- int[] aa = findImage(loadScreen(), loadAnchor("野外"));
-
- System.out.println(JSONUtil.toJsonStr(aa));
- }
-
- public static Rect findImage(BufferedImage bigImage, BufferedImage smallImage) {
- int bigWidth = bigImage.getWidth();
- int bigHeight = bigImage.getHeight();
- int smallWidth = smallImage.getWidth();
- int smallHeight = smallImage.getHeight();
- int[][] bigData = getImageRGB(bigImage);
- int[][] smallData = getImageRGB(smallImage);
- int[] target = {-1, -1};
- int yEnd = bigHeight - smallHeight;
- int xEnd = bigWidth - smallWidth;
-
- for (int y = 0; y < yEnd; y++) {
- for (int x = 0; x < xEnd; x++) {
- // 对关键点进行先期匹配,降低运算复杂度。如果关键点本身就不匹配,就没必要再去匹配小图的每一个像素点
- if (ColorUtil.isSimilar(bigData[x][y], smallData[0][0]) && // 左上角
- ColorUtil.isSimilar(bigData[x + smallWidth - 1][y], smallData[smallWidth - 1][0]) &&// 右上角
- ColorUtil.isSimilar(bigData[x][y + smallHeight - 1], smallData[0][smallHeight - 1]) &&// 左下角
- ColorUtil.isSimilar(bigData[x + smallWidth - 1][y + smallHeight - 1], smallData[smallWidth - 1][smallHeight - 1]) && // 右下角
- ColorUtil.isSimilar(bigData[x + smallWidth / 2][y + smallHeight / 2], smallData[smallWidth / 2][smallHeight / 2]) //中心点
- ) {
-
- // 进行全像素匹配
- boolean isMatched = checkAllMatch(x, y, smallHeight, smallWidth, bigData, smallData);
- if (isMatched) {
- System.out.println("像素点X" + x + " : Y" + y + ",对应的值为:" + bigData[x][y]);
-
- // 获取小图的中心位置的点
- int centerX = x + smallWidth / 2;
- int centerY = y + smallHeight / 2;
- target[0] = centerX;
- target[1] = centerY;
- return new int[][]{{x,y},};
- return new Rect
- }
- }
-
- }
- }
-
- return target;
- }
-
- public static int[][] getImageRGB(BufferedImage bfImage) {
- int width = bfImage.getWidth();
- int height = bfImage.getHeight();
- int[][] result = new int[width][height];
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- // 对某个像素点的RGB编码并存入数据库
- result[x][y] = bfImage.getRGB(x, y) & 0xFFFFFF;
-
- // 单独获取每一个像素点的Red,Green,和Blue的值。
- // int r = (bfImage.getRGB(x, y) & 0xFF0000) >> 16;
- // int g = (bfImage.getRGB(x, y) & 0xFF00) >> 8;
- // int b = bfImage.getRGB(x, y) & 0xFF;
+ Logger.info("启动脚本");
+
+ Logger.info("初始化脚本...");
+ JMacro jMacro = new JMacro();
+
+ // 开始定位App,好让后续的图片查找限定在一定范围内提高效率(如果过程中移动过App窗口需要重新定位,否则无法具体部件位置)
+ Logger.info("开始定位程序...");
+ List load = Imager.load(FileUtil.loopFiles(new File("legend", "main")));
+ ScreenRect appRect = null;
+ for (BufferedImage bufferedImage : load) {
+ appRect = jMacro.findPic(bufferedImage, 0.99d);
+ if (appRect != null) {
+ break;
}
}
- return result;
- }
-
- private static boolean checkAllMatch(int x, int y, int smallHeight, int smallWidth, int[][] bigData, int[][] smallData) {
- boolean isMatched = true;
- for (int smallY = 0; smallY < smallHeight; smallY++) {
- for (int smallX = 0; smallX < smallWidth; smallX++) {
- // 如果发现有一个像素点,两者的值不一样,则认为不相等,如果不相等,则没必要继续比较其它点.
- if (!ColorUtil.isSimilar(bigData[x + smallX][y + smallY], smallData[smallX][smallY])) {
- isMatched = false;
- return isMatched;
- }
- }
+ if (appRect == null) {
+ Logger.error("定位主程序失败");
+ return;
}
- return isMatched;
- }
-
-
- // 获取屏幕截图
- static BufferedImage loadScreen() {
- // 获取缺省工具包
- Toolkit tk = Toolkit.getDefaultToolkit();
- // 屏幕尺寸规格
- Dimension di = tk.getScreenSize();
- LogUtil.info("获取屏幕尺寸({},{})", di.width, di.height);
- Rectangle rec = new Rectangle(0, 0, di.width, di.height);
- return robot.createScreenCapture(rec);
- }
+ Logger.info("定位图片成功,位置{},{},宽{},高{}", appRect.getLeft(), appRect.getTop(), appRect.getWidth(), appRect.getHeight());
- static BufferedImage loadAnchor(String anchor) {
- return ImgUtil.read(new File("anchor", anchor + ".png"));
+ // 移动鼠标
+ jMacro.mouseRightClick(appRect);
}
}
diff --git a/src/main/java/com/example/jmacro/base/ScreenPoint.java b/src/main/java/com/example/jmacro/base/ScreenPoint.java
new file mode 100644
index 0000000..ebd4e25
--- /dev/null
+++ b/src/main/java/com/example/jmacro/base/ScreenPoint.java
@@ -0,0 +1,30 @@
+package com.example.jmacro.base;
+
+/**
+ * 屏幕点
+ */
+public class ScreenPoint {
+
+ private int x;
+ private int y;
+
+ public ScreenPoint() {
+ }
+
+
+ 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;
+ }
+}
diff --git a/src/main/java/com/example/jmacro/base/ScreenRect.java b/src/main/java/com/example/jmacro/base/ScreenRect.java
new file mode 100644
index 0000000..7de8e10
--- /dev/null
+++ b/src/main/java/com/example/jmacro/base/ScreenRect.java
@@ -0,0 +1,66 @@
+package com.example.jmacro.base;
+
+/**
+ * 屏幕区域
+ */
+public class ScreenRect {
+
+ private int left;
+ private int top;
+ private int right;
+ private int bottom;
+
+ public ScreenRect() {
+ }
+
+ public ScreenRect(int left, int top, int right, int bottom) {
+ this.left = left;
+ this.top = top;
+ this.right = right;
+ this.bottom = bottom;
+ }
+
+ 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;
+ }
+
+ public int getHeight() {
+ return this.bottom - this.top;
+ }
+
+ public int[] getCenter() {
+ return new int[]{(left + right) / 2, (top + bottom) / 2};
+ }
+}
diff --git a/src/main/java/com/example/jmacro/ColorUtil.java b/src/main/java/com/example/jmacro/util/ColorUtil.java
similarity index 93%
rename from src/main/java/com/example/jmacro/ColorUtil.java
rename to src/main/java/com/example/jmacro/util/ColorUtil.java
index 6c4ad97..c43f684 100644
--- a/src/main/java/com/example/jmacro/ColorUtil.java
+++ b/src/main/java/com/example/jmacro/util/ColorUtil.java
@@ -1,4 +1,4 @@
-package com.example.jmacro;
+package com.example.jmacro.util;
public class ColorUtil {
@@ -27,7 +27,7 @@ public class ColorUtil {
}
public static boolean isSimilar(int colorInt1, int colorInt2) {
- return calculateSimilarity(colorInt1, colorInt2) > 0.99d;
+ return calculateSimilarity(colorInt1, colorInt2) > 0.90d;
}
public static void main(String[] args) {
diff --git a/src/main/java/com/example/jmacro/util/Imager.java b/src/main/java/com/example/jmacro/util/Imager.java
new file mode 100644
index 0000000..141534b
--- /dev/null
+++ b/src/main/java/com/example/jmacro/util/Imager.java
@@ -0,0 +1,102 @@
+package com.example.jmacro.util;
+
+import cn.hutool.core.img.ImgUtil;
+
+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;
+
+/**
+ * 图片加载器
+ */
+public class Imager {
+
+ private final static Map cache = new HashMap();
+
+ /**
+ * 加载图片
+ *
+ * @param pic 图片
+ * @return 缓冲区图像
+ */
+ public static BufferedImage load(File pic) {
+ if (cache.containsKey(pic.getAbsolutePath())) {
+ return cache.get(pic.getAbsolutePath());
+ }
+ BufferedImage read = ImgUtil.read(pic);
+ cache.put(pic.getAbsolutePath(), read);
+ return read;
+ }
+
+ /**
+ * 加载多个图片
+ *
+ * @param pics 图片集
+ * @return 缓冲区图像集
+ */
+ public static BufferedImage[] load(File[] pics) {
+ BufferedImage[] images = new BufferedImage[pics.length];
+ for (int i = 0; i < images.length; i++) {
+ images[i] = load(pics[i]);
+ }
+ return images;
+ }
+
+ /**
+ * 加载多个图片
+ *
+ * @param pics 图片集
+ * @return 缓冲区图像集
+ */
+ public static List load(List pics) {
+ List images = new ArrayList();
+ for (File file : pics) {
+ images.add(load(file));
+ }
+ return images;
+ }
+
+ public static int[][] getImageRGB(BufferedImage bfImage) {
+ int width = bfImage.getWidth();
+ int height = bfImage.getHeight();
+ int[][] result = new int[width][height];
+ for (int y = 0; y < height; y++) {
+ for (int x = 0; x < width; x++) {
+ // 对某个像素点的RGB编码并存入数据库
+ result[x][y] = bfImage.getRGB(x, y) & 0xFFFFFF;
+
+ // 单独获取每一个像素点的Red,Green,和Blue的值。
+ // int r = (bfImage.getRGB(x, y) & 0xFF0000) >> 16;
+ // int g = (bfImage.getRGB(x, y) & 0xFF00) >> 8;
+ // int b = bfImage.getRGB(x, y) & 0xFF;
+ }
+ }
+ return result;
+ }
+
+ /**
+ * 计算图片相似度
+ *
+ * @param x
+ * @param y
+ * @param smallHeight
+ * @param smallWidth
+ * @param bigData
+ * @param smallData
+ * @return
+ */
+ public static double calcSimilar(int x, int y, int smallHeight, int smallWidth, int[][] bigData, int[][] smallData) {
+ int similar = 0;
+ for (int smallY = 0; smallY < smallHeight; smallY++) {
+ for (int smallX = 0; smallX < smallWidth; smallX++) {
+ if (ColorUtil.isSimilar(bigData[x + smallX][y + smallY], smallData[smallX][smallY])) {
+ similar++;
+ }
+ }
+ }
+ return (double) similar / (smallWidth * smallHeight);
+ }
+}
diff --git a/src/main/java/com/example/jmacro/LogUtil.java b/src/main/java/com/example/jmacro/util/Logger.java
similarity index 83%
rename from src/main/java/com/example/jmacro/LogUtil.java
rename to src/main/java/com/example/jmacro/util/Logger.java
index 326aa7c..4daebba 100644
--- a/src/main/java/com/example/jmacro/LogUtil.java
+++ b/src/main/java/com/example/jmacro/util/Logger.java
@@ -1,8 +1,8 @@
-package com.example.jmacro;
+package com.example.jmacro.util;
import cn.hutool.core.util.StrUtil;
-public class LogUtil {
+public class Logger {
public static void info(String log, Object... arg) {
System.out.println(StrUtil.format(log, arg));