如何在java中使jpeg无损?

发布于 2024-12-07 19:30:23 字数 268 浏览 1 评论 0原文

有人可以告诉我如何在java中使用无损压缩编写'jpeg'文件吗?

我使用下面的代码读取字节来编辑字节

WritableRaster raster = image.getRaster();
DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer();

,并且我需要将字节再次写入为“jpeg”文件,而不用有损进行压缩。

Is there someone that can tell me how to write 'jpeg' file using lossless compression in java?

I read the bytes using the code below to edit the bytes

WritableRaster raster = image.getRaster();
DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer();

And I need to write again the bytes as 'jpeg' file without compressing in lossy.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗化ㄋ丶相逢 2024-12-14 19:30:23

JAI 包提供了保存“无损 JPEG”格式的能力。将压缩类型设置为 JPEG-LS 或 JPEG-LOSSLESS 取决于您想要什么变体。

不过,我不确定您是否真的想要无损 JPEG。它是一种独立的格式,与普通的 JPEG 格式没有太大关系。一般来说,它并没有得到很好的支持;对于无损图像存储,您通常最好使用 PNG 之类的东西。

如果您需要进行无损图像转码(即在不干扰 DCT 矩阵边界的情况下可以执行的一组裁剪和旋转操作),您通常会使用 jpegtran 命令来完成,因为没有据我所知,目前 Java 绑定到了 IJG 库。

预计到达时间:

你知道如何使用 JAI 做到这一点吗?

我自己没有尝试过(下面的代码未经测试),但它应该是对 setCompressionType 的直接调用。 (当然,Java 中的“直接”仍然意味着通过迷宫般的曲折小对象来设置其他地方的简单开关:)

ImageWriter writer= (ImageWriter) ImageIO.getImageWritersByFormatName("jpeg").next();
ImageWriteParam param= writer.getDefaultWriteParam();
param.setCompressionMode(param.MODE_EXPLICIT);
param.setCompressionType("JPEG-LS");
writer.setOutput(ImageIO.createImageOutputStream(new File(path)));
writer.write(null, new IIOImage(image, null, null), param);

JPEG-LS 是较新的无损 JPEG 标准。它的压缩程度超过了原始的 JPEG-LOSSLESS 标准,但支持更差。大多数支持 JPEG 的应用程序将无法对它们执行任何操作。

The JAI package offers the ability to save “lossless JPEG” formats. Set compresion type to JPEG-LS or JPEG-LOSSLESS depending on what variant you want.

I'm not sure you really want lossless-JPEG though. It's a separate format that's not really much to do with the normal JPEG format. It is not, in general, very well-supported; for lossless image storage you are typically better off with something like PNG.

If you need to do lossless image transcoding (ie the set of cropping and rotation operations you can do without disturbing the boundaries of the DCT matrices), you would generally do it with the jpegtran command, as there's not currently a Java binding to the IJG library as far as I know.

ETA:

do you know how to do it with JAI.

I've not tried it myself (code below is untested), but it should be a straightforward call to setCompressionType. (Of course ‘straightforward’ in Java still means negotiating a maze of twisty little objects to set what would be a simple switch anywhere else:)

ImageWriter writer= (ImageWriter) ImageIO.getImageWritersByFormatName("jpeg").next();
ImageWriteParam param= writer.getDefaultWriteParam();
param.setCompressionMode(param.MODE_EXPLICIT);
param.setCompressionType("JPEG-LS");
writer.setOutput(ImageIO.createImageOutputStream(new File(path)));
writer.write(null, new IIOImage(image, null, null), param);

JPEG-LS is the newer lossless JPEG standard. It compresses more than the original JPEG-LOSSLESS standard but support is even worse. Most applications that support JPEG will not be able to do anything with these.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文