使用 ImageIO.write() 方法时如何防止图像质量损失? [英] How to prevent loss of image quality while using ImageIO.write() method?

查看:443
本文介绍了使用 ImageIO.write() 方法时如何防止图像质量损失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 java 中处理图像,我在本地磁盘中读取和写入图像.我的问题是在写图像时我失去了我阅读的实际图像的质量.它通过图像文件大小将质量从 6.19MB(实际图像大小)文件降低到 1.22MB(使用 ImageIO.write() 写入,这是巨大的损失.我在使用时如何防止这种损失

I am dealing with Images in java where I do read and write Images in my local disk. My Problem is while writing Images I am losing the quality of the actual Image I read. It reduces the quality by image file size from 6.19MB(actual image size) file to 1.22MB(written using ImageIO.write() which is drastic loss. How can I prevent this loss when i do use

 ImageIO.write(image, "jpg", os);

用于写入图像.请记住,我在这里不需要任何压缩.只是我想读取图像并以相同的质量和相同的文件大小写入相同的图像.我也试过了,

for writing Image. Remember, I dont need any compression over here. Just I want to read the Image and write the same image with same quality and same file Size. I also tried,

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

但它需要更多的执行时间并进行压缩过程.

but it takes my execution time more and does a compression process.

请帮我解决这个问题.有没有机会用

Please help me out in this. Is there any chance to write lossless image quality using

 ImageIO.write(image, "jpg", os);

或者其他方式?

提前致谢...!

推荐答案

您似乎对 JPEG 的压缩"和质量"有点困惑(我部分归咎于 ImageIO API,因为他们将设置命名为压缩质量").

You seem to be a little confused about JPEG "compression" and "quality" (and I partly blame the ImageIO API for that, as they named the setting "compressionQuality").

JPEG 总是会被压缩.这就是格式的重点.如果您不想压缩,JPEG 不是您想要使用的格式.尝试未压缩的 TIFF 或 BMP.正如评论者已经说过的那样,普通的 JPEG 总是有损的(顺便说一句,JPEG-LS 和 JPEG Lossless 确实是不同的算法).

JPEG is always going to compress. That's the point of the format. If you don't want compression, JPEG is not the format you want to use. Try uncompressed TIFF or BMP. As the commenters have already said, normal JPEG is always going to be lossy (JPEG-LS and JPEG Lossless are really different algorithms btw).

如果您仍想使用 JPEG,这里有一些帮助.不幸的是,无法使用以下方法控制compressionQuality"设置:

If you still want to go with JPEG, here's some help. Unfortunately, there's no way to control the "compressionQuality" setting using:

ImageIO.write(image, "jpg", os);

您需要使用更详细的形式:

You need to use the more verbose form:

ImageReader reader = ...;
reader.setInput(...);
IIOImage image = reader.readAll(0, null); // Important, also read metadata

RenderedImage renderedImage = image.getRenderedImage();

// Modify renderedImage as you like

ImageWriter writer = ImageIO.getImageWriter(reader);
ImageWriteParam param = writer.getDefaultWriteParam();
paran.setCompressionMode(MODE_COPY_FROM_METADATA); // This modes ensures closest to original compression

writer.setOutput(...);
writer.write(null, image, param); // Write image along with original meta data

注意:try/finally 块和流 close() 和 reader/writer dispose() 为清晰起见省略.您会希望在实际代码中使用它们.:-)

Note: try/finally blocks and stream close()and reader/writer dispose() omitted for clarity. You'll want them in real code. :-)

这不应该使您的执行时间明显变长,因为 ImageIO.write(...) 在内部使用类似的代码.

And this shouldn't really make your execution time noticeably longer, as ImageIO.write(...) uses similar code internally.

这篇关于使用 ImageIO.write() 方法时如何防止图像质量损失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