Alexey
Alexey

Reputation: 7247

How to get tags from image metadata in java?

I have an image in the folder. Manually I added tags to the image . I want to load the image and display its tags.

In order to get image metadata I used tutorial http://johnbokma.com/java/obtaining-image-metadata.html

It displays the xml, but this xml doesn't contain tags. Example of xml:

Format name: javax_imageio_jpeg_image_1.0
<javax_imageio_jpeg_image_1.0>
    <JPEGvariety/>
    <markerSequence>
        <unknown MarkerTag="225"/>
        <unknown MarkerTag="225"/>
        <dqt>
            <dqtable elementPrecision="0" qtableId="0"/>
        </dqt>
        <dqt>
            <dqtable elementPrecision="0" qtableId="1"/>
        </dqt>
        <sof process="0" samplePrecision="8" numLines="3000" samplesPerLine="4000" numFrameComponents="3">
            <componentSpec componentId="1" HsamplingFactor="2" VsamplingFactor="1" QtableSelector="0"/>
            <componentSpec componentId="2" HsamplingFactor="1" VsamplingFactor="1" QtableSelector="1"/>
            <componentSpec componentId="3" HsamplingFactor="1" VsamplingFactor="1" QtableSelector="1"/>
        </sof>
        <dht>
            <dhtable class="0" htableId="0"/>
        </dht>
        <dht>
            <dhtable class="1" htableId="0"/>
        </dht>
        <dht>
            <dhtable class="0" htableId="1"/>
        </dht>
        <dht>
            <dhtable class="1" htableId="1"/>
        </dht>
        <sos numScanComponents="3" startSpectralSelection="0" endSpectralSelection="63" approxHigh="0" approxLow="0">
            <scanComponentSpec componentSelector="1" dcHuffTable="0" acHuffTable="0"/>
            <scanComponentSpec componentSelector="2" dcHuffTable="1" acHuffTable="1"/>
            <scanComponentSpec componentSelector="3" dcHuffTable="1" acHuffTable="1"/>
        </sos>
    </markerSequence>
</javax_imageio_jpeg_image_1.0>
Format name: javax_imageio_1.0
<javax_imageio_1.0>
    <Chroma>
        <ColorSpaceType name="YCbCr"/>
        <NumChannels value="3"/>
    </Chroma>
    <Compression>
        <CompressionTypeName value="JPEG"/>
        <Lossless value="FALSE"/>
        <NumProgressiveScans value="1"/>
    </Compression>
    <Dimension>
        <ImageOrientation value="normal"/>
    </Dimension>
</javax_imageio_1.0>

Upvotes: 1

Views: 2845

Answers (1)

Martin Wilson
Martin Wilson

Reputation: 3386

What software did you use to add 'tags' to the image? Let's assume it was Windows Explorer. In that case, the 'tags' will probably be stored in the image in both of these fields:
XMP:Subject
XMP:LastKeywordXMP (specifically, the element MicrosoftPhoto:LastKeywordXMP)

I don't think the getImageMetadata method of the readers you get with ImageIO.getImageReaders() by default will return XMP data. (This is what the code you reference is using).

I would suggest finding an open source library, or code, that does what you need. There seem to be quite a few around, for example http://commons.apache.org/imaging/ or http://java2s.com/Open-Source/Java/Image/metadata-extractor/com/drew/metadata/xmp/XmpReader.java.htm (Note: I haven't tried using this).

If all else fails you could use the excellent ExifTool

This is a command line tool but simple to use. For example, to extract your tags: exiftool -XMP:Subject test.jpg

Upvotes: 2

Related Questions