Java處理某些圖片紅色問題

百度了  微信平臺上傳圖片變紅  找到這個解決辦法

問題現象:

Java上傳圖片時,對某些圖片進行縮放、裁剪或者生成縮略圖時會蒙上一層紅色,通過檢查只要通過ImageIO.read()方法讀取後再保存,該圖片便已經變成紅圖。所以,能夠推測直接緣由在於ImageIO.read()方法加載圖片的過程存在問題。java


[java] view plain copy 在CODE上查看代碼片派生到個人代碼片微信

  1. public static BufferedImage getImages(byte[] data) throws IOException {  less

  2.         ByteArrayInputStream input = new ByteArrayInputStream(data);  ide

  3.         return ImageIO.read(input);  ui

  4.     }  url


通過查閱得知ImageIO.read()方法讀取圖片時可能存在不正確處理圖片ICC信息的問題,ICC爲JPEG圖片格式中的一種頭部信息,致使渲染圖片前景色時蒙上一層紅色。spa

解決方案:

再也不使用ImageIO.read()方法加載圖片,而使用JDK中提供的Image src=Toolkit.getDefaultToolkit().getImage.net


[java] view plain copy 在CODE上查看代碼片派生到個人代碼片code

  1. Image src=Toolkit.getDefaultToolkit().getImage(file.getPath());  orm

  2. BufferedImage p_w_picpath=BufferedImageBuilder.toBufferedImage(src);//Image to BufferedImage  


或者Toolkit.getDefaultToolkit().createImage



[java] view plain copy 在CODE上查看代碼片派生到個人代碼片

  1. Image p_w_picpathTookit = Toolkit.getDefaultToolkit().createImage(bytes);  

  2. BufferedImage cutImage = BufferedImageBuilder.toBufferedImage(p_w_picpathTookit);  


BufferedImageBuilder源碼:



[java] view plain copy 在CODE上查看代碼片派生到個人代碼片

  1. public static BufferedImage toBufferedImage(Image p_w_picpath) {  

  2.         if (p_w_picpath instanceof BufferedImage) {  

  3.             return (BufferedImage) p_w_picpath;  

  4.         }  

  5.         // This code ensures that all the pixels in the p_w_picpath are loaded  

  6.         p_w_picpath = new ImageIcon(p_w_picpath).getImage();  

  7.         BufferedImage bp_w_picpath = null;  

  8.         GraphicsEnvironment ge = GraphicsEnvironment  

  9.                 .getLocalGraphicsEnvironment();  

  10.         try {  

  11.             int transparency = Transparency.OPAQUE;  

  12.             GraphicsDevice gs = ge.getDefaultScreenDevice();  

  13.             GraphicsConfiguration gc = gs.getDefaultConfiguration();  

  14.             bp_w_picpath = gc.createCompatibleImage(p_w_picpath.getWidth(null),  

  15.                     p_w_picpath.getHeight(null), transparency);  

  16.         } catch (HeadlessException e) {  

  17.             // The system does not have a screen  

  18.         }  

  19.         if (bp_w_picpath == null) {  

  20.             // Create a buffered p_w_picpath using the default color model  

  21.             int type = BufferedImage.TYPE_INT_RGB;  

  22.             bp_w_picpath = new BufferedImage(p_w_picpath.getWidth(null),  

  23.                     p_w_picpath.getHeight(null), type);  

  24.         }  

  25.         // Copy p_w_picpath to buffered p_w_picpath  

  26.         Graphics g = bp_w_picpath.createGraphics();  

  27.         // Paint the p_w_picpath onto the buffered p_w_picpath  

  28.         g.drawImage(p_w_picpath, 00null);  

  29.         g.dispose();  

  30.         return bp_w_picpath;  

  31.     }  


參考:

http://blog.csdn.net/kobejayandy/article/details/44346809

相關文章
相關標籤/搜索