how to get pixels on screen in java?

You code will request individual pixels of the screen, which is considerably slow.

Instead, you can capture a screenshot (image) of the screen (or parts thereof), and then read the pixel values from that (in-memory) screenshot:

Robot robot = new Robot();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle area = new Rectangle(0, 0, screenSize.width, screenSize.height);
BufferedImage screenshot = robot.createScreenCapture(area);

int x = ...;
int y = ...;
Color pixelColor = new Color(screenshot.getRGB(x, y));

By the way, if you only need certain channels, you can extract them as follows:

int rgb = screenshot.getRGB(x, y);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top