从PNG文件中获取collor信息

时间:2015-04-09 23:34:08

标签: java c++ png

如果您能够查看png文件的字节,那么如何获取颜色信息。你怎么知道哪些字节是红色,蓝色或绿色。在查看png文件的字节时有没有办法提取颜色信息?

使用C ++或Java提取像素颜色的过程是什么?

1 个答案:

答案 0 :(得分:1)

在Java中你可以做这样的事情

public static Color[][]  byteArrayToColors(byte[] bytes){  
        BufferedImage paintImage=null;
        try {
            InputStream inputStream = new ByteArrayInputStream(bytes);
            paintImage = ImageIO.read(inputStream);
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
         Color[][] cols = new Color[paintImage.getWidth()][paintImage.getHeight()];
    for(int z = 0;z < paintImage.getWidth();z++){
        for(int a = 0;a < paintImage.getHeight();a++){
            int color = paintImage.getRGB(z, a);

            int  red = (color & 0x00ff0000) >> 16;
            int  green = (color & 0x0000ff00) >> 8;
            int  blue = color & 0x000000ff;
            int alpha = (color>>24) & 0xff;
            Color col = new Color(red,green,blue,alpha);
            cols[z][a] = col;

        }
    }
return cols;
}