我一直在尝试几个小时来完成这项工作,但由于某种原因,它没有找到匹配项。你可以用任何图像对它进行测试,但它应该截取屏幕的左上角(1000px×1000px)并在其中找到指定的图像。任何帮助将不胜感激!在Java中的图像中查找图像
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 + ")");
}
}
是你正在寻找的,而你正在测试它在屏幕上显示的图像?我的意思是,如果你需要找到一个相似的图像,一个像素逐像素匹配没有工作的机会。 – anttix 2012-07-27 03:15:21
'images/Dex.png'一个PNG可能包含半透明的颜色。一定要跳过检查这些像素。 – 2012-07-27 06:11:05