学习使用环境: jfinal-club-1.6
问题:
更换头像, 上传png图片时, 出现图片颜色失真.
如博客所示. 地址:
https://blog.csdn.net/xujie3/article/details/51848831
用java转换图片格式(不是修改后缀名),png转jpg时遇到一个问题,图片发生颜色失真,白色背景变成橘红色,加两句代码就可以解决这个问题。
解决方法:
ImageKit这个类中. 方法
saveWithQuality
增加代码
BufferedImage newBufferedImage = new BufferedImage(im.getWidth(), im.getHeight(), BufferedImage.TYPE_INT_RGB);
newBufferedImage.createGraphics().drawImage(im, 0, 0, Color.WHITE, null);
public static void saveWithQuality(BufferedImage im, float quality, String outputImageFile) { ImageWriter writer = null; FileOutputStream newImage = null; try { BufferedImage newBufferedImage = new BufferedImage(im.getWidth(), im.getHeight(), BufferedImage.TYPE_INT_RGB); newBufferedImage.createGraphics().drawImage(im, 0, 0, Color.WHITE, null); // 输出到文件流 newImage = new FileOutputStream(outputImageFile); writer = ImageIO.getImageWritersBySuffix("jpg").next(); ImageWriteParam param = writer.getDefaultWriteParam(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(quality); ImageOutputStream os = ImageIO.createImageOutputStream(newImage); writer.setOutput(os); writer.write((IIOMetadata) null, new IIOImage(newBufferedImage, null, null), param); os.flush(); os.close(); } catch (IOException e) { throw new RuntimeException(e); } finally { if (writer != null) { try {writer.dispose();} catch (Throwable e) {} } if (newImage != null) { try {newImage.close();} catch (IOException e) {throw new RuntimeException(e);} } } }