Lukas
Lukas

Reputation: 9845

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.

Upvotes: 4

Views: 4912

Answers (1)

Tom Smilack
Tom Smilack

Reputation: 2075

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.

Upvotes: 9

Related Questions