以 jpeg 格式保存色彩空间

发布于 2024-10-31 05:22:02 字数 278 浏览 5 评论 0原文

我有一个 servlet 来转换和缓存较小版本的照片。它是使用 java.awt.image + javax.imageio 和第三方重采样过滤器实现的。原件均使用 sRGB 颜色配置文件上传。当我重新采样并再次保存它们时,它们仍然处于 sRGB 格式,但这不会记录在保存的文件中。

我如何确保此信息已保存在文件中?

如果您想知道它有什么不同,没有配置文件的图像在我的屏幕(Safari + OSX + 校准屏幕)上比具有正确的 sRGB 配置文件时更加饱和。我也确信这是丢失的配置文件信息,而不是重采样算法。

I have a servlet to convert and cache smaller versions of photographs. It is implemented using java.awt.image + javax.imageio and a third party resample filter. The originals are all uploaded with an sRGB color profile. When I resample them and save them again they still are in sRGB however this is not recorded in the saved file.

How can I make sure this information is saved in the file?

In case you wondered it makes a difference, images without a profile are much more saturated on my screen (Safari + OSX + Calibrated screen) then when they have the correct sRGB profile. Also I'm sure it's the missing profile information and not the resampling algorithm.

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

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

发布评论

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

评论(1

止于盛夏 2024-11-07 05:22:02

事实证明,包含一个 EXIF 标签 ColorSpace=1 就足够了,它告诉它应该作为 sRGB 进行处理。使用 Apache Commons Sanselan 成功做到了这一点。不幸的是,这个库并不完整,因此它只能用于在文件创建后修改 EXIF。

相关代码,基于Sanselan示例:

public void addExifMetadata(File jpegImageFile, File dst)
            throws IOException, ImageReadException, ImageWriteException {
        OutputStream os = null;
        try {
            TiffOutputSet outputSet = new TiffOutputSet();

            TiffOutputField colorspace = TiffOutputField.create(
                        TiffConstants.EXIF_TAG_COLOR_SPACE, outputSet.byteOrder, new Integer(1));
            TiffOutputDirectory exifDirectory = outputSet.getOrCreateExifDirectory();
            exifDirectory.add(colorspace);

            os = new FileOutputStream(dst);
            os = new BufferedOutputStream(os);
            new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os, outputSet);

            os.close();
            os = null;
        } finally  {
            if (os != null)
                try {
                    os.close();
                } catch (IOException e) {

                }
        }
    }

Turns out it is enough to include an EXIF tag ColorSpace=1 that tells it should be processed as sRGB. Succeeded into doing this using Apache Commons Sanselan. This library is unfortunatly not complete so it can only be used to modify the EXIF after the file has been created.

Relevant code, based on Sanselan example:

public void addExifMetadata(File jpegImageFile, File dst)
            throws IOException, ImageReadException, ImageWriteException {
        OutputStream os = null;
        try {
            TiffOutputSet outputSet = new TiffOutputSet();

            TiffOutputField colorspace = TiffOutputField.create(
                        TiffConstants.EXIF_TAG_COLOR_SPACE, outputSet.byteOrder, new Integer(1));
            TiffOutputDirectory exifDirectory = outputSet.getOrCreateExifDirectory();
            exifDirectory.add(colorspace);

            os = new FileOutputStream(dst);
            os = new BufferedOutputStream(os);
            new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os, outputSet);

            os.close();
            os = null;
        } finally  {
            if (os != null)
                try {
                    os.close();
                } catch (IOException e) {

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