Java Code Examples for com.drew.metadata.Directory

The following examples show how to use com.drew.metadata.Directory. These examples are extracted from open source projects. 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 Project: localization_nifi   Source File: ExtractImageMetadata.java    License: 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 Project: pattypan   Source File: CreateFilePane.java    License: MIT License 6 votes vote down vote up
/**
 *
 * @param filePath
 * @return
 */
private String getExifDate(File file) {

  try {
    Metadata metadata = ImageMetadataReader.readMetadata(file);
    Directory directory = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
    int dateTag = ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL;

    if (directory != null && directory.containsTag(dateTag)) {
      Date date = directory.getDate(dateTag, TimeZone.getDefault());
      return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
    } else {
      return "";
    }
  } catch (ImageProcessingException | IOException ex) {
    Session.LOGGER.log(Level.INFO, 
        "Exif error for {0}: {1}",
        new String[]{file.getName(), ex.getLocalizedMessage()}
    );
    return "";
  }
}
 
Example 3
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 4
Source Project: metadata-extractor   Source File: XmpReader.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Performs the XMP data extraction, adding found values to the specified instance of {@link Metadata}.
 * <p>
 * The extraction is done with Adobe's XMPCore library.
 */
public void extract(@NotNull final String xmpString, @NotNull Metadata metadata, @Nullable Directory parentDirectory)
{
    XmpDirectory directory = new XmpDirectory();

    if (parentDirectory != null)
        directory.setParent(parentDirectory);

    try {
        XMPMeta xmpMeta = XMPMetaFactory.parseFromString(xmpString, PARSE_OPTIONS);
        directory.setXMPMeta(xmpMeta);
    } catch (XMPException e) {
        directory.addError("Error processing XMP data: " + e.getMessage());
    }

    if (!directory.isEmpty())
        metadata.addDirectory(directory);
}
 
Example 5
Source Project: metadata-extractor   Source File: ExifTiffHandler.java    License: Apache License 2.0 6 votes vote down vote up
private static boolean handlePrintIM(@NotNull final Directory directory, final int tagId)
{
    if (tagId == ExifDirectoryBase.TAG_PRINT_IMAGE_MATCHING_INFO)
        return true;

    if (tagId == 0x0E00) {
        // Tempting to say every tagid of 0x0E00 is a PIM tag, but can't be 100% sure
        if (directory instanceof CasioType2MakernoteDirectory ||
            directory instanceof KyoceraMakernoteDirectory ||
            directory instanceof NikonType2MakernoteDirectory ||
            directory instanceof OlympusMakernoteDirectory ||
            directory instanceof PanasonicMakernoteDirectory ||
            directory instanceof PentaxMakernoteDirectory ||
            directory instanceof RicohMakernoteDirectory ||
            directory instanceof SanyoMakernoteDirectory ||
            directory instanceof SonyType1MakernoteDirectory)
            return true;
    }

    return false;
}
 
Example 6
Source Project: metadata-extractor   Source File: AdobeJpegReader.java    License: Apache License 2.0 6 votes vote down vote up
public void extract(@NotNull SequentialReader reader, @NotNull Metadata metadata)
{
    Directory directory = new AdobeJpegDirectory();
    metadata.addDirectory(directory);

    try {
        reader.setMotorolaByteOrder(false);

        if (!reader.getString(PREAMBLE.length()).equals(PREAMBLE)) {
            directory.addError("Invalid Adobe JPEG data header.");
            return;
        }

        directory.setInt(AdobeJpegDirectory.TAG_DCT_ENCODE_VERSION, reader.getUInt16());
        directory.setInt(AdobeJpegDirectory.TAG_APP14_FLAGS0, reader.getUInt16());
        directory.setInt(AdobeJpegDirectory.TAG_APP14_FLAGS1, reader.getUInt16());
        directory.setInt(AdobeJpegDirectory.TAG_COLOR_TRANSFORM, reader.getInt8());
    } catch (IOException ex) {
        directory.addError("IO exception processing data: " + ex.getMessage());
    }
}
 
