0

I am trying to get screen captures of my computer screen and then process the data. Since I am processing the data, I need to know what color scheme like ARGB or RGB or BGR is being used for the following BufferedImage used to store the screen capture.

BufferedImage img = robot.createScreenCapture(new Rectangle(0,0,Toolkit.getDefaultToolkit().getScreenSize().width,Toolkit.getDefaultToolkit().getScreenSize().height));

I wrote these lines of code to see if the color scheme used made a difference in the data received

int[] pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
Color color = new Color(pixels[80000], true);
System.out.println( "ALPHA: " + color.getAlpha());
System.out.println( "RED: " + color.getRed());
System.out.println( "GREEN: " + color.getGreen());
System.out.println( "BLUE: " + color.getBlue());
        
color = new Color(pixels[80000], false);
System.out.println( "ALPHA: " + color.getAlpha());
System.out.println( "RED: " + color.getRed());
System.out.println( "GREEN: " + color.getGreen());
System.out.println( "BLUE: " + color.getBlue());

The first color is created with an alpha value, but the second one drops it. And, this was the result

ALPHA: 255

RED: 63

GREEN: 72

BLUE: 204

ALPHA: 255

RED: 63

GREEN: 72

BLUE: 204

So, it appears that the two sets are identical despite using two different color schemes. Why is this? Do the screen captures not have a specific color scheme?

Some Guy
  • 143
  • 9
  • 2
    If the actual pixel happens to have an alpha of 255, then it makes no difference whether you pass true or false to the Color constructor. (And I’m pretty sure a screen capture will never have transparency in any pixel.) Of course, since [the documentation](https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/java/awt/Robot.html#createScreenCapture(java.awt.Rectangle)) doesn’t make any guarantees, it will never be safe to make any assumptions about the color model of the returned image. – VGR Jan 25 '22 at 18:24
  • @VGR I tried .getAlphaRaster on the screen capture and got null, does that mean it has no alpha? – Some Guy Jan 25 '22 at 18:27
  • Again, [the documentation](https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/java/awt/image/BufferedImage.html#getAlphaRaster()) has your answer. – VGR Jan 25 '22 at 18:29
  • 2
    Being a screen capture, most likely the pixel layout will vary, depending on your screen's configuration. You could check `BufferedImage.getType()` for the pixel layout. But it's probably safer and easier to just use `BufferedImage.getRGB(x, y)` which will always return a packed 32 bit ARGB representation in sRGB color space (same as `BufferedImage.TYPE_INT_ARGB`). – Harald K Jan 26 '22 at 09:42

0 Answers0