我已经尝试了几个小时来完成这项工作,但由于某种原因,它没有找到匹配项。您可以使用任何图像对其进行测试,但它应该截取屏幕左上角的屏幕截图(1000 像素 x 1000 像素)并在其中找到指定的图像。任何帮助将不胜感激!
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageRobotTester {
/**
* @param args
*/
public static void main(String[] args) {
ImageRobot i = new ImageRobot();
try {
BufferedImage settingsImage = ImageIO.read(new File("images/Dex.png"));
Robot r = new Robot();
BufferedImage screen = r.createScreenCapture(new Rectangle(0, 0, 1000, 1000));
i.subImage(settingsImage, screen);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class ImageRobot {
public ImageRobot() {
// TODO Auto-generated constructor stub
}
public void subImage(BufferedImage needle, BufferedImage hayStack)
{
int xMatch = 0;
int yMatch = 0;
int possMatches = 0;
try {
for(int j = 0; j < hayStack.getHeight() - needle.getHeight(); j++)
{
for(int i = 0; i < hayStack.getWidth() - needle.getWidth(); i++)
{
BufferedImage hayStackSub = hayStack.getSubimage(i, j, needle.getWidth(), needle.getHeight());
if(hayStackSub.equals(needle))
{
System.out.println("match!");
xMatch = i;
yMatch = j;
}
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Out of bounds! (" + xMatch + ", " + yMatch + ")");
}
System.out.println("(" + xMatch + ", " + yMatch + ")");
}
}