Read keywords in xmp from png with Java

2019-09-05 07:23发布

问题:

I am trying to get keywords added to a PNG image file in Photoshop using Java.

I tried this method using the imageio in Java:

http://johnbokma.com/java/obtaining-image-metadata.html

However, all I get is the following:

Format name: javax_imageio_png_1.0
<javax_imageio_png_1.0>
    <IHDR width="128" height="128" bitDepth="8" colorType="RGBAlpha" compressionMethod="deflate" filterMethod="adaptive" interlaceMethod="none"/>
    <cHRM whitePointX="31269" whitePointY="32899" redX="63999" redY="33001" greenX="30000" greenY="60000" blueX="15000" blueY="5999"/>
    <iCCP profileName="Photoshop ICC profile" compressionMethod="deflate"/>
    <pHYs pixelsPerUnitXAxis="2835" pixelsPerUnitYAxis="2835" unitSpecifier="meter"/>
</javax_imageio_png_1.0>
Format name: javax_imageio_1.0
<javax_imageio_1.0>
    <Chroma>
        <ColorSpaceType name="RGB"/>
        <NumChannels value="4"/>
        <BlackIsZero value="TRUE"/>
    </Chroma>
    <Compression>
        <CompressionTypeName value="deflate"/>
        <Lossless value="TRUE"/>
        <NumProgressiveScans value="1"/>
    </Compression>
    <Data>
        <PlanarConfiguration value="PixelInterleaved"/>
        <SampleFormat value="UnsignedIntegral"/>
        <BitsPerSample value="8 8 8 8"/>
    </Data>
    <Dimension>
        <PixelAspectRatio value="1.0"/>
        <ImageOrientation value="Normal"/>
        <HorizontalPixelSize value="0.35273367"/>
        <VerticalPixelSize value="0.35273367"/>
    </Dimension>
    <Transparency>
        <Alpha value="nonpremultipled"/>
    </Transparency>
</javax_imageio_1.0>

Nothing about the keywords added...

Am I not doing it right? Is this library not getting the XMP metadata?

EDIT:

Although I would have preferred to know if the standard library could read the metadata, I gave the pngj library a try. But that gave me the following error:

Exception in thread "main" ar.com.hjg.pngj.PngjExceptionInternal: this should not happen
    at ar.com.hjg.pngj.PngIDatChunkInputStream.read(PngIDatChunkInputStream.java:117)
    at ar.com.hjg.pngj.PngReader.readSkippingAllRows(PngReader.java:777)
    at se.expertinfo.ditaplugin.ImageMetadata.showChunks(ImageMetadata.java:105)
    at se.expertinfo.ditaplugin.DitaPlugin.printImageMetadata(DitaPlugin.java:33)
    at se.expertinfo.ditaplugin.DitaPlugin.main(DitaPlugin.java:24)
Java Result: 1

If I comment out the pngr.readSkippingAllRows(); (which may for all I know make the method useless?) I get the following result:

filename=architecture3.png ImageInfo [cols=128, rows=128, bitDepth=8, channels=4, bitspPixel=32, bytesPixel=4, bytesPerRow=512, samplesPerRow=512, samplesPerRowP=512, alpha=true, greyscale=false, indexed=false, packed=false]
ChunkList: read: 5
 Read:
chunk id= IHDR (len=13 offset=8) c=PngChunkIHDR G=0
chunk id= pHYs (len=9 offset=33) c=PngChunkPHYS G=1
chunk id= iCCP (len=2639 offset=54) c=PngChunkICCP G=1
chunk id= cHRM (len=32 offset=2705) c=PngChunkCHRM G=1
chunk id= IDAT (len=25329 offset=2749) c=PngChunkIDAT G=4

BUILD SUCCESSFUL (total time: 0 seconds)

I.e. nothing about the keywords in there...

BTW: Here is the metadata set in Photoshop. The same metadata appears under the IPTC tab...

EDIT 2: In response to leonbloy:

Added Title and Author, but still no output (Getting the IEND though):

filename=architecture3.png ImageInfo [cols=128, rows=128, bitDepth=8, channels=4, bitspPixel=32, bytesPixel=4, bytesPerRow=512, samplesPerRow=512, samplesPerRowP=512, alpha=true, greyscale=false, indexed=false, packed=false] ChunkList: read: 6 Read: chunk id= IHDR (len=13 offset=8) c=PngChunkIHDR G=0 chunk id= pHYs (len=9 offset=33) c=PngChunkPHYS G=1 chunk id= iCCP (len=2639 offset=54) c=PngChunkICCP G=1 chunk id= cHRM (len=32 offset=2705) c=PngChunkCHRM G=1 chunk id= IDAT (len=25329 offset=2749) c=PngChunkIDAT G=4 chunk id= IEND (len=0 offset=28090) c=PngChunkIEND G=6

I can see the added metadata in Adobe Bridge...

EDIT 3:

Well, I have no explanation for it, but all of a sudden the javax.imageio library works fine! I'm getting all the metadata as expected, and I don't even know of doing any changes to it.

I'm just missing one thing - I would have liked to be able to get metadata in key/value pairs, like Product=Apple, but I cannot seem to find any such way of adding metadata. Is this available in some way, and how would I get to it?