我如何“监控”屏幕中心?

时间:2015-07-29 13:28:51

标签: java pixel resolution

在Java中,我想基本上关注屏幕死点的1个像素,并在颜色发生变化时检测/调用动作(例如,中心聚焦在白色背景上,突然我打开了绿色背景,检测到1个像素来自白色 - >绿色)。我知道我需要分辨率的高度和宽度,并从中确定中心。

现在唯一的问题是我不知道我需要什么代码来进一步继续这个过程。有人可以指导我完成我的工作吗?我知道这有点宽泛,因为它不包含任何代码。

1 个答案:

答案 0 :(得分:4)

这是一个快速而肮脏的例子,也许有帮助:

public class PixelBot {

private final Robot bot;

private boolean running = true;

private int lastPixelValue = 0;

public static void main(String[] args) throws Exception {
    new PixelBot();
}

public PixelBot() throws AWTException {
    this.bot = new Robot();
    this.runInBackground();
}

private void checkPixel() {
    Rectangle areaOfInterest = getAreaOfInterest();
    BufferedImage image = bot.createScreenCapture(areaOfInterest);

    int clr = image.getRGB(0, 0);
    if (clr != lastPixelValue) {
        int red = (clr & 0x00ff0000) >> 16;
        int green = (clr & 0x0000ff00) >> 8;
        int blue = clr & 0x000000ff;
        System.out.println("\nPixel color changed to: Red: " + red + ", Green: " + green + ", Blue: " + blue);
        Toolkit.getDefaultToolkit().beep();
        lastPixelValue = clr;
    } else {
        System.out.print(".");
    }
}

private Rectangle getAreaOfInterest() {
    // screen size may change:
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    // half of screen, minus 1 pixel to be captured:
    int centerPointX = (int) (screenSize.getWidth() / 2 - 1);
    int centerPointY = (int) (screenSize.getHeight() / 2 - 1);
    Point centerOfScreenMinusOnePixel = new Point(centerPointX, centerPointY);
    return new Rectangle(centerOfScreenMinusOnePixel, new Dimension(1, 1));
}

private void runInBackground() {
    new Thread(new Runnable() {

        @Override
        public void run() {
            while (running) {
                checkPixel();
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
}

public void stop() {
    this.running = false;
}
}