java 储存颜色,用Java保存时图像会更改颜色

When I save this image:

holiday-series-2013-3-4504416610680832-hp.jpg

with this method:

private final static Path ROOT_PATH = Paths.getPath("C:/images");

private static void saveImageFromWebSimple(final String url) {

URL u = null;

try {

u = new URL(url);

} catch (MalformedURLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

String file = url.substring(url.indexOf("//") + 2);

Path filePath = ROOT_PATH.resolve(file);

try {

Files.createDirectories(filePath.getParent());

BufferedImage img = ImageIO.read(u);

ImageIO.write(img, "jpg", filePath.toFile());

} catch (IOException e) {

e.printStackTrace();

}

}

this is my result:

W8WUE.jpg

This doesn't happen with all pictures though.

Can you tell me why?

解决方案

According to @uckelman's comment on this post, Java's decoder makes a different assumption about the format of the image than most other renders when the image is missing the JFIF header:

I believe the answer to your question of how to detect the bad JPEGs

is found here and here. What you have is a JPEG with no JFIF marker.

All other image loaders assume that the data is YCbCr in that case,

except for ImageIO, which assumes that it is RGB when channels 1 and 2

are not subsampled. So, check whether the first 4 bytes are FF D8 FF

E1, and if so, whether channels 1 and 2 are subsampled. That's the

case where you need to convert.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值