gpt4 book ai didi

java - 如何使用 TwelveMonkey 的 ExifWriter 类将 Exif 写入 JPEG

转载 作者:行者123 更新时间:2023-11-30 07:31:38 27 4
gpt4 key购买 nike

我正在使用TwelveMonkey's lib 从 jpeg 读取 Exif 数据,例如:

       try (ImageInputStream stream = ImageIO.createImageInputStream(input)) {
List<JPEGSegment> exifSegment = JPEGSegmentUtil.readSegments(stream, JPEG.APP1, "Exif");
InputStream exifData = exifSegment.get(0).data();
exifData.read(); // Skip 0-pad for Exif in JFIF
try (ImageInputStream exifStream = ImageIO.createImageInputStream(exifData)) {
return new EXIFReader().read(exifStream);
}
}

因此我有一个CompoundDirectory与一堆Entry元素。但我该如何使用 ExifWriter到 jpeg。使用它写入输出流只会损坏 jpeg(图像查看器认为这是损坏的 tiff)。

更新:我想要实现的是将 jpeg 读取到 BufferedImage ,还读取 exif 数据,对其进行缩放,然后将其压缩为 jpeg,再次保留 exif 数据(即将之前读取的数据写入横向扩展的 jpeg)。为此,我目前使用一些详细版本的 ImageIO方法。以下是当前执行此操作的基本代码:https://gist.github.com/patrickfav/5a51566f31c472d02884 (exif 阅读器似乎可以工作,作家当然不行)

最佳答案

TwelveMonkeys Exif 包(EXIFReader/EXIFWriter)是相当低级的,其设计目的是供 ImageReader/ImageWriter 实现高效使用。它仍然可以完全用作通用元数据包,但可能需要您做更多的工作,并且需要了解用于承载 Exif 数据的容器格式的一些知识。

要将 Exif 数据写入 JPEG,您需要编写 APP1/Exif 段作为 normal JIF structure 的一部分。 EXIFWriter 将写入您应仅放入此段内部的数据。其他一切都必须由您提供。

有多种方法可以实现这一目标。您可以在二进制/流级别上使用 JPEG,也可以修改图像数据并使用 ImageIO 元数据来写入 Exif。我将概述使用 IIOMetadata 类编写 Exif 的过程。

来自JPEG Metadata Format Specification and Usage Notes :

(Note that an application wishing to interpret Exif metadata given a metadata tree structure in the javax_imageio_jpeg_image_1.0 format must check for an unknown marker segment with a tag indicating an APP1 marker and containing data identifying it as an Exif marker segment. Then it may use application-specific code to interpret the data in the marker segment. If such an application were to encounter a metadata tree formatted according to a future version of the JPEG metadata format, the Exif marker segment might not be unknown in that format - it might be structured as a child node of the JPEGvariety node. Thus, it is important for an application to specify which version to use by passing the string identifying the version to the method/constructor used to obtain an IIOMetadata object.)

EXIFReader 将是您的“解释数据的应用程序特定代码”。以同样的方式,您应该能够插入带有 APP1unknown 标记段节点(通常为 0xFFE1,但在ImageIO 元数据,仅使用最后一个八位字节的十进制表示形式作为字符串,因此该值为“225”)。使用 ByteArrayOutputStream 并将 Exif 数据写入其中,并将生成的字节数组作为“用户对象”传递到元数据节点。

IIOMetadata metadata = ...; 

IIOMetadataNode root = new IIOMetadataNode("javax_imageio_jpeg_image_1.0");
IIOMetadataNode markerSequence = new IIOMetadataNode("markerSequence");
root.appendChild(markerSequence);

Collection<Entry> entries = ...; // Your original Exif entries

// Write the full Exif segment data
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
// APPn segments are prepended with a 0-terminated ASCII identifer
bytes.write("Exif".getBytes(StandardCharsets.US_ASCII));
bytes.write(new byte[2]); // Exif uses 0-termination + 0 pad for some reason
// Write the Exif data (note that Exif is a TIFF structure)
new TIFFWriter().write(entries, new MemoryCacheImageOutputStream(bytes));

// Wrap it all in a meta data node
IIOMetadataNode exif = new IIOMetadataNode("unknown");
exif.setAttribute("MarkerTag", String.valueOf(0xE1)); // APP1 or "225"
exif.setUserObject(bytes.toByteArray());

// Append Exif node
markerSequence.appendChild(exif);

// Merge with original data
metadata.mergeTree("javax_imageio_jpeg_image_1.0", root);

如果您的原始元数据已包含 Exif 段,则最好使用:

// Start out with the original tree
IIOMetadataNode root = (IIOMetadataNode) metadata.getAsTree("javax_imageio_jpeg_image_1.0");
IIOMetadataNode markerSequence = (IIOMetadataNode) root.getElementsByTagName("markerSequence").item(0); // Should always a single markerSequence

...

// Remove any existing Exif, or make sure you update the node,
// to avoid having two Exif nodes
// Logic for creating the node as above

...

// Replace the tree, instead of merging
metadata.setFromTree("javax_imageio_jpeg_image_1.0", root);

我不太喜欢 ImageIO 元数据 API,因为代码极其冗长,但我希望您了解如何实现您的目标。 :-)

<小时/>

PS:图像查看者认为您的图像是 TIFF 的原因是 Exif 数据是 TIFF 结构。如果您仅将 JPEG 中的 Exif 数据写入其他空文件,则您将得到一个 TIFF 文件,其中 IFD0 中没有图像数据(IFD1 中可能有缩略图) .

关于java - 如何使用 TwelveMonkey 的 ExifWriter 类将 Exif 写入 JPEG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36029295/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com