绿色)。我知-6ren">
gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 02:41:42 26 4
gpt4 key购买 nike

在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com