简体   繁体   English

获取图像的所有RGB颜色

[英]Getting all RGB colors of an image

So far I have this: 到目前为止,我有这个:

BufferedImage image = ImageIO.read(
     new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"));

int w = image.getWidth();
int h = image.getHeight();

int[] dataBuffInt = image.getRGB(0, 0, w, h, null, 0, w); 

Color c = new Color(dataBuffInt[100]);

System.out.println(c.getRed());   // = (dataBuffInt[100] >> 16) & 0xFF
System.out.println(c.getGreen()); // = (dataBuffInt[100] >> 8)  & 0xFF
System.out.println(c.getBlue());  // = (dataBuffInt[100] >> 0)  & 0xFF
System.out.println(c.getAlpha()); // = (dataBuffInt[100] >> 24) & 0xFF

Earlier, I tried putting the getRed , getGreen , and getBlue in a for loop but it only shows the same RGB value. 之前,我尝试将getRedgetGreengetBlue放入for循环中,但它只显示相同的RGB值。 How do I get all the RGB values in an image? 如何获得图像中的所有RGB值? Given that I wanna store them in different arrays. 鉴于我想将它们存储在不同的数组中。

I'm not entirely clear on the question, but assuming you mean unique RGB values, just loop, and just use say java.util.Set implementation that maintains uniqueness? 我对这个问题尚不完全清楚,但是假设您的意思是唯一的RGB值,则循环并仅使用说保持唯一性的java.util.Set实现?

Set<Color> colors = new HashSet<Color>();
for (int datum : dataBuffInt) {
    colors.add(new Color(datum));
}
System.out.println(String.format("%d different colors", colors.size()));

Or if you mean separate components? 或者,如果您是指单独的组件?

for (int datum : dataBuffInt) {
    Color color = new Color(datum);
    reds.add(color.getRed());
    greens.add(color.getGreen());
    blues.add(color.getBlue());

}
System.out.println(String.format("reds: %d greens: %d blues: %d", reds.size(), greens.size(), blues.size()));

Are you certain when you had the for loop you were using the index variable into the array and not a static value, like 100 ? 您确定在有for循环时会在数组中使用索引变量而不是静态值(如100吗? When I run your code with a for loop I see different values: 当我使用for循环运行代码时,我看到了不同的值:

for (int i = 0; i < dataBuffInt.length; i++) {
    Color c = new Color(dataBuffInt[i]);

    System.out.println("COLOR");
    System.out.println(c.getRed());   // = (dataBuffInt[100] >> 16) & 0xFF
    System.out.println(c.getGreen()); // = (dataBuffInt[100] >> 8)  & 0xFF
    System.out.println(c.getBlue());  // = (dataBuffInt[100] >> 0)  & 0xFF
    System.out.println(c.getAlpha()); // = (dataBuffInt[100] >> 24) & 0xFF
    System.out.println();
}

If you want unique colors you could build a set one pixel at a time: 如果您想要独特的颜色,则可以一次设置一个像素:

final BufferedImage image = ImageIO.read(new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"));
final Set<Color> uniqueColors = new HashSet<Color>(image.getWidth() * image.getHeight());
for (int y = 0; y < image.getHeight(); y++) {
    for (int x = 0; x < image.getWidth(); x++) {
        final int rgb = image.getRGB(x, y);
        uniqueColors.add(new Color(rgb));
    }
}

for (final Color color : uniqueColors) {
    System.out.println(format("red: {0}, green: {1}, blue: {2}, alpha: {3}",
            color.getRed(),
            color.getGreen(),
            color.getBlue(),
            color.getAlpha()));
}

Or use your existing code and dump the array into a set. 或使用现有代码并将数组转储到集合中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM