Java Code Examples for com.drew.metadata.Directory#getTags()

The following examples show how to use com.drew.metadata.Directory#getTags() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExtractImageMetadata.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
private Map<String, String> getTags(Integer max, Metadata metadata) {
    Map<String, String> results = new HashMap<>();
    int i =0;

    for (Directory directory : metadata.getDirectories()) {
        for (Tag tag : directory.getTags()) {
            results.put(directory.getName() + "." + tag.getTagName(), tag.getDescription());

            if(max!=null) {
                i++;
                if (i >= max) {
                    return results;
                }
            }
        }
    }

    return results;
}
 
Example 2
Source File: MetadataProcessor.java    From FakeImageDetection with GNU General Public License v3.0 6 votes vote down vote up
public MetadataProcessor(File imageFile) {
    this.imageFile = imageFile;
    try {
        data = ImageMetadataReader.readMetadata(imageFile);
    } catch (Exception ex) {
        Logger.getLogger(MetadataProcessor.class.getName()).log(Level.SEVERE, null, ex);
    }
    
    for (Directory directory : data.getDirectories()) {
        extracted_data += String.format("----------------------------------------------%15s---------------------------------\n", directory.getName());
        for (Tag tag : directory.getTags()) {
            extracted_data += tag + "\n";
        }
        if (directory.hasErrors()) {
            for (String error : directory.getErrors()) {
                System.err.println("ERROR: " + error);
            }
        }
    }
}
 
Example 3
Source File: MetaDataTreeTableModel.java    From Pixelitor with GNU General Public License v3.0 6 votes vote down vote up
DirNode(Directory dir) {
    this.dir = dir;
    Collection<Tag> tags = dir.getTags();
    int tagIndex = 0;
    for (Tag tag : tags) {
        String tagName = tag.getTagName();
        if (tagName.startsWith("Unknown")) {
            continue;
        }
        String description = tag.getDescription();
        if (description != null && description.startsWith("Unknown")) {
            continue;
        }
        nodes.add(new TagNode(tagName, description, tagIndex));
        tagIndex++;
    }
}
 
Example 4
Source File: ImageResource.java    From entando-components with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected Map<String, String> getImgMetadata(File file, List<String> ignoreKeysList) {
    logger.debug("Get image Metadata in Resource Action");
    Map<String, String> meta = new HashMap<>();
    try {
        Metadata metadata = ImageMetadataReader.readMetadata(file);

        for (Directory directory : metadata.getDirectories()) {
            for (Tag tag : directory.getTags()) {
                if (!ignoreKeysList.contains(tag.getTagName())) {
                    logger.debug("Add Metadata with key: {}", tag.getTagName());
                    meta.put(tag.getTagName(), tag.getDescription());
                } else {
                    logger.debug("Skip Metadata key {}", tag.getTagName());
                }
            }
        }
    } catch (ImageProcessingException|IOException ex) {
        logger.error("Error reading image metadata for file {}", file.getName(), ex);
    }
    return meta;
}
 
Example 5
Source File: ExtractImageMetadata.java    From nifi with Apache License 2.0 6 votes vote down vote up
private Map<String, String> getTags(Integer max, Metadata metadata) {
    Map<String, String> results = new HashMap<>();
    int i =0;

    for (Directory directory : metadata.getDirectories()) {
        for (Tag tag : directory.getTags()) {
            results.put(directory.getName() + "." + tag.getTagName(), tag.getDescription());

            if(max!=null) {
                i++;
                if (i >= max) {
                    return results;
                }
            }
        }
    }

    return results;
}
 
