Java Code Examples for com.drew.metadata.exif.ExifIFD0Directory

The following examples show how to use com.drew.metadata.exif.ExifIFD0Directory. 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: react-native-camera-face-detector   Source File: MutableImage.java    License: MIT License 6 votes vote down vote up
public void fixOrientation() throws ImageMutationFailedException {
    try {
        Metadata metadata = originalImageMetaData();

        ExifIFD0Directory exifIFD0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
        if (exifIFD0Directory == null) {
            return;
        } else if (exifIFD0Directory.containsTag(ExifIFD0Directory.TAG_ORIENTATION)) {
            int exifOrientation = exifIFD0Directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
            if(exifOrientation != 1) {
                rotate(exifOrientation);
                exifIFD0Directory.setInt(ExifIFD0Directory.TAG_ORIENTATION, 1);
            }
        }
    } catch (ImageProcessingException | IOException | MetadataException e) {
        throw new ImageMutationFailedException("failed to fix orientation", e);
    }
}
 
Example 2
Source Project: metadata-extractor   Source File: MetadataTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testOrderOfDifferentTypes()
{
    Metadata metadata = new Metadata();
    Directory directory1 = new ExifSubIFDDirectory();
    Directory directory2 = new ExifThumbnailDirectory();
    Directory directory3 = new ExifIFD0Directory();

    metadata.addDirectory(directory1);
    metadata.addDirectory(directory2);
    metadata.addDirectory(directory3);

    List<Directory> directories = new ArrayList<Directory>();
    for (Directory directory : metadata.getDirectories()) {
        directories.add(directory);
    }

    assertEquals(3, directories.size());
    assertSame(directory1, directories.toArray()[0]);
    assertSame(directory2, directories.toArray()[1]);
    assertSame(directory3, directories.toArray()[2]);
}
 
Example 3
Source Project: scrimage   Source File: OrientationTools.java    License: Apache License 2.0 6 votes vote down vote up
static Set<Orientation> imageOrientationsOf(ImageMetadata metadata) {

      String exifIFD0DirName = new ExifIFD0Directory().getName();

      Tag[] tags = Arrays.stream(metadata.getDirectories())
         .filter(dir -> dir.getName().equals(exifIFD0DirName))
         .findFirst()
         .map(Directory::getTags)
         .orElseGet(() -> new Tag[0]);

      Set<Tag> tag274s = Arrays.stream(tags).filter(t -> t.getType() == 274).collect(Collectors.toSet());

      if (tag274s.size() == 1) {
         return tag274s.stream()
            .map(OrientationTools::fromTag)
            .collect(Collectors.toSet());
      } else {
         return tag274s.stream()
            .filter(t -> t.getName().toLowerCase().equals("orientation"))
            .map(OrientationTools::fromTag)
            .collect(Collectors.toSet());
      }
   }
 
Example 4
Row(@NotNull File file, @NotNull Metadata metadata, @NotNull String relativePath)
{
    this.file = file;
    this.metadata = metadata;
    this.relativePath = relativePath;

    ExifIFD0Directory ifd0Dir = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
    ExifSubIFDDirectory subIfdDir = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
    ExifThumbnailDirectory thumbDir = metadata.getFirstDirectoryOfType(ExifThumbnailDirectory.class);
    if (ifd0Dir != null) {
        manufacturer = ifd0Dir.getDescription(ExifIFD0Directory.TAG_MAKE);
        model = ifd0Dir.getDescription(ExifIFD0Directory.TAG_MODEL);
    }
    boolean hasMakernoteData = false;
    if (subIfdDir != null) {
        exifVersion = subIfdDir.getDescription(ExifSubIFDDirectory.TAG_EXIF_VERSION);
        hasMakernoteData = subIfdDir.containsTag(ExifSubIFDDirectory.TAG_MAKERNOTE);
    }
    if (thumbDir != null) {
        Integer width = thumbDir.getInteger(ExifThumbnailDirectory.TAG_IMAGE_WIDTH);
        Integer height = thumbDir.getInteger(ExifThumbnailDirectory.TAG_IMAGE_HEIGHT);
        thumbnail = width != null && height != null
            ? String.format("Yes (%s x %s)", width, height)
            : "Yes";
    }
    for (Directory directory : metadata.getDirectories()) {
        if (directory.getClass().getName().contains("Makernote")) {
            makernote = directory.getName().replace("Makernote", "").trim();
            break;
        }
    }
    if (makernote == null) {
        makernote = hasMakernoteData ? "(Unknown)" : "N/A";
    }
}
 
Example 5
Source Project: metadata-extractor   Source File: DirectoryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testToString()
{
    Directory directory = new ExifIFD0Directory();
    assertEquals("Exif IFD0 Directory (0 tags)", directory.toString());
    directory.setString(1, "Tag 1");
    assertEquals("Exif IFD0 Directory (1 tag)", directory.toString());
    directory.setString(2, "Tag 2");
    assertEquals("Exif IFD0 Directory (2 tags)", directory.toString());
}
 
Example 6
Source Project: metadata-extractor   Source File: MetadataTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testToString()
{
    Metadata metadata = new Metadata();
    assertEquals("Metadata (0 directories)", metadata.toString());

    metadata.addDirectory(new ExifIFD0Directory());
    assertEquals("Metadata (1 directory)", metadata.toString());

    metadata.addDirectory(new ExifSubIFDDirectory());
    assertEquals("Metadata (2 directories)", metadata.toString());
}
 
Example 7
Source Project: Bats   Source File: ImageRecordReader.java    License: 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
    }
  }
}
 
Example 8
Source Project: sejda   Source File: ExifHelper.java    License: GNU Affero General Public License v3.0 4 votes vote down vote up
private static int readExifOrientation(Metadata metadata) throws MetadataException {
    Directory directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
    return directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
}