package com.alatus.jiuxingxing; import com.sun.jna.platform.win32.User32; import java.awt.*; import java.awt.event.InputEvent; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import java.io.IOException; import java.io.InputStream; import java.util.Date; public class ScreenRecognizer { private static BufferedImage targetImage; public static void main(String[] args) { try { targetImage = loadTargetImage(); if (targetImage == null) { return; // 如果加载图像失败则退出 } Robot robot = new Robot(); boolean flag = true; while (flag) { if ((User32.INSTANCE.GetAsyncKeyState(123) & 0x8000) != 0) { flag = false; // 按下 F12 退出 } BufferedImage screenCapture = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())); Point contentLocation = findContentLocation(screenCapture); System.out.println(new Date()); System.out.println(contentLocation); if (contentLocation != null) { robot.mouseMove(contentLocation.x, contentLocation.y); robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); Thread.sleep(100); // 延迟以避免快速点击 } } } catch (Exception e) { e.printStackTrace(); } } private static BufferedImage loadTargetImage() { try (InputStream is = ScreenRecognizer.class.getResourceAsStream("/images/1.png")) { if (is != null) { return ImageIO.read(is); } else { System.out.println("加载文件失败"); return null; } } catch (IOException e) { e.printStackTrace(); return null; } } private static Point findContentLocation(BufferedImage screenCapture) { for (int x = 0; x <= screenCapture.getWidth() - targetImage.getWidth(); x++) { for (int y = 0; y <= screenCapture.getHeight() - targetImage.getHeight(); y++) { boolean found = true; for (int i = 0; i < targetImage.getWidth(); i++) { for (int j = 0; j < targetImage.getHeight(); j++) { if (screenCapture.getRGB(x + i, y + j) != targetImage.getRGB(i, j)) { found = false; break; } } if (!found) break; } if (found) { return new Point(x, y); } } } return null; // 没有找到 } }package com.alatus.jiuxingxing; import com.sun.jna.platform.win32.User32; import java.awt.*; import java.awt.event.InputEvent; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import java.io.IOException; import java.io.InputStream; import java.util.Date; public class ScreenRecognizer { private static BufferedImage targetImage; public static void main(String[] args) { try { targetImage = loadTargetImage(); if (targetImage == null) { return; // 如果加载图像失败则退出 } Robot robot = new Robot(); boolean flag = true; while (flag) { if ((User32.INSTANCE.GetAsyncKeyState(123) & 0x8000) != 0) { flag = false; // 按下 F12 退出 } BufferedImage screenCapture = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())); Point contentLocation = findContentLocation(screenCapture); System.out.println(new Date()); System.out.println(contentLocation); if (contentLocation != null) { robot.mouseMove(contentLocation.x, contentLocation.y); robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); Thread.sleep(100); // 延迟以避免快速点击 } } } catch (Exception e) { e.printStackTrace(); } } private static BufferedImage loadTargetImage() { try (InputStream is = ScreenRecognizer.class.getResourceAsStream("/images/1.png")) { if (is != null) { return ImageIO.read(is); } else { System.out.println("加载文件失败"); return null; } } catch (IOException e) { e.printStackTrace(); return null; } } private static Point findContentLocation(BufferedImage screenCapture) { for (int x = 0; x <= screenCapture.getWidth() - targetImage.getWidth(); x++) { for (int y = 0; y <= screenCapture.getHeight() - targetImage.getHeight(); y++) { boolean found = true; for (int i = 0; i < targetImage.getWidth(); i++) { for (int j = 0; j < targetImage.getHeight(); j++) { if (screenCapture.getRGB(x + i, y + j) != targetImage.getRGB(i, j)) { found = false; break; } } if (!found) break; } if (found) { return new Point(x, y); } } } return null; // 没有找到 } }
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>screen-element-finder</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <!-- OpenCV Java依赖 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-imaging</artifactId> <version>1.0-alpha3</version> </dependency> <dependency> <groupId>net.java.dev.jna</groupId> <artifactId>jna-platform</artifactId> <version>5.12.0</version> </dependency> <!-- JNA依赖 --> <dependency> <groupId>net.java.dev.jna</groupId> <artifactId>jna</artifactId> <version>5.12.0</version> </dependency> <!-- JNativeHook依赖 --> <dependency> <groupId>com.github.kwhat</groupId> <artifactId>jnativehook</artifactId> <version>2.2.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> </configuration> </plugin> </plugins> </build> </project><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>screen-element-finder</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <!-- OpenCV Java依赖 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-imaging</artifactId> <version>1.0-alpha3</version> </dependency> <dependency> <groupId>net.java.dev.jna</groupId> <artifactId>jna-platform</artifactId> <version>5.12.0</version> </dependency> <!-- JNA依赖 --> <dependency> <groupId>net.java.dev.jna</groupId> <artifactId>jna</artifactId> <version>5.12.0</version> </dependency> <!-- JNativeHook依赖 --> <dependency> <groupId>com.github.kwhat</groupId> <artifactId>jnativehook</artifactId> <version>2.2.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> </configuration> </plugin> </plugins> </build> </project>

9576

被折叠的 条评论
为什么被折叠?