Example 6
Source File: MetadataHelper.java    From leafpicrevived with GNU General Public License v3.0 5 votes vote down vote up
public MediaDetailsMap<String, String> getAllDetails(Context context, Media media) {
    MediaDetailsMap<String, String> data = new MediaDetailsMap<String, String>();
    try {
        Metadata metadata = ImageMetadataReader.readMetadata(context.getContentResolver().openInputStream(media.getUri()));
        for (Directory directory : metadata.getDirectories()) {

            for (Tag tag : directory.getTags()) {
                data.put(tag.getTagName(), directory.getObject(tag.getTagType()) + "");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return data;
}
 
Example 7
Source File: ImageDataReader.java    From java-11-examples with Apache License 2.0 5 votes vote down vote up
public static ImageInfo getImageInfo(InputStream imageStream) throws ImageProcessingException, IOException {
    Metadata metadata = ImageMetadataReader.readMetadata(imageStream);
    ImageInfo.Builder builder = new ImageInfo.Builder();
    for (Directory directory: metadata.getDirectories()) {
        for (Tag tag: directory.getTags()) {
            if (ParsingUtils.IMAGE_HEIGHT.equals(tag.getTagName())) {
                builder.setPixelsY(ParsingUtils.getIntegerFromData(tag.getDescription()));
            } else if (ParsingUtils.IMAGE_WIDTH.equals(tag.getTagName())) {
                builder.setPixelsX(ParsingUtils.getIntegerFromData(tag.getDescription()));
            }
        }
    }
    return builder.build();
}
 
Example 8
Source File: MutableImage.java    From react-native-camera-face-detector with MIT License 5 votes vote down vote up
public void writeDataToFile(File file, ReadableMap options, int jpegQualityPercent) throws IOException {
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(toJpeg(currentRepresentation, jpegQualityPercent));
    fos.close();

    try {
        ExifInterface exif = new ExifInterface(file.getAbsolutePath());

        // copy original exif data to the output exif...
        // unfortunately, this Android ExifInterface class doesn't understand all the tags so we lose some
        for (Directory directory : originalImageMetaData().getDirectories()) {
            for (Tag tag : directory.getTags()) {
                int tagType = tag.getTagType();
                Object object = directory.getObject(tagType);
                exif.setAttribute(tag.getTagName(), object.toString());
            }
        }

        writeLocationExifData(options, exif);

        if(hasBeenReoriented)
            rewriteOrientation(exif);

        exif.saveAttributes();
    } catch (ImageProcessingException  | IOException e) {
        Log.e(TAG, "failed to save exif data", e);
    }
}
 
Example 9
Source File: SampleProcessor.java    From tutorials with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
protected void process(Record record, SingleLaneBatchMaker batchMaker) throws StageException {
  LOG.info("Input record: {}", record);

  FileRef fileRef = record.get("/fileRef").getValueAsFileRef();
  Metadata metadata;
  try {
    metadata = ImageMetadataReader.readMetadata(fileRef.createInputStream(getContext(), InputStream.class));
  } catch (ImageProcessingException | IOException e) {
    String filename = record.get("/fileInfo/filename").getValueAsString();
    LOG.info("Exception getting metadata from {}", filename, e);
    throw new OnRecordErrorException(record, Errors.SAMPLE_02, e);
  }

  for (Directory directory : metadata.getDirectories()) {
    LinkedHashMap<String, Field> listMap = new LinkedHashMap<>();

    for (Tag tag : directory.getTags()) {
      listMap.put(tag.getTagName(), Field.create(tag.getDescription()));
    }

    if (directory.hasErrors()) {
      for (String error : directory.getErrors()) {
        LOG.info("ERROR: {}", error);
      }
    }

    record.set("/" + directory.getName(), Field.createListMap(listMap));
  }

  LOG.info("Output record: {}", record);

  batchMaker.addRecord(record);
}
 
Example 10
Source File: ProcessAllImagesInFolderUtility.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@Override
public void onExtractionSuccess(@NotNull File file, @NotNull Metadata metadata, @NotNull String relativePath, @NotNull PrintStream log)
{
    super.onExtractionSuccess(file, metadata, relativePath, log);

    for (Directory directory : metadata.getDirectories()) {
        for (Tag tag : directory.getTags()) {

            // Only interested in unknown tags (those without names)
            if (tag.hasTagName())
                continue;

            HashMap<Integer, Integer> occurrenceCountByTag = _occurrenceCountByTagByDirectory.get(directory.getName());
            if (occurrenceCountByTag == null) {
                occurrenceCountByTag = new HashMap<Integer, Integer>();
                _occurrenceCountByTagByDirectory.put(directory.getName(), occurrenceCountByTag);
            }

            Integer count = occurrenceCountByTag.get(tag.getTagType());
            if (count == null) {
                count = 0;
                occurrenceCountByTag.put(tag.getTagType(), 0);
            }

            occurrenceCountByTag.put(tag.getTagType(), count + 1);
        }
    }
}
 
Example 11
Source File: ProcessAllImagesInFolderUtility.java    From metadata-extractor with Apache License 2.0 5 votes vote down vote up
@Override
public void onExtractionSuccess(@NotNull File file, @NotNull Metadata metadata, @NotNull String relativePath, @NotNull PrintStream log)
{
    super.onExtractionSuccess(file, metadata, relativePath, log);

    // Iterate through all values, calling toString to flush out any formatting exceptions
    for (Directory directory : metadata.getDirectories()) {
        directory.getName();
        for (Tag tag : directory.getTags()) {
            tag.getTagName();
            tag.getDescription();
        }
    }
}
 
Example 12
Source File: ResourceAction.java    From entando-components with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected Map getImgMetadata(File file) {
    logger.debug("Get image Metadata in Resource Action");
    Map<String, String> meta = new HashMap<>();
    ResourceInterface resourcePrototype = this.getResourceManager().createResourceType(this.getResourceType());
    try {
        Metadata metadata = ImageMetadataReader.readMetadata(file);
        String ignoreKeysConf = resourcePrototype.getMetadataIgnoreKeys();
        String[] ignoreKeys = null;
        if (null != ignoreKeysConf) {
            ignoreKeys = ignoreKeysConf.split(",");
            logger.debug("Metadata ignoreKeys: {}", ignoreKeys);
        } else {
            logger.debug("Metadata ignoreKeys not configured");
        }
        List<String> ignoreKeysList = new ArrayList<String>();
        if (null != ignoreKeys) {
            ignoreKeysList = Arrays.asList(ignoreKeys);
        }
        for (Directory directory : metadata.getDirectories()) {
            for (Tag tag : directory.getTags()) {
                if (!ignoreKeysList.contains(tag.getTagName())) {
                    logger.debug("Add Metadata with key: {}", tag.getTagName());
                    meta.put(tag.getTagName(), tag.getDescription());
                } else {
                    logger.debug("Skip Metadata key {}", tag.getTagName());
                }
            }
        }
    } catch (ImageProcessingException ex) {
        logger.error("Error reading metadata from file " + this.getFileName() + " - message " + ex.getMessage());
    } catch (IOException ioex) {
        logger.error("Error reading file", ioex);
    }
    return meta;
}
 
Example 13
Source File: ImageRecordReader.java    From Bats with Apache License 2.0 4 votes vote down vote up
private void processDirectory(final MapWriter writer, final Directory directory, final Metadata metadata) {
  for (Tag tag : directory.getTags()) {
    try {
      final int tagType = tag.getTagType();
      Object value;
      if (descriptive || isDescriptionTag(directory, tagType)) {
        value = directory.getDescription(tagType);
        if (directory instanceof PngDirectory) {
          if (((PngDirectory) directory).getPngChunkType().areMultipleAllowed()) {
            value = new String[] { (String) value };
          }
        }
      } else {
        value = directory.getObject(tagType);
        if (directory instanceof ExifIFD0Directory && tagType == ExifIFD0Directory.TAG_DATETIME) {
          ExifSubIFDDirectory exifSubIFDDir = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
          String subsecond = null;
          if (exifSubIFDDir != null) {
            subsecond = exifSubIFDDir.getString(ExifSubIFDDirectory.TAG_SUBSECOND_TIME);
          }
          value = directory.getDate(tagType, subsecond, timeZone);
        } else if (directory instanceof ExifSubIFDDirectory) {
          if (tagType == ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL) {
            value = ((ExifSubIFDDirectory) directory).getDateOriginal(timeZone);
          } else if (tagType == ExifSubIFDDirectory.TAG_DATETIME_DIGITIZED) {
            value = ((ExifSubIFDDirectory) directory).getDateDigitized(timeZone);
          }
        } else if (directory instanceof GpsDirectory) {
          if (tagType == GpsDirectory.TAG_LATITUDE) {
            value = ((GpsDirectory) directory).getGeoLocation().getLatitude();
          } else if (tagType == GpsDirectory.TAG_LONGITUDE) {
            value = ((GpsDirectory) directory).getGeoLocation().getLongitude();
          }
        }
        if (isVersionTag(directory, tagType)) {
          value = directory.getString(tagType, "US-ASCII");
        } else if (isDateTag(directory, tagType)) {
          value = directory.getDate(tagType, timeZone);
        }
      }
      writeValue(writer, formatName(tag.getTagName()), value);
    } catch (Exception e) {
      // simply skip this tag
    }
  }
}