简体   繁体   English

元数据提取器Java不提取exif或iptc

[英]Metadata extractor java doesn't extract exif or iptc

I am trying to get the exif of a jpeg image but it doesn't work. 我正在尝试获取jpeg图像的exif,但是它不起作用。 First I read my image using BufferedImage and I convert it to a file and then I apply the same code as here: https://code.google.com/p/metadata-extractor/source/browse/Samples/com/drew/metadata/SampleUsage.java?name=2.5.1 . 首先,我使用BufferedImage读取图像并将其转换为文件,然后应用与此处相同的代码: https : //code.google.com/p/metadata-extractor/source/browse/Samples/com/drew/ metadata / SampleUsage.java?name = 2.5.1 What am I doing wrong? 我究竟做错了什么? Why does the JpegSegmentReader.SEGMENT_APP1 returns null? 为什么JpegSegmentReader.SEGMENT_APP1返回null?

    File outfile = new File("image.jpg");
    ImageIO.write(imagine, "jpg", outfile);
    try{
            JpegSegmentReader segmentReader = new JpegSegmentReader(outfile);
            byte[] exifSegment = segmentReader.readSegment(JpegSegmentReader.SEGMENT_APP1);
            System.out.println(Arrays.toString(segmentReader.readSegment(JpegSegmentReader.SEGMENT_APP1)));
            byte[] iptcSegment = segmentReader.readSegment(JpegSegmentReader.SEGMENT_APPD);
            Metadata metadata = new Metadata();
            if (exifSegment != null)
                new ExifReader().extract(new ByteArrayReader(exifSegment), metadata);
            if (iptcSegment != null)
                new IptcReader().extract(new ByteArrayReader(iptcSegment), metadata);
            printImageTags(metadata);
        }catch (JpegProcessingException e) {
            System.err.println("error 3a: " + e);
        }

ImageIO.write() doesn't write Exif metadata* (APP1/Exif). ImageIO.write()不写入Exif元数据*(APP1 / Exif)。 It only stores JFIF (for more information on Exif/JFIF, see JPEG on WikiPedia) in the APP0 segment. 它仅在APP0段中存储JFIF(有关Exif / JFIF的更多信息,请参阅WikiPedia上的JPEG )。 Because of this, there will never be an APP1 segment in your code. 因此,您的代码中永远不会有APP1段。

There's also no Exif metadata available in the BufferedImage or RenderedImage that you are writing, as objects of these types only contain pixel data. 您正在编写的BufferedImageRenderedImage中也没有Exif元数据,因为这些类型的对象仅包含像素数据。

If you want to extract Exif metadata, you need to find a reference to the original file you read the image ( imagine ) from, and read from there. 如果要提取Exif元数据,则需要找到对原始文件的引用,该原始文件是您从中读取图像( imagine )并从那里读取的。

*) ImageIO and the standard JPEGImageWriter can write Exif metadata, but only if you pass the Exif metadata to the writer, using the IIOMetadata API. *) ImageIO和标准JPEGImageWriter 可以写入Exif元数据,但JPEGImageWriter是您必须使用IIOMetadata API将Exif元数据传递给写入器。 But I don't think this is relevant for your use case. 但是我认为这与您的用例无关。

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

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