在 jpeg 中保存色彩空间 [英] Saving colorspace in jpeg

查看:40
本文介绍了在 jpeg 中保存色彩空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 servlet 来转换和缓存较小版本的照片.它是使用 java.awt.image + javax.imageio 和第三方重采样过滤器实现的.原件均使用 sRGB 颜色配置文件上传.当我对它们重新采样并再次保存它们时,它们仍然是 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?

如果您想知道这有什么不同,那么没有配置文件的图像在我的屏幕(Safari + OSX + 校准屏幕)上会更加饱和,然后当它们具有正确的 sRGB 配置文件时.另外我确定这是缺少的配置文件信息而不是重采样算法.

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.

推荐答案

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

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.

相关代码,基于Sanselan示例:

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) {

                }
        }
    }

这篇关于在 jpeg 中保存色彩空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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