我将如何处理 "monitoring" 屏幕中心?
How would I go about "monitoring" the center of the screen?
在 Java 中,我想主要关注屏幕死角的 1 个像素,detect/call 如果颜色发生变化(例如,中心专注于白色背景,突然我打开一个绿色背景,检测到 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 中,我想主要关注屏幕死角的 1 个像素,detect/call 如果颜色发生变化(例如,中心专注于白色背景,突然我打开一个绿色背景,检测到 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;
}
}