Java-ImageIO.write(image, "jpg", iamgeOut)输出的图片失真
ImageIO.write(image, "jpg", iamgeOut)输出的图片会失真,ImageIO.write(image, "png", iamgeOut)则不会,原因何在?
是因为jpg的格式会压缩,而png不会吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
原因:图片本身的问题(ICC丢失或不全)
链接:http://blog.csdn.net/shixing_11/article/details/6897871
public static BufferedImage toBufferedImage(File img) throws IOException {
Image image = Toolkit.getDefaultToolkit().getImage(img.getPath());
BufferedImage bimage = null;
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
image = new ImageIcon(image).getImage();
int width = image.getWidth(null);
int height = image.getHeight(null);
if (width < 0 || height < 0) {
/*Map<String,Integer> map = getImageSize(img);
width = map.get(IMAGE_WIDTH);
height = map.get(IMAGE_HEIGHT);*/
return null;
}
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
int transparency = Transparency.OPAQUE;
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(width, height, transparency);
} catch (HeadlessException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
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;
}
在dispose()之后不要return了,直接打印出来
ServletOutputStream responseOutputStream = response.getOutputStream();
// 输出图象到页面
ImageIO.write(image, "JPEG", responseOutputStream);
out.clear();
out = pageContext.pushBody();