简体   繁体   English

使用Twelvemonkeys ImageIO读取TIFF ICC配置文件

[英]Read TIFF ICC profile using Twelvemonkeys ImageIO

I need to extract the embedded ICC profile from TIFF files. 我需要从TIFF文件中提取嵌入式ICC配置文件。 I can read the IIOMetadata and my IDE shows the ifd field containing the ICC profile (tag ID 34675). 我可以阅读IIOMetadata和我的IDE显示ifd包含ICC配置文件(标签ID 34675)字段。 But how can I read it to a ICC_Profile object? 但是,如何将其读取到ICC_Profile对象?

ImageInputStream input = ImageIO.createImageInputStream(file);

try {
    ImageReader reader = ImageIO.getImageReaders(input).next();
    if (reader == null) {
        throw new IllegalArgumentException("No image reader for file: " + file);
    }

    try {
        reader.setInput(input);
        IIOMetadata metadata = reader.getImageMetadata(0);
        // metadata contains a field "ifd" containing the ICC profile
        // How to extract it?

    } finally {
        reader.dispose();
    }

} finally {
    input.close();
}

You can use the function getProfile() of the ICCProfile class. 您可以使用ICCProfile类的函数getProfile()

Usage: 用法:

int profileId = ...; 
ICCProfile iccp = new ICCProfile(profileId, input);
ICC_Profile icc_p = iccp.getProfile();

In accordance to the code at google result #1 for twelvemonkeys icc_profile . 根据google结果的代码#1,十二只猴子icc_profile

Found a solution. 找到了解决方案。 For this Twelvemonkeys package imageio-metadata is needed in version 3.4. 对于此Twelvemonkeys软件包,版本3.4中需要imageio-metadata Older version does not contain TIFFEntry class. 旧版本不包含TIFFEntry类。

 /**
 * Extract ICC profile from an image file.
 *
 * @param file image file
 * @return ICC profile
 * @throws IOException on file errors
 */
protected ICC_Profile extractICCProfile(File file) throws IOException {

    ICC_Profile profile;

    try (ImageInputStream input = ImageIO.createImageInputStream(file)) {
        ImageReader reader = ImageIO.getImageReaders(input).next();
        if (reader == null) {
            throw new IllegalArgumentException("No image reader for file: " + file);
        }

        try {
            reader.setInput(input);
            TIFFImageMetadata metadata = (TIFFImageMetadata) reader.getImageMetadata(0);
            TIFFEntry entry = (TIFFEntry) metadata.getTIFFField(TIFF.TAG_ICC_PROFILE);
            byte[] iccBytes = (byte[]) entry.getValue();
            profile = ICC_Profile.getInstance(iccBytes);
        } finally {
            reader.dispose();
        }
    }

    return profile;
}

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

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