【发布时间】: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