Robot.getPixelColor vs BufferedImage.getRGB

时间:2018-05-03 12:50:59

标签: java awtrobot

我今天要问这个是为了比较两者。

我要做的是制作能够在特定时间点击空格键的机器人。

无论如何,让我展示一下我要比较的内容。

    Robot robot = new Robot();

这将是机器人。 (java.awt.Robot中)

我的问题是,为了尝试不断读取屏幕上的一个(或多个)像素,这会更快吗?

我当前(正在进行)的程序是相同的,但使用

的程序除外

Robot.getPixelColor(int, int)

,另一个使用

BufferedImage image Robot.createScreenCapture(Rectangle)
imaage.getRGB

这些只是每秒运行很多次,而我只是想弄清楚哪个更加一致。它们在运行速度上看起来都是随机的,所以任何建议都值得赞赏。

如果您希望将示例放入其中,请按以下步骤进行设置。

以下是使用Robot.getPixelColor(int,int)

的示例
    Robot robot = new Robot();
    Rectangle rect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    Point prompt = new Point(913, 506, robot);
    BufferedImage screenImage = robot.createScreenCapture(rect);    
    for(int i = 0; i < 360; i++) {
        zones.add(new Point(radius * Math.cos(Math.toRadians(i)) + centerX, radius * Math.sin(Math.toRadians(i)) + centerY, robot));
    }
    System.out.println("Resolution: "  + width + " * " + height/* + " " + robot.getPixelColor(20, 20)/* + " " + screenImage.getRGB((int) prompt.getX(), (int) prompt.getY())*/);
    while(true) {
        tempZone = null;
        while(prompt.hasColor(promptColor)) {
            System.out.println("Found prompt, trying to find zone.");
            int count = 0;
            for(int i = 0; i < zones.size(); i++) {
                if(zones.get(i).hasColor(zoneColor)){
                    tempZone = zones.get(i);
                    break;
                }
            }
            if(tempZone != null) {
                System.out.println("Found zone at: " + tempZone.getX() + ", " + tempZone.getY() + ". Looking for ticker...");
                tempZone.whenHasColor(zoneColor);
            }else{
                System.out.println("Unable to find zone.");
            }
            while(prompt.hasColor(promptColor)) {}
        }
    }

使用Point Class:

public class Point {    
    private double x;
    private double y;
    private final Robot robot;

    public Point(double x, double y, Robot rob) {
        this.x = x;
        this.y = y;
        this.robot = rob;
    }

    public double getX() {
        return x;
    }
    public double getY() {
        return y;
    }
    public boolean hasColor(Color color) {
        return (robot.getPixelColor((int )x, (int) y).equals(color));
    }
    public void whenHasColor(Color color) throws IOException {
        Rectangle rect = new Rectangle(889 + (int) x, 449 + (int) y, 1, 1);
        while(hasColor(color)) {
            //Nothing
        }
        BufferedImage cap = robot.createScreenCapture(rect);
        if(Math.abs(Math.abs(cap.getRGB(0, 0)) - 15657182) < 350000) {
            System.out.println("Missed Zone, failure.");
        }else {
            robot.keyPress(Main.promptKey);
            System.out.println("Found ticker pressing key. ");
        }
    }
}

对于任何想知道这个具体来自何处的人,我使用this website的修改版本作为制作此机器人的媒介。

现在使用Robot.createScreenCapture(Rectangle)和BufferedImage.getRGB(int,int)的示例

    Robot robot = new Robot();
    Rectangle rect = new Rectangle(889, 449, 149, 149); 
    Point prompt = new Point(24, 57, robot);
    String format = "png";
    String fileName = "fullDebug1." + format;       
    BufferedImage screenImage = robot.createScreenCapture(rect);    
    for(int i = 0; i < 360; i++) {
        zones.add(new Point(radius * Math.cos(Math.toRadians(i)) + centerX, radius * Math.sin(Math.toRadians(i)) + centerY, robot));
    }
    System.out.println("Resolution: "  + width + " * " + height/* + " " + screenImage.getRGB((int) prompt.getX(), (int) prompt.getY())*/);
    while(true) {
        tempZone = null;
        int zoneFinal = 0;
        while(prompt.hasColor(promptColor)) {
            System.out.println("Found prompt, trying to find zone.");
            BufferedImage zoneImage = robot.createScreenCapture(rect);
            int count = 0;
            for(int i = 0; i < zones.size(); i++) {
                if(zones.get(i).hasColorZones(zoneColor, zoneImage)){
                    tempZone = zones.get(i);
                    break;
                }
            }
            if(tempZone != null) {
                System.out.println("Found zone at: " + tempZone.getX() + ", " + tempZone.getY() + ". Looking for ticker...");
                tempZone.whenHasColor(zoneColor);
            }else{
                System.out.println("Unable to find zone.");
            }
            while(prompt.hasColor(promptColor)) {}
        }

及其Point类:

public class Point {    
    private double x;
    private double y;
    private final Robot robot;

    public Point(double x, double y, Robot rob) {
        this.x = x;
        this.y = y;
        this.robot = rob;
    }

    public double getX() {
        return x;
    }
    public double getY() {
        return y;
    }
    public boolean hasColor(int color) {
        Rectangle rect = new Rectangle(889, 449, 149, 149);
        BufferedImage screenImage = robot.createScreenCapture(rect);
        return (Math.abs(screenImage.getRGB((int) x, (int) y)) == Math.abs(color))
    }
    public boolean hasColorPixel(int color, Rectangle rect) throws IOException {
        BufferedImage cap = robot.createScreenCapture(rect);
        return Math.abs(cap.getRGB(0, 0)) == Math.abs(color);
    }

    public boolean hasColorZones(int color, BufferedImage screenImage) {
        return Math.abs(screenImage.getRGB((int) x, (int) y)) == Math.abs(color);
    }

    public boolean hasColorList(int[] colors, BufferedImage screenImage) {
        for(int i : colors) {
            if(Math.abs(screenImage.getRGB((int) x, (int) y)) == Math.abs(i)) {
                return true;
            }
        }
        return false;
    }

    public void whenHasColor(int color) throws IOException {
        Rectangle rect = new Rectangle(889 + (int) x, 449 + (int) y, 1, 1);
        while(hasColorPixel(color, rect)) {
            //Nothing
        }
        BufferedImage cap = robot.createScreenCapture(rect);
        if(Math.abs(Math.abs(cap.getRGB(0, 0)) - 15657182) < 350000) {
            System.out.println("Missed Zone, failure.");
        }else {
            robot.keyPress(Main.promptKey);
            System.out.println("Found ticker pressing key. ");
        }
    }
}

所以我的问题是,哪个会运行得更快?有时后一个选项(我更彻底地调试)连续10次,但随后也会随机失败。起初我以为是我的笔记本电脑无法处理它,但是在我的桌面上运行它时我得到了相同的结果。我宁愿不让机器人与正常工作不一致。

我希望我能够为任何能够帮助我的人提供足够的信息。如果我需要添加或更改任何内容,请告诉我!这是我的第一个问题,我想学习这个网站更好。 另一个道歉,如果1)屏幕上的像素不一样,那么你将无法运行它来模拟我的情况,2)如果这篇文章很长。我想做一个好的&#34;最小,完整,可验证的例子,&#34;但我不知道如何缩短它。

修改

为了使代码更具相关性,正如Ben指出的那样,我本可以削减它。但我想到了一些让它更有用的东西。

可以对该代码做些什么来优化它以便更快地运行,因此机器人将100%始终如一地工作,或者至少比现在更接近100%(~60%)

0 个答案:

没有答案
相关问题