开发者

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

开发者 https://www.devze.com 2023-01-16 12:53 出处:网络
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

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消