问题描述
我必须保存无损的jpeg图像.我正在从事一个隐写术项目,但是Java压缩并保存了我的结果.我研究了每个论坛并尝试了所有方法,但没有成功.
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.
这是我的无损保存jpeg图像的示例代码:
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();
此过程后,我计算出PSNR值为28.53173," example.jpg "的大小大于" image.jpg ".
After this process I compute PSNR value is 28.53173 and "example.jpg"'s size is bigger than "image.jpg".
我尝试导入JAI库,但是我不确定Java 8是否支持JAI.
I try import JAI library but I am not sure Java 8 is support JAI.
推荐答案
JPEG一直有损.即使以100%的压缩质量,也会丢失一些信息,但是可以将其减少到最低程度.
JPEG is lossy all the time. Even at 100% compression quality there will be some loss of information, but it will be minimised.
您的example.jpg具有更大尺寸的原因是因为它是使用100%品质因数进行编码的,而大多数jpeg编码器的默认值是50%-75%,这是最有可能使用的示例.jpg.您可以尝试使用不同的质量因数来查看两个文件的大小是否相同.
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.
确实存在无损JPEG格式(在JAI中可用),但是应将其视为与常规JPEG不同的格式.但是,它没有被广泛使用,并且您可能无法在大多数应用程序中查看它,这实际上会破坏共享无辜图像的观点.
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.
这篇关于如何在Java中保存无损jpg?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!