Example 7
private static void writeHierarchyLevel(@NotNull Metadata metadata, @NotNull PrintWriter writer, @Nullable Directory parent, int level)
{
    final int indent = 4;

    for (Directory child : metadata.getDirectories()) {
        if (parent == null) {
            if (child.getParent() != null)
                continue;
        } else if (!parent.equals(child.getParent())) {
            continue;
        }

        for (int i = 0; i < level*indent; i++) {
            writer.write(' ');
        }
        writer.write("- ");
        writer.write(child.getName());
        writer.write(NEW_LINE);
        writeHierarchyLevel(metadata, writer, child, level + 1);
    }
}
 
Example 8
Source Project: metadata-extractor   Source File: ExifDirectoryTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGetDirectoryName() throws Exception
{
    Directory subIFDDirectory = new ExifSubIFDDirectory();
    Directory ifd0Directory = new ExifIFD0Directory();
    Directory thumbDirectory = new ExifThumbnailDirectory();
    Directory gpsDirectory = new GpsDirectory();

    assertFalse(subIFDDirectory.hasErrors());
    assertFalse(ifd0Directory.hasErrors());
    assertFalse(thumbDirectory.hasErrors());
    assertFalse(gpsDirectory.hasErrors());

    assertEquals("Exif IFD0", ifd0Directory.getName());
    assertEquals("Exif SubIFD", subIFDDirectory.getName());
    assertEquals("Exif Thumbnail", thumbDirectory.getName());
    assertEquals("GPS", gpsDirectory.getName());
}
 
Example 9
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 10
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 11
Source Project: nifi   Source File: ExtractImageMetadata.java    License: 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 12
Source Project: Bats   Source File: ImageRecordReader.java    License: Apache License 2.0 5 votes vote down vote up
private boolean isVersionTag(final Directory directory, final int tagType) {
  return directory instanceof ExifSubIFDDirectory &&
      (tagType == ExifSubIFDDirectory.TAG_EXIF_VERSION || tagType == ExifSubIFDDirectory.TAG_FLASHPIX_VERSION) ||
      directory instanceof ExifInteropDirectory &&
      tagType == ExifInteropDirectory.TAG_INTEROP_VERSION ||
      directory instanceof FujifilmMakernoteDirectory &&
      tagType == FujifilmMakernoteDirectory.TAG_MAKERNOTE_VERSION ||
      directory instanceof NikonType2MakernoteDirectory &&
      tagType == NikonType2MakernoteDirectory.TAG_FIRMWARE_VERSION ||
      directory instanceof OlympusCameraSettingsMakernoteDirectory &&
      tagType == OlympusCameraSettingsMakernoteDirectory.TagCameraSettingsVersion ||
      directory instanceof OlympusEquipmentMakernoteDirectory &&
      tagType == OlympusEquipmentMakernoteDirectory.TAG_EQUIPMENT_VERSION ||
      directory instanceof OlympusFocusInfoMakernoteDirectory &&
      tagType == OlympusFocusInfoMakernoteDirectory.TagFocusInfoVersion ||
      directory instanceof OlympusImageProcessingMakernoteDirectory &&
      tagType == OlympusImageProcessingMakernoteDirectory.TagImageProcessingVersion ||
      directory instanceof OlympusMakernoteDirectory &&
      tagType == OlympusMakernoteDirectory.TAG_MAKERNOTE_VERSION ||
      directory instanceof OlympusRawDevelopment2MakernoteDirectory &&
      tagType == OlympusRawDevelopment2MakernoteDirectory.TagRawDevVersion ||
      directory instanceof OlympusRawDevelopmentMakernoteDirectory &&
      tagType == OlympusRawDevelopmentMakernoteDirectory.TagRawDevVersion ||
      directory instanceof OlympusRawInfoMakernoteDirectory &&
      tagType == OlympusRawInfoMakernoteDirectory.TagRawInfoVersion ||
      directory instanceof PanasonicMakernoteDirectory &&
      (tagType == PanasonicMakernoteDirectory.TAG_FIRMWARE_VERSION || tagType == PanasonicMakernoteDirectory.TAG_MAKERNOTE_VERSION || tagType == PanasonicMakernoteDirectory.TAG_EXIF_VERSION) ||
      directory instanceof SamsungType2MakernoteDirectory &&
      tagType == SamsungType2MakernoteDirectory.TagMakerNoteVersion ||
      directory instanceof SonyType6MakernoteDirectory &&
      tagType == SonyType6MakernoteDirectory.TAG_MAKERNOTE_THUMB_VERSION ||
      directory instanceof PanasonicRawIFD0Directory &&
      tagType == PanasonicRawIFD0Directory.TagPanasonicRawVersion;
}
 
