王兵 1 year ago
parent 15bb5d817e
commit ce19710a0c

@ -0,0 +1,66 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="AliAccessStaticViaInstance" enabled="false" level="BLOCKER" enabled_by_default="false" />
<inspection_tool class="AliArrayNamingShouldHaveBracket" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AliControlFlowStatementWithoutBraces" enabled="false" level="BLOCKER" enabled_by_default="false" />
<inspection_tool class="AliDeprecation" enabled="false" level="CRITICAL" enabled_by_default="false" />
<inspection_tool class="AliEqualsAvoidNull" enabled="false" level="CRITICAL" enabled_by_default="false" />
<inspection_tool class="AliLongLiteralsEndingWithLowercaseL" enabled="false" level="BLOCKER" enabled_by_default="false" />
<inspection_tool class="AliMissingOverrideAnnotation" enabled="false" level="BLOCKER" enabled_by_default="false" />
<inspection_tool class="AliWrapperTypeEquality" enabled="false" level="BLOCKER" enabled_by_default="false" />
<inspection_tool class="AlibabaAbstractClassShouldStartWithAbstractNaming" enabled="false" level="CRITICAL" enabled_by_default="false" />
<inspection_tool class="AlibabaAbstractMethodOrInterfaceMethodMustUseJavadoc" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaAvoidApacheBeanUtilsCopy" enabled="false" level="BLOCKER" enabled_by_default="false" />
<inspection_tool class="AlibabaAvoidCallStaticSimpleDateFormat" enabled="false" level="CRITICAL" enabled_by_default="false" />
<inspection_tool class="AlibabaAvoidCommentBehindStatement" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaAvoidComplexCondition" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaAvoidConcurrentCompetitionRandom" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaAvoidDoubleOrFloatEqualCompare" enabled="false" level="CRITICAL" enabled_by_default="false" />
<inspection_tool class="AlibabaAvoidManuallyCreateThread" enabled="false" level="CRITICAL" enabled_by_default="false" />
<inspection_tool class="AlibabaAvoidMissUseOfMathRandom" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaAvoidNegationOperator" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaAvoidNewDateGetTime" enabled="false" level="BLOCKER" enabled_by_default="false" />
<inspection_tool class="AlibabaAvoidPatternCompileInMethod" enabled="false" level="BLOCKER" enabled_by_default="false" />
<inspection_tool class="AlibabaAvoidReturnInFinally" enabled="false" level="CRITICAL" enabled_by_default="false" />
<inspection_tool class="AlibabaAvoidStartWithDollarAndUnderLineNaming" enabled="false" level="CRITICAL" enabled_by_default="false" />
<inspection_tool class="AlibabaAvoidUseTimer" enabled="false" level="BLOCKER" enabled_by_default="false" />
<inspection_tool class="AlibabaBigDecimalAvoidDoubleConstructor" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaBooleanPropertyShouldNotStartWithIs" enabled="false" level="CRITICAL" enabled_by_default="false" />
<inspection_tool class="AlibabaClassCastExceptionWithSubListToArrayList" enabled="false" level="CRITICAL" enabled_by_default="false" />
<inspection_tool class="AlibabaClassCastExceptionWithToArray" enabled="false" level="CRITICAL" enabled_by_default="false" />
<inspection_tool class="AlibabaClassMustHaveAuthor" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaClassNamingShouldBeCamel" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaCollectionInitShouldAssignCapacity" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaCommentsMustBeJavadocFormat" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaConcurrentExceptionWithModifyOriginSubList" enabled="false" level="CRITICAL" enabled_by_default="false" />
<inspection_tool class="AlibabaConstantFieldShouldBeUpperCase" enabled="false" level="CRITICAL" enabled_by_default="false" />
<inspection_tool class="AlibabaCountDownShouldInFinally" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaDontModifyInForeachCircle" enabled="false" level="BLOCKER" enabled_by_default="false" />
<inspection_tool class="AlibabaEnumConstantsMustHaveComment" enabled="false" level="CRITICAL" enabled_by_default="false" />
<inspection_tool class="AlibabaExceptionClassShouldEndWithException" enabled="false" level="CRITICAL" enabled_by_default="false" />
<inspection_tool class="AlibabaIbatisMethodQueryForList" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaLockShouldWithTryFinally" enabled="false" level="BLOCKER" enabled_by_default="false" />
<inspection_tool class="AlibabaLowerCamelCaseVariableNaming" enabled="false" level="CRITICAL" enabled_by_default="false" />
<inspection_tool class="AlibabaMethodReturnWrapperType" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaMethodTooLong" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaPackageNaming" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaPojoMustOverrideToString" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaPojoMustUsePrimitiveField" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaPojoNoDefaultValue" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaRemoveCommentedCode" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaServiceOrDaoClassShouldEndWithImpl" enabled="false" level="CRITICAL" enabled_by_default="false" />
<inspection_tool class="AlibabaStringConcat" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaSwitchStatement" enabled="false" level="CRITICAL" enabled_by_default="false" />
<inspection_tool class="AlibabaTestClassShouldEndWithTestNaming" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaThreadLocalShouldRemove" enabled="false" level="CRITICAL" enabled_by_default="false" />
<inspection_tool class="AlibabaThreadPoolCreation" enabled="false" level="BLOCKER" enabled_by_default="false" />
<inspection_tool class="AlibabaThreadShouldSetName" enabled="false" level="CRITICAL" enabled_by_default="false" />
<inspection_tool class="AlibabaTransactionMustHaveRollback" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaUndefineMagicConstant" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaUnsupportedExceptionWithModifyAsList" enabled="false" level="CRITICAL" enabled_by_default="false" />
<inspection_tool class="AlibabaUseQuietReferenceNotation" enabled="false" level="MAJOR" enabled_by_default="false" />
<inspection_tool class="AlibabaUseRightCaseForDateFormat" enabled="false" level="CRITICAL" enabled_by_default="false" />
<inspection_tool class="MapOrSetKeyShouldOverrideHashCodeEquals" enabled="false" level="CRITICAL" enabled_by_default="false" />
</profile>
</component>

@ -11,6 +11,11 @@
<option name="name" value="Aliyun Repository" />
<option name="url" value="https://maven.aliyun.com/repository/public" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="http://maven.aliyun.com/nexus/content/repositories/central/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SmartFoxProjectConfig">
<option name="projectInspectionClosed" value="true" />
</component>
</project>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 323 KiB

Before

Width:  |  Height:  |  Size: 398 B

After

Width:  |  Height:  |  Size: 398 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

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

@ -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;
//
// // 单独获取每一个像素点的RedGreen和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<int[]> rgbList = new ArrayList<int[]>();
// 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;
// }
//
//}
//

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

@ -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;
// 单独获取每一个像素点的RedGreen和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<BufferedImage> 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);
}
}

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

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

@ -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) {

@ -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<String, BufferedImage> 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<BufferedImage> load(List<File> pics) {
List<BufferedImage> 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;
// 单独获取每一个像素点的RedGreen和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);
}
}

@ -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));
Loading…
Cancel
Save

Powered by TurnKey Linux.