How do you determine if an image (java.awt.Image) is black and white, without color?

I am trying to determine if an image is a simple black and white image or has a color (using Java). If it has color, I need to show the user a warning that the color will be lost.

+4
source share
1 answer

The BufferedImage class has an int getRGB(int x, int y) method int getRGB(int x, int y) that returns a hexadecimal integer representing the pixel color in (x, y) (and another method that returns a pixel matrix). From this you can get the values ​​of r, g and b as follows:

  int r = (0x00ff0000 & rgb) >> 16;
 int g = (0x0000ff00 & rgb) >> 8;
 int b = (0x000000ff & rgb); 

and then check if they are all equal for each pixel in the image. If r == g == b for each pixel, then it is in gray scale.

This is the first thing that comes to mind. I'm not sure if any grayscale flag will be set when reading in the image.

+8
source

All Articles