使用Java从tif文件中提取IPTC/EXIF数据

发布于 2024-08-14 00:39:50 字数 326 浏览 9 评论 0原文

我正在开发的系统具有使用 com.drew.metadata 包从 JPEG 文件中提取元数据的功能。 http://www.drewnoakes.com/code/exif/ 但是仅限于JPEG 文件,现在客户询问如何从 TIF 以及可能的其他图像格式中提取 IPTC。

有谁知道与 Drew Noakes 类似的 API,可以从 TIF 中提取 IPTC?

理想情况下,这将是一种纯 Java 方法,例如 com.drew.metadata 方法。

The system I'm working on has a feature to extract metadata from JPEG files using the com.drew.metadata package. http://www.drewnoakes.com/code/exif/ However that is limited to JPEG files, and now a customer has asked about extracting IPTC from TIF, and possibly other image formats.

Does anyone know about similar APIs to Drew Noakes one, that can extract IPTC from TIF?

Ideally this would be a pure Java approach like the com.drew.metadata one.

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

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

发布评论

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

评论(4

流年已逝 2024-08-21 00:39:50

这是一个老问题了。现在,我的元数据提取器库支持 TIFF 文件,以及 JPEG、WebP、PSD、PNG、GIF、BMP、ICO、PCX 和许多相机原始格式。

该项目最近转移到 GitHub:

https://github.com/drewnoakes/metadata-extractor

并且可通过 Maven 获取:

http://search.maven.org/#search%7Cga% 7C1%7C德鲁诺克斯

This is an old question. Nowadays my metadata-extractor library supports TIFF files, as well as JPEG, WebP, PSD, PNG, GIF, BMP, ICO, PCX and many camera raw formats.

The project recently moved to GitHub:

https://github.com/drewnoakes/metadata-extractor

And is available via Maven:

http://search.maven.org/#search%7Cga%7C1%7Cdrewnoakes

别再吹冷风 2024-08-21 00:39:50

我最近花了一些时间编写 icafe Java 图像库的元数据操作部分,并使其能够插入和提取元数据类型,如 EXIF、IPTC、PHOTOSHOP、ICC_Profile、缩略图等。有些功能比其他功能更好,但它们都相对工作得很好。所有元数据读取都有一个通用接口,如下所示:

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Iterator;

import com.icafe4j.image.meta.Metadata;
import com.icafe4j.image.meta.MetadataEntry;
import com.icafe4j.image.meta.MetadataType;
import com.icafe4j.image.meta.iptc.IPTC;

public class ExtractIPTC {

    public static void main(String[] args) throws IOException {
        Map<MetadataType, Metadata> metadataMap = Metadata.readMetadata(args[0]);
        IPTC iptc = (IPTC)metadataMap.get(MetadataType.IPTC);

        if(iptc != null) {
            Iterator<MetadataEntry> iterator = iptc.iterator();

            while(iterator.hasNext()) {
                MetadataEntry item = iterator.next();
                printMetadata(item, "", "     ");
            }
        }   
    }
    private void printMetadata(MetadataEntry entry, String indent, String increment) {
        logger.info(indent + entry.getKey() (StringUtils.isNullOrEmpty(entry.getValue())? "" : ": " + entry.getValue()));
        if(entry.isMetadataEntryGroup()) {
             indent += increment;
             Collection<MetadataEntry> entries = entry.getMetadataEntries();
             for(MetadataEntry e : entries) {
                printMetadata(e, indent, increment);
             }          
        }
    }   
}

如果我们从项目的“images”目录中传递图像“iptc.tif”作为参数,我们将得到以下信息:

Record number 2: Application Record
Dataset name: Keywords
Dataset tag: 25[0x0019]
Dataset size: 6
Dataset value: Bayern
Record number 2: Application Record
Dataset name: Keywords
Dataset tag: 25[0x0019]
Dataset size: 11
Dataset value: Deckelstein
Record number 2: Application Record
Dataset name: Keywords
Dataset tag: 25[0x0019]
Dataset size: 7
Dataset value: Germany
Record number 2: Application Record
Dataset name: Keywords
Dataset tag: 25[0x0019]
Dataset size: 10
Dataset value: Nittendorf

上面的代码适用于 JPEG 和TIFF 类似。它会自动检测图像类型并委托给相应的代码来完成工作。

注意:TIFF 文件中可能有多个位置包含 IPTC 数据。一个是 RichTiffIPTC 标签,另一个隐藏在 Photoshop 标签内。目前icafe只保留一份IPTC数据。如果带有 IPTC 数据的 Photoshop 标签和 RichTiffIPTC 标签同时存在,它将保留 RichTiffIPTC 数据。否则,无论存在哪个标签,它都会保留该标签的 IPTC 数据。保存两个地方的数据没有问题。当前的实现使用将元数据类型键映射到唯一元数据的映射。因此它只保留一个唯一的元数据实例。

更新: icafe 现在可以合并来自 RichTiffIPTC 和 Photoshop IRB 的 IPTC 数据删除重复项。

更新2: ICAFE 中所有元数据类型的基类 - Metadata 现在实现了 Iterable 接口,因此用户现在可以迭代 MetadataEntry 的集合。 MetadataEntry 本身是使用复合模式创建的,因此 MetadataEntry 可以包含其他 MetadataEntry 的集合。每个 MetadataEntry 包含一个键和一个值对。这种设计允许元数据条目的树结构遍历。

I spent some time recently coding the metadata manipulation part of icafe Java image library and make it able to insert and extract metadata types like EXIF, IPTC, PHOTOSHOP, ICC_Profile, thumbnail etc. Some functions are better than others, but they all relatively work fine. There is a common interface for all the metadata reading shown below:

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Iterator;

import com.icafe4j.image.meta.Metadata;
import com.icafe4j.image.meta.MetadataEntry;
import com.icafe4j.image.meta.MetadataType;
import com.icafe4j.image.meta.iptc.IPTC;

public class ExtractIPTC {

    public static void main(String[] args) throws IOException {
        Map<MetadataType, Metadata> metadataMap = Metadata.readMetadata(args[0]);
        IPTC iptc = (IPTC)metadataMap.get(MetadataType.IPTC);

        if(iptc != null) {
            Iterator<MetadataEntry> iterator = iptc.iterator();

            while(iterator.hasNext()) {
                MetadataEntry item = iterator.next();
                printMetadata(item, "", "     ");
            }
        }   
    }
    private void printMetadata(MetadataEntry entry, String indent, String increment) {
        logger.info(indent + entry.getKey() (StringUtils.isNullOrEmpty(entry.getValue())? "" : ": " + entry.getValue()));
        if(entry.isMetadataEntryGroup()) {
             indent += increment;
             Collection<MetadataEntry> entries = entry.getMetadataEntries();
             for(MetadataEntry e : entries) {
                printMetadata(e, indent, increment);
             }          
        }
    }   
}

If we pass the image "iptc.tif" from the "images" directory of the project as the argument, we will get the following information:

Record number 2: Application Record
Dataset name: Keywords
Dataset tag: 25[0x0019]
Dataset size: 6
Dataset value: Bayern
Record number 2: Application Record
Dataset name: Keywords
Dataset tag: 25[0x0019]
Dataset size: 11
Dataset value: Deckelstein
Record number 2: Application Record
Dataset name: Keywords
Dataset tag: 25[0x0019]
Dataset size: 7
Dataset value: Germany
Record number 2: Application Record
Dataset name: Keywords
Dataset tag: 25[0x0019]
Dataset size: 10
Dataset value: Nittendorf

The above code works for JPEG and TIFF alike. It automatically detects the image type and delegate to corresponding code to do the work.

NOTE: there could be more than one places in a TIFF file which contains IPTC data. One is RichTiffIPTC tag, the other is buried inside a Photoshop tag. Currently, icafe only keeps one IPTC data. If both Photoshop tag with IPTC data and a RichTiffIPTC tag exist, it will keep the RichTiffIPTC data. Otherwise, whichever tag exists, it will keep IPTC data from that tag. There is no problem keeping data from both places. Current implementation using a map mapping a metadata type key to a unique metadata. So it only keeps one unique metadata instance.

Update: icafe can now combine IPTC data from both RichTiffIPTC and Photoshop IRB and remove duplicates.

Update2: The base class of all the metadata types in ICAFE - Metadata now implements Iterable interface so the user can now iterate through a collection of MetadataEntry. MetadataEntry itself is created using composite pattern so a MetadataEntry can contain a collection of other MetadataEntry. Each MetadataEntry contains a key and a value pair. This design allow for a tree structure traversal of the metadata entries.

闻呓 2024-08-21 00:39:50

这里有一个使用 imageio lib 访问 IPTC 的好例子

http://www.barregren.se/blog/how-read-exif-and-iptc-java-image-io-api

不幸的是,您'仍然需要自己处理一些工作。

There's a good example here of using the imageio lib to access IPTC here

http://www.barregren.se/blog/how-read-exif-and-iptc-java-image-i-o-api

Unfortunately, you'll still have to handle some of the work yourself.

洒一地阳光 2024-08-21 00:39:50

如果您找不到纯 Java 实现,您可以考虑使用 ImageMagick 的 Java 绑定 (JMagick )。这将允许多种不同的可能的输出格式。

If you cannot find a pure Java implementation, you could consider using the Java bindings to ImageMagick (JMagick). That would allow for a plethora of different possible output formats.

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