简体   繁体   English

读取/写入JPG图像而不损失质量的最快方法?

[英]Fastest way to Read/Write JPG Images without losing Quality?

I have JPG image files, which I want to load into a BufferedImage and later write the BufferedImage back into a JPG file. 我有JPG图片文件,我想将其加载到BufferedImage中,然后再将BufferedImage写回到JPG文件中。 Here is what I am currently doing. 这是我目前正在做的事情。

Is there a better way not to lose quality and make read/write faster? 有没有更好的方法而不丢失质量并使读/写速度更快?

Read: 读:

BufferedImage image = ImageIO.read(new File(storagePath + fileName + extension));

Write: 写:

BufferedImage image = // some jpg image         

Iterator iter = ImageIO.getImageWritersByFormatName("JPG");
if (iter.hasNext()) {
    ImageWriter writer = (ImageWriter) iter.next();
    ImageWriteParam iwp = writer.getDefaultWriteParam();
    iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    iwp.setCompressionQuality(quality);

    File outFile = new File(storagePath + fileName + extension);
    FileImageOutputStream output = new FileImageOutputStream(outFile);
    writer.setOutput(output);
    IIOImage iioImage = new IIOImage(image, null, null);
    writer.write(null, iioImage, iwp);
}

You can minimize the loss in quality in recompressing JPEG by using the same quantization tables used to correct the original image. 通过使用与校正原始图像相同的量化表,可以最大程度地减少重新压缩JPEG时的质量损失。 It is still possible to get single bit errors from rounding but you can get it pretty close. 仍然有可能从舍入中获得一位错误,但您可以将其接近。

The problem is how to get the quantization tables. 问题是如何获得量化表。 If your encoded will allow you to specify them, you can pull the values out of the source image. 如果您的编码允许您指定它们,则可以将值从源图像中拉出。 Otherwise, you have to hope that the images were originally encoded using the same encoder. 否则,您必须希望最初使用相同的编码器对图像进行编码。

"Quality Values" are not part of JPEG. “质量值”不是JPEG的一部分。 They are a method for selecting quantization tables used by some encoder. 它们是一种选择某些编码器使用的量化表的方法。 The LIBJPEG is the most common encoder but there are others out there that do things differently. LIBJPEG是最常见的编码器,但还有其他功能则有所不同。

PNG encoding is generally slower than JPEG. PNG编码通常比JPEG慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM