Extracting metadata from PNG image

2019-07-21 08:47发布

问题:

I'm trying to extract metadata from a PNG image format. I'm using this library? http://code.google.com/p/metadata-extractor/

Even though it claims that PNG format is supported I get an error File format is not supported when I try it with a PNG image. From the source (in method readMetadata also it looks like that it doesn't support PNG format: http://code.google.com/p/metadata-extractor/source/browse/Source/com/drew/imaging/ImageMetadataReader.java?r=1aae00f3fe64388cd14401b2593b580677980884

I've also given this piece of code a try as well but it also doesn't extract the metadata on the PNG.

BTW, I'm adding metadata on PNG with imagemagick like this:

mogrify -comment "Test" Demo/myimage.png

Has anyone used this library for PNG format or are there other ways to extract metadata from PNG image?

回答1:

You can try PNGJ (I'm the developer)

See eg here an example to dump all chunks. If you want to read a particular text chunk (recall that in PNG each textual metadata has a key and a value), you could write

 pngr.getMetadata().getTxtForKey("mykey")

A useful little Windows program to peek inside PNG chunk structure is TweakPNG

Update: If you want to check all textual chunks (bear in mind that there are three types with some differences, but...)

PngReader pngr = FileHelper.createPngReader(new File(file));
pngr.readSkippingAllRows();
for (PngChunk c : pngr.getChunksList().getChunks()) {
      if (!ChunkHelper.isText(c))   continue;
      PngChunkTextVar ct = (PngChunkTextVar) c;
      String key = ct.getKey();
      String val = ct.getVal();
      // ... 
}

Bear also in mind that textual chunks with repeated keys are allowed.