有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从PNG文件获取java或信息

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

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

共 (1) 个答案

  1. # 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;
    }