Search code examples
javaandroidmetadata-extractor

java metadata-extractor tag description


I am using the Java library Metadata-extractor and cannot extract the tag description correctly using the getUserCommentDescription method code below, although the tag.getDescription does work:

String exif = "File: " + file;
File jpgFile = new File(file);
Metadata metadata = ImageMetadataReader.readMetadata(jpgFile);

for (Directory directory : metadata.getDirectories()) {
    String directoryName = directory.getName();
    for (Tag tag : directory.getTags()) {
       String tagName = tag.getTagName();
       String description = tag.getDescription();
       if (tagName.toLowerCase().contains("comment")) {
          Log.d("DEBUG", description);
       }
       exif += "\n " + tagName + ": " + description;  //Returns the    correct values.
       Log.d("DEBUG", directoryName + " " + tagName + " " + description);
   }
   if (directoryName.equals("Exif IFD0")) {
      // create a descriptor
      ExifSubIFDDirectory exifDirectory =   metadata.getDirectory(ExifSubIFDDirectory.class);
      ExifSubIFDDescriptor descriptor = new     ExifSubIFDDescriptor(exifDirectory);
      Log.d("DEBUG","Comments: " +    descriptor.getUserCommentDescription()); //Always null.
  }

Am I missing something here?


Solution

  • You are checking for the directory name Exif IFD0 and then accessing the ExifSubIFDDirectory.

    Try this code outside the loop:

    Metadata metadata = ImageMetadataReader.readMetadata(jpgFile);
    ExifSubIFDDirectory exifDirectory = metadata.getDirectory(ExifSubIFDDirectory.class);
    ExifSubIFDDescriptor descriptor = new ExifSubIFDDescriptor(exifDirectory);
    String comment = descriptor.getUserCommentDescription();
    

    If this returns null then it may be an encoding issue or bug. If you run this code:

    byte[] commentBytes =
        exifDirectory.getByteArray(ExifSubIFDDirectory.TAG_USER_COMMENT);
    

    Do you have bytes in the array?

    If so then please open an issue in the issue tracker and include a sample image that can be used to reproduce the problem. You must authorise any image you provide for use in the public domain.