Example 13
Source Project: Bats   Source File: GenericMetadataDirectory.java    License: Apache License 2.0 5 votes vote down vote up
public void setPixelWidth(Directory directory, int tagType) {
  try {
    setPixelWidth(directory.getInt(tagType));
  } catch (MetadataException e) {
    // Nothing needs to be done
  }
}
 
Example 14
Source Project: Bats   Source File: GenericMetadataDirectory.java    License: Apache License 2.0 5 votes vote down vote up
public void setPixelHeight(Directory directory, int tagType) {
  try {
    setPixelHeight(directory.getInt(tagType));
  } catch (MetadataException e) {
    // Nothing needs to be done
  }
}
 
Example 15
Source Project: Bats   Source File: GenericMetadataDirectory.java    License: Apache License 2.0 5 votes vote down vote up
public void setOrientation(Directory directory, int tagType) {
  try {
    setOrientation(directory.getInt(tagType));
  } catch (MetadataException e) {
    // Nothing needs to be done
  }
}
 
Example 16
Source Project: Bats   Source File: GenericMetadataDirectory.java    License: Apache License 2.0 5 votes vote down vote up
public void setDPIWidth(Directory directory, int tagType) {
  try {
    setDPIWidth(directory.getInt(tagType));
  } catch (MetadataException e) {
    // Nothing needs to be done
  }
}
 
Example 17
Source Project: Bats   Source File: GenericMetadataDirectory.java    License: Apache License 2.0 5 votes vote down vote up
public void setDPIWidth(Directory directory, int tagType, double factor) {
  try {
    setDPIWidth(directory.getInt(tagType) * factor);
  } catch (MetadataException e) {
    // Nothing needs to be done
  }
}
 
Example 18
Source Project: Bats   Source File: GenericMetadataDirectory.java    License: Apache License 2.0 5 votes vote down vote up
public void setDPIHeight(Directory directory, int tagType) {
  try {
    setDPIHeight(directory.getInt(tagType));
  } catch (MetadataException e) {
    // Nothing needs to be done
  }
}
 
Example 19
Source Project: Bats   Source File: GenericMetadataDirectory.java    License: Apache License 2.0 5 votes vote down vote up
public void setDPIHeight(Directory directory, int tagType, double factor) {
  try {
    setDPIHeight(directory.getInt(tagType) * factor);
  } catch (MetadataException e) {
    // Nothing needs to be done
  }
}
 
Example 20
Source Project: Bats   Source File: GenericMetadataDirectory.java    License: Apache License 2.0 5 votes vote down vote up
public void setBitPerPixel(Directory directory, int tagType) {
  try {
    setBitPerPixel(directory.getInt(tagType));
  } catch (MetadataException e) {
    // Nothing needs to be done
  }
}
 
Example 21
Source Project: Bats   Source File: GenericMetadataDirectory.java    License: Apache License 2.0 5 votes vote down vote up
public void setBitPerPixel(Directory directory, int tagType1, int tagType2) {
  try {
    setBitPerPixel(directory.getInt(tagType1) * directory.getInt(tagType2));
  } catch (MetadataException e) {
    // Nothing needs to be done
  }
}
 
Example 22
Source Project: Bats   Source File: GenericMetadataDirectory.java    License: Apache License 2.0 5 votes vote down vote up
public void setAlpha(Directory directory, int tagType) {
  try {
    setAlpha(directory.getBoolean(tagType));
  } catch (MetadataException e) {
    // Nothing needs to be done
  }
}
 
Example 23
Source Project: Bats   Source File: GenericMetadataDirectory.java    License: Apache License 2.0 5 votes vote down vote up
public void setDuration(Directory directory, int tagType) {
  Object o = directory.getObject(tagType);
  if (o != null) {
    if (o instanceof String) {
      String[] time = ((String) o).split(":");
      setDuration(
        Long.parseLong(time[0]) * 3600 +
        Long.parseLong(time[1]) * 60 +
        Long.parseLong(time[2]));
    } else if (o instanceof Number) {
      setDuration(((Number) o).longValue());
    }
  }
}
 
Example 24
Source Project: Bats   Source File: GenericMetadataDirectory.java    License: Apache License 2.0 5 votes vote down vote up
public void setFrameRate(Directory directory, int tagType) {
  try {
    setFrameRate(directory.getDouble(tagType));
  } catch (MetadataException e) {
    // Nothing needs to be done
  }
}
 
Example 25
Source Project: Bats   Source File: GenericMetadataDirectory.java    License: Apache License 2.0 5 votes vote down vote up
public void setAudioSampleSize(Directory directory, int tagType) {
  try {
    setAudioSampleSize(directory.getInt(tagType));
  } catch (MetadataException e) {
    // Nothing needs to be done
  }
}
 
Example 26
Source Project: Bats   Source File: GenericMetadataDirectory.java    License: Apache License 2.0 5 votes vote down vote up
public void setAudioSampleRate(Directory directory, int tagType) {
  try {
    setAudioSampleRate(directory.getDouble(tagType));
  } catch (MetadataException e) {
    // Nothing needs to be done
  }
}
 
Example 27
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 28
Source Project: java-11-examples   Source File: ImageDataReader.java    License: 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 29
Source Project: spring-boot   Source File: RotateImgUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取图片正确显示需要旋转的角度(顺时针)
 */
private static final int getRotateAngle(InputStream is) throws TSException {
    int angle = 0;
    Metadata metadata;
    try {
        metadata = JpegMetadataReader.readMetadata(is);
        Directory directory = metadata.getDirectory(ExifDirectory.class);
        if (directory.containsTag(ExifDirectory.TAG_ORIENTATION)) {
            // Exif信息中方向  
            int orientation = directory.getInt(ExifDirectory.TAG_ORIENTATION);
            // 原图片的方向信息
            if (6 == orientation) {
                //6旋转90
                angle = 90;
            } else if (3 == orientation) {
                //3旋转180
                angle = 180;
            } else if (8 == orientation) {
                //8旋转90
                angle = 270;
            }
        }
    } catch (Exception e) {
        throw new TSException(TSEDictionary.UNDEFINED_FAIL.getCode(), "iphone图片服务器旋转异常");
    }
    return angle;
}
 
Example 30
Source Project: spring-boot   Source File: RotateImgUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取图片正确显示需要旋转的角度(顺时针)
 *
 * @return
 * @throws Exception
 */
private static final int getRotateAngle(File file) throws TSException {
    int angle = 0;
    Metadata metadata;
    try {
        metadata = JpegMetadataReader.readMetadata(file);
        Directory directory = metadata.getDirectory(ExifDirectory.class);
        if (directory.containsTag(ExifDirectory.TAG_ORIENTATION)) {
            // Exif信息中方向  
            int orientation = directory.getInt(ExifDirectory.TAG_ORIENTATION);
            // 原图片的方向信息
            if (6 == orientation) {
                //6旋转90
                angle = 90;
            } else if (3 == orientation) {
                //3旋转180
                angle = 180;
            } else if (8 == orientation) {
                //8旋转90
                angle = 270;
            }
        }
    } catch (Exception e) {
        throw new TSException(TSEDictionary.UNDEFINED_FAIL.getCode(), "iphone图片服务器旋转异常");
    }
    return angle;
}