Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you determine if an image (java.awt.Image) is B&W, with no color?

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

like image 299
Lukas Avatar asked Jan 21 '23 11:01

Lukas


1 Answers

The BufferedImage class has a method int getRGB(int x, int y) that returns a hex integer representing the color of the pixel at (x, y) (and another method that returns a matrix of pixels). From that you can get the r, g, and b values like so:

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

and then check whether they are all equal for every pixel in the image. If r == g == b for every pixel, then it's in gray scale.

That's the first thing that comes to mind. I'm not sure if there would be some kind of gray scale flag set when reading in an image.

like image 181
Tom Smilack Avatar answered Jan 29 '23 23:01

Tom Smilack