Java壓縮圖片錯誤——蒙上一層紅色

1 起源

生產問題——現場運維反饋安卓客戶端從文件服務器獲取的壓縮後的商品圖片存在失真,屬於偶現問題。

2 排查思路

(1)查看算法邏輯,沒看出問題。

(2)百度,關鍵詞:Java圖片失真,看了很多博客都說是ImageIO和BufferedImage以及API參數的問題,比如加上參數 BufferedImage.SCALE_SMOOTH:

graph.drawImage(cropedImage.getScaledInstance(targetW,  targetH,  
BufferedImage.SCALE_SMOOTH), 0, 0, Color.white,null);

試過後,沒有用,進一步查看JDK官方與圖片相關的API,沒有提到特別的注意事項。
(3)google、Stack Overflow
提到是JDK的bug,但是在oracle官網提到圖片失真是JDK1.4的bug,但是1.6已結修復了,而項目中用到的是JDK1.7.
(4)百度,關鍵詞:Java 壓縮 圖片後 表面變成紅色
參考鏈接:https://blog.csdn.net/qq_25446311/article/details/79140008
根據文章內容,順利解決。
轉換代碼:


public BufferedImage toBufferedImage(Image image) {
		if (image instanceof BufferedImage) {
			return (BufferedImage) image;
		}
		// This code ensures that all the pixels in the image are loaded
		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) {
			// The system does not have a screen
		}
		if (bimage == null) {
			// Create a buffered image using the default color model
			int type = BufferedImage.TYPE_INT_RGB;
			bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
		}
		// Copy image to buffered image
		Graphics g = bimage.createGraphics();
		// Paint the image onto the buffered image
		g.drawImage(image, 0, 0, null);
		g.dispose();
		return bimage;
	}
	

3 總結

(1)搜索引擎使用
經過前後對比可以看到,準確描述問題的重要性。
(2)錯誤總結
只要使用 ImageIO.read 就可能存在圖片蒙上紅色的情況,原因是拍攝圖片的設備問題,比如攝像機拍攝就可能存在該問題,手機可能就沒有。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章