Java 如何将RGB值与颜色进行比较?

Java 如何将RGB值与颜色进行比较?,java,colors,rgb,Java,Colors,Rgb,我正在扫描buffereImage中的像素,看看其中的一些像素是否是某种颜色。我通过以下方式尝试了这一点: for(int x = 0; x < 16; x++) { for(int y = 0; y < 16; y++) { if(image.getRGB(x, y) == new Color(209, 167, 86).getRGB()) System.out.println("Same Color Detected!"); } } 下面是我如何

我正在扫描
buffereImage
中的像素,看看其中的一些像素是否是某种颜色。我通过以下方式尝试了这一点:

for(int x = 0; x < 16; x++) {
    for(int y = 0; y < 16; y++) {
        if(image.getRGB(x, y) == new Color(209, 167, 86).getRGB()) System.out.println("Same Color Detected!");
    }
}
下面是我如何获得图像的:

playerOrig = ImageIO.read(getClass().getResourceAsStream("/Player/player.gif"));
我正在使用Eclipse和Java1.6

我打印出了图像的
ColorModel
,得到了以下结果:

IndexColorModel: #pixelBits = 4 numComponents = 4 color space = java.awt.color.ICC_ColorSpace@45d6a56e transparency = 2 transIndex   = 11 has alpha = true isAlphaPre = false
java.awt.color.ICC_ColorSpace@45d6a56e
然后,我打印出
Color
对象的
ColorSpace
,得到如下结果:

IndexColorModel: #pixelBits = 4 numComponents = 4 color space = java.awt.color.ICC_ColorSpace@45d6a56e transparency = 2 transIndex   = 11 has alpha = true isAlphaPre = false
java.awt.color.ICC_ColorSpace@45d6a56e
图为:

playerOrig = ImageIO.read(getClass().getResourceAsStream("/Player/player.gif"));

对我有用。。。 注意:我在本地机器上测试了代码,它对我来说很好,就像您使用默认的
ColorModel
一样。我正在使用OSX和Java(TM)SE运行时环境(build 1.7.0_25-b15)祝你好运

这是一个
根据您提供的示例图像,当图像大小为320x320像素时,您仅检查图像的16x16网格

当我更新你的代码以包含图像的全宽和全高时,它工作得很好

try {
    BufferedImage img = ImageIO.read(...);
    Color match = new Color(209, 167, 86);

    for (int x = 0; x < img.getWidth(); x++) {
        for (int y = 0; y < img.getHeight(); y++) {
            if (img.getRGB(x, y) == match.getRGB()) {
                System.out.println("Same Color Detected!");
            }
        }
    }
} catch (IOException ex) {
    ex.printStackTrace();
}
试试看{
BuffereImage img=ImageIO.read(…);
颜色匹配=新颜色(20916786);
对于(int x=0;x
已更新

基于新的、更小的示例,代码仍然有效

为了证明这一点,我写了一个简单的颜色替换算法


试试看{
BuffereImage img=ImageIO.read(新文件(“8bit.gif”);
BuffereImage replaced=新的BuffereImage(img.getWidth(),img.getHeight(),BuffereImage.TYPE_INT_ARGB);
颜色匹配=新颜色(20916786);
颜色为=新颜色(0,255,0);
对于(int x=0;x
image.getRGB()
Color.getRGB()
都返回
int
所以请原谅我的错误confusion@MadProgrammer很抱歉,我指的是另一个值。有示例图像吗?它是否包含alpha值?您知道您提供的示例图像是320x320像素Sokay,works find for me…那么如何获得表示相同内容的值?
新颜色(int,boolean)
将创建一个新的
颜色
对象,表示压缩的
int
值。如果
int
值包含一个alpha位,则希望传递
true
。然后,您可以使用
Color#getRed/Green/Blue
对inducial Color组件进行编辑,我得到了相同的输出。我将在目录中创建一个新的图像文件。它仍然不起作用,我认为这可能与以下事实有关:图像的拍摄方法与您在这里的拍摄方法不同。@SparklyLama-我将图像放在类路径上,如果从那里加载,则加载完全相同的正确输出。请参阅我对代码的编辑。你正在阅读的图像不是你认为的那样,没有任何其他明显的原因。颜色替换正是我正在做的,我和你有完全相同的代码,它不适合我。“不知道这里发生了什么……”斯帕克里拉没有上下文,很难知道。您拥有的
图像
参考可能不是您所期望的,
图像
可能是使用一些奇怪的颜色空间创建的。。。
try {
    BufferedImage img = ImageIO.read(new File("8bit.gif"));
    BufferedImage replaced = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Color match = new Color(209, 167, 86);
    Color with = new Color(0, 255, 0);

    for (int x = 0; x < img.getWidth(); x++) {
        for (int y = 0; y < img.getHeight(); y++) {
            int pixel = img.getRGB(x, y);
            if (pixel == match.getRGB()) {
                System.out.println("Same Color Detected!");
                replaced.setRGB(x, y, with.getRGB());
            } else {
                replaced.setRGB(x, y, pixel);
            }
        }
    }

    ImageIO.write(replaced, "png", new File("replaced.png"));

    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(replaced)));
} catch (IOException ex) {
    ex.printStackTrace();
}