将图像转换为颜色数组 [英] Converting Image to Color array

查看:141
本文介绍了将图像转换为颜色数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中加载图像,并想要转换为RGB阵列,这样我可以读取每个单一像素的颜色。我在Google上搜索,但我只找到了如何将颜色数组转换为图像。

I load an image in Java and want to convertit to a RGB-Array so I can read the color of each single pixel. I searched on Google, but I only found how to convert Color-Arrays to Images.

推荐答案

API方法:

BufferedImage bi = ImageIO.read( new File( "image.png" ) );
int[] data = ( (DataBufferInt) bi.getRaster().getDataBuffer() ).getData();
for ( int i = 0 ; i < data.length ; i++ ) {
    Color c = new Color(data[i]);
    // RGB is now accessible as
    c.getRed();
    c.getGreen();
    c.getBlue();
}

如果您遇到由于颜色模型引起的问题, p>

If you face issues due to the color model, create a copy first

BufferedImage img2 = new BufferedImage( bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_RGB );

img2.getGraphics().drawImage( bi, 0, 0, null );

并在上述代码中使用img2。

and use img2 in the above code.

这篇关于将图像转换为颜色数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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