7

I would like, in Java, to scan the screen for a particular color.

Any idea how to do that?

Rok Povsic
  • 4,029
  • 5
  • 29
  • 41
  • What do you need this for? Just curious. – James P. Jun 13 '10 at 23:46
  • Thought so. This was one of the first approaches used for a game I used to play. The textures could be swapped around for with ones that had solid colour and an external program would scan the screen. It became obsolete when someone found a way to access variables directly in memory. – James P. Jun 25 '10 at 07:07

2 Answers2

14
    Robot robot = new Robot();
    Rectangle captureSize = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    BufferedImage bufferedImage = robot.createScreenCapture(captureSize);
    // ...

    int color = image.getRGB(x, y);

    int  red = (color & 0x00ff0000) >> 16;
    int  green = (color & 0x0000ff00) >> 8;
    int  blue = color & 0x000000ff;
    // ...
Romain Hippeau
  • 23,328
  • 5
  • 53
  • 74
2

Use Java.awt.Robot to take a screenshot as an Image and proceess that.

Thorbjørn Ravn Andersen
  • 68,906
  • 28
  • 171
  • 323