【问题标题】:how can i extract the colors in the image using java?如何使用java提取图像中的颜色?
【发布时间】:2017-07-20 09:21:26
【问题描述】:

我正在尝试使用以下代码来提取图像中的颜色。我正在使用以下代码,但无法获取颜色。谁能有更好的解决方案,或者请告诉我的代码有什么问题。

public static void main(String args[]) throws IOException {
    File file = new File("image.png");
    BufferedImage image = ImageIO.read(file);
    //  int clr;
    int redd = 0;
    int greenn = 0;
    int bluee = 0;
    for (int i = 0; i < image.getHeight(); i++) {
        for (int j = 0; j < image.getWidth(); j++) {
            int clr = image.getRGB(i, j);
            redd = (clr & 0x00ff0000) >> 16;
            greenn = (clr & 0x0000ff00) >> 8;
            bluee = clr & 0x000000ff;

        }
    }
    System.out.println("Red Color value = " + redd);
    System.out.println("Green Color value = " + greenn);
    System.out.println("Blue Color value = " + bluee);

任何建议都会非常有帮助,谢谢:-)

【问题讨论】:

  • 您想要颜色的确切位置,因为您读取了所有图像但您在每次迭代时都会覆盖,因此您将只有最后一个像素的数据
  • 图片的宽高互换。除此之外,代码基本上是“正确的”,所以不清楚实际问题是什么。 (如果问题是异常,你至少应该提到它)

标签: java ocr rgb bufferedimage


【解决方案1】:

您最好使用 Java 的 Color 库来解析图像的 RGB 组件:

Color clr = new Color(image.getRGB());
redd = c.getRed();
greenn = c.getGreen();
bluee = c.getBlue();

你看看有没有帮助?

【讨论】:

  • 我已经尝试过使用此代码,但它现在可以工作了,在 getrgb 中它要求坐标,如果我分配坐标,它会抛出与我提出的问题相同的错误
  • 你能更新你的帖子,说明抛出了什么错误吗?
【解决方案2】:

你可以使用一些javafx features

import javafx.scene.image.Image;
import javafx.scene.image.PixelReader;
import javafx.scene.paint.Color;

public class Test {
    public static void main(String[] args) {
        Image image = new Image("image.png");
        PixelReader reader = image.getPixelReader();
        Color c = reader.getColor(154, 87);
        System.out.println(c);
    }
}

【讨论】:

  • 哪个?您是否更改了图像的路径..?
  • 是的,我已经更改了图像的路径,但它会引发很多错误
  • 无论是在您的帖子中,还是在您发布错误的任何地方..请给他们
猜你喜欢
  • 2019-04-30
  • 1970-01-01
  • 2019-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-18
  • 2012-08-15
相关资源
最近更新 更多