java - 我将如何处理屏幕中心的 "monitoring"?

标签 java pixel resolution

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

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

最佳答案

这是一个简单粗暴的例子,也许它有帮助:

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

关于java - 我将如何处理屏幕中心的 "monitoring"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31701857/

相关文章:

java - Android - 从 URI 获取文件路径

java - 从输入流读取并添加到 stringbuilder

image-processing - OpenCv 构建时不保存图像?

svg - SVG 平移变换函数的单位

java 实例方法和变量的继承解析

java - 使用 HttpURLConnection 的 HTTP 请求不会重用 TCP 连接

java - 我想在连接到 websocket 时显示加载动画

visual-c++ - 获取图像的像素值

c++ - 变量声明中结构的范围解析是什么意思?

CSS 屏幕分辨率