简体   繁体   English

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

[英]How would I go about “monitoring” the center of the screen?

In Java I want to essentially focus on the 1 pixel at the dead center of the screen and detect/call an action if there is a change of color (ex. Center is focused on a white background, and suddenly I open up a green background, that 1 pixel was detected as going from white -> green). 在Java中我想基本上关注屏幕死点的1个像素,如果颜色发生变化则检测/调用动作(例如,Center聚焦在白色背景上,然后我突然打开绿色背景,检测到1个像素是从白色 - >绿色)。 I know I would need the height and width of the resolution and determine the center from that. 我知道我需要分辨率的高度和宽度,并从中确定中心。

The only problem now is I have no idea what code I would need to further move on with this process. 现在唯一的问题是我不知道我需要什么代码来进一步继续这个过程。 Can someone guide me through what I can do? 有人可以指导我完成我的工作吗? I know this is kinda broad since it doesn't include any code. 我知道这有点宽,因为它不包含任何代码。

Here is a quick and dirty example, maybe it helps: 这是一个快速而肮脏的例子,也许它会有所帮助:

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;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM