Java处理某些图片红色问题(ImageIO.read()读取ICC)

imageIO.read 在读取图片的时候可能会不正确的读取图片的ICC信息,

所以当这种情况不能用ImageIO读取图片文件

使用java中自带jdk

 

ICC为JPEG图片格式中的一种头部信息,导致渲染图片前景色时蒙上一层红色。

 

Image src=Toolkit.getDefaultToolkit().getImage(file.getPath()); 
BufferedImage image=BufferedImageBuilder.toBufferedImage(src);

public static BufferedImage toBufferedImage(Image image) { 
        if (image instanceof BufferedImage) { 
            return (BufferedImage) image; 
        } 
        image = new ImageIcon(image).getImage(); 
        BufferedImage bimage = null; 
        GraphicsEnvironment ge = GraphicsEnvironment 
                .getLocalGraphicsEnvironment(); 
        try { 
            int transparency = Transparency.OPAQUE; 
            GraphicsDevice gs = ge.getDefaultScreenDevice(); 
            GraphicsConfiguration gc = gs.getDefaultConfiguration(); 
            bimage = gc.createCompatibleImage(image.getWidth(null), 
                    image.getHeight(null), transparency); 
        } catch (HeadlessException e) { 
        } 
        if (bimage == null) { 
            int type = BufferedImage.TYPE_INT_RGB; 
            bimage = new BufferedImage(image.getWidth(null), 
                    image.getHeight(null), type); 
        } 
        Graphics g = bimage.createGraphics(); 
        g.drawImage(image, 0, 0, null); 
        g.dispose(); 
        return bimage; 
    } 

 

你可能感兴趣的:(java,image,swing,awt,ImageIO)