如何确定图像(java.awt.Image)是否为B& W,没有颜色? [英] How do you determine if an image (java.awt.Image) is B&W, with no color?

查看:113
本文介绍了如何确定图像(java.awt.Image)是否为B& W,没有颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定图像是简单的黑白图像,还是有颜色(使用Java)。如果它有颜色,我需要向用户显示一条警告消息,颜色将会丢失。

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.

推荐答案

BufferedImage 类有一个方法 int getRGB(int x,int y)返回一个十六进制整数,表示(x,y)处像素的颜色(以及另一种返回像素矩阵的方法)。从那里你可以得到如下的r,g和b值:

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);

然后检查它们是否对于图像中的每个像素都相等。如果对于每个像素r == g == b,那么它是灰度级的。

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.

这篇关于如何确定图像(java.awt.Image)是否为B& W,没有颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