java中怎么放入.jpg,如何在Java中保存无损jpg?

I have to save a jpeg image lossless. I am work on a steganography project but Java compressing and saving my result. I research every forums and try everything but it didn't work.

Here my example code for lossless save a jpeg image:

BufferedImage image = ImageIO.read(new File("sources/image.jpg"));

ImageWriter writer = ImageIO.getImageWritersByFormatName("JPEG").next();

JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(null);

jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);

jpegParams.setCompressionQuality(1f);

writer.setOutput(ImageIO.createImageOutputStream(new File("example.jpg")));

writer.write(null, new IIOImage(image,null,null), jpegParams);

writer.dispose();

After this process I compute PSNR value is 28.53173 and "example.jpg"'s size is bigger than "image.jpg".

I try import JAI library but I am not sure Java 8 is support JAI.

解决方案

JPEG is lossy all the time. Even at 100% compression quality there will be some loss of information, but it will be minimised.

The reason why your example.jpg has a bigger size is because it was encoded with a 100% quality factor, while most jpeg encoders have a default value of 50%-75%, which is what was most likely used for example.jpg. You can try different quality factors to see when both files have the same size.

A lossless JPEG format does exist (available in JAI), but it should be thought of as a different format to the conventional JPEG. However, it's not widely used and you'll probably not be able to view it in most applications, which would practically defeat the point of sharing an innocent image.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值