Java Code Examples for com.drew.metadata.Metadata

The following examples show how to use com.drew.metadata.Metadata. 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
Source Project: metadata-extractor   Source File: QuickTimeMediaHandler.java    License: Apache License 2.0 6 votes vote down vote up
public QuickTimeMediaHandler(Metadata metadata, QuickTimeContext context)
{
    super(metadata);

    if (context.creationTime != null && context.modificationTime != null) {
        // Get creation/modification times
        directory.setDate(
            QuickTimeMediaDirectory.TAG_CREATION_TIME,
            DateUtil.get1Jan1904EpochDate(context.creationTime)
        );
        directory.setDate(
            QuickTimeMediaDirectory.TAG_MODIFICATION_TIME,
            DateUtil.get1Jan1904EpochDate(context.modificationTime)
        );
    }
}
 
Example 4
Source Project: metadata-extractor   Source File: Mp4HandlerFactory.java    License: Apache License 2.0 6 votes vote down vote up
public Mp4Handler<?> getHandler(HandlerBox box, Metadata metadata, Mp4Context context)
{
    String type = box.getHandlerType();
    if (type.equals(HANDLER_SOUND_MEDIA)) {
        return new Mp4SoundHandler(metadata, context);
    } else if (type.equals(HANDLER_VIDEO_MEDIA)) {
        return new Mp4VideoHandler(metadata, context);
    } else if (type.equals(HANDLER_HINT_MEDIA)) {
        return new Mp4HintHandler(metadata, context);
    } else if (type.equals(HANDLER_TEXT_MEDIA)) {
        return new Mp4TextHandler(metadata, context);
    } else if (type.equals(HANDLER_META_MEDIA)) {
        return new Mp4MetaHandler(metadata, context);
    }
    return caller;
}
 
Example 5
Source Project: metadata-extractor   Source File: EpsMetadataReader.java    License: Apache License 2.0 6 votes vote down vote up
@NotNull
public static Metadata readMetadata(@NotNull File file) throws IOException
{
    Metadata metadata = new Metadata();

    FileInputStream stream = new FileInputStream(file);

    try {
        new EpsReader().extract(stream, metadata);
    } finally {
        stream.close();
    }

    new FileSystemMetadataReader().read(file, metadata);
    return metadata;
}
 
Example 6
public QuickTimeHandler<?> getHandler(String type, Metadata metadata, QuickTimeContext context)
{
    if (type.equals(HANDLER_METADATA_DIRECTORY)) {
        return new QuickTimeDirectoryHandler(metadata);
    } else if (type.equals(HANDLER_METADATA_DATA)) {
        return new QuickTimeDataHandler(metadata);
    } else if (type.equals(HANDLER_SOUND_MEDIA)) {
        return new QuickTimeSoundHandler(metadata, context);
    } else if (type.equals(HANDLER_VIDEO_MEDIA)) {
        return new QuickTimeVideoHandler(metadata, context);
    } else if (type.equals(HANDLER_TIMECODE_MEDIA)) {
        return new QuickTimeTimecodeHandler(metadata, context);
    } else if (type.equals(HANDLER_TEXT_MEDIA)) {
        return new QuickTimeTextHandler(metadata, context);
    } else if (type.equals(HANDLER_SUBTITLE_MEDIA)) {
        return new QuickTimeSubtitleHandler(metadata, context);
    } else if (type.equals(HANDLER_MUSIC_MEDIA)) {
        return new QuickTimeMusicHandler(metadata, context);
    }
    return caller;
}
 
Example 7
Source Project: metadata-extractor   Source File: PngMetadataReader.java    License: Apache License 2.0 6 votes vote down vote up
@NotNull
public static Metadata readMetadata(@NotNull InputStream inputStream) throws PngProcessingException, IOException
{
    Iterable<PngChunk> chunks = new PngChunkReader().extract(new StreamReader(inputStream), _desiredChunkTypes);

    Metadata metadata = new Metadata();

    for (PngChunk chunk : chunks) {
        try {
            processChunk(metadata, chunk);
        } catch (Exception e) {
            metadata.addDirectory(new ErrorDirectory("Exception reading PNG chunk: " + e.getMessage()));
        }
    }

    return metadata;
}
 
Example 8
@Override
public void onExtractionSuccess(@NotNull File file, @NotNull Metadata metadata, @NotNull String relativePath, @NotNull PrintStream log)
{
    super.onExtractionSuccess(file, metadata, relativePath, log);

    String extension = getExtension(file);

    if (extension == null) {
        return;
    }

    // Sanitise the extension
    extension = extension.toLowerCase();
    if (_extensionEquivalence.containsKey(extension))
        extension = _extensionEquivalence.get(extension);

    List<Row> list = _rowListByExtension.get(extension);
    if (list == null) {
        list = new ArrayList<Row>();
        _rowListByExtension.put(extension, list);
    }
    list.add(new Row(file, metadata, relativePath));
}
 
Example 9
Source Project: metadata-extractor   Source File: XmpReaderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception
{
    Metadata metadata = new Metadata();
    List<byte[]> jpegSegments = new ArrayList<byte[]>();
    jpegSegments.add(FileUtil.readBytes("Tests/Data/withXmpAndIptc.jpg.app1.1"));
    new XmpReader().readJpegSegments(jpegSegments, metadata, JpegSegmentType.APP1);

    Collection<XmpDirectory> xmpDirectories = metadata.getDirectoriesOfType(XmpDirectory.class);

    assertNotNull(xmpDirectories);
    assertEquals(1, xmpDirectories.size());

    _directory = xmpDirectories.iterator().next();

    assertFalse(_directory.hasErrors());
}
 
Example 10
Source Project: metadata-extractor   Source File: JpegDnlReader.java    License: Apache License 2.0 6 votes vote down vote up
public void extract(byte[] segmentBytes, Metadata metadata, JpegSegmentType segmentType)
{
    JpegDirectory directory = metadata.getFirstDirectoryOfType(JpegDirectory.class);
    if (directory == null) {
        ErrorDirectory errorDirectory = new ErrorDirectory();
        metadata.addDirectory(errorDirectory);
        errorDirectory.addError("DNL segment found without SOFx - illegal JPEG format");
        return;
    }

    SequentialReader reader = new SequentialByteArrayReader(segmentBytes);

    try {
        // Only set height from DNL if it's not already defined
        Integer i = directory.getInteger(JpegDirectory.TAG_IMAGE_HEIGHT);
        if (i == null || i == 0) {
            directory.setInt(JpegDirectory.TAG_IMAGE_HEIGHT, reader.getUInt16());
        }
    } catch (IOException ex) {
        directory.addError(ex.getMessage());
    }
}
 
Example 11
Source Project: metadata-extractor   Source File: ExifReaderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testDifferenceImageAndThumbnailOrientations() throws Exception
{
    // This metadata contains different orientations for the thumbnail and the main image.
    // These values used to be merged into a single directory, causing errors.
    // This unit test demonstrates correct behaviour.
    Metadata metadata = processBytes("Tests/Data/repeatedOrientationTagWithDifferentValues.jpg.app1");
    ExifIFD0Directory ifd0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
    ExifThumbnailDirectory thumbnailDirectory = metadata.getFirstDirectoryOfType(ExifThumbnailDirectory.class);

    assertNotNull(ifd0Directory);
    assertNotNull(thumbnailDirectory);

    assertEquals(1, ifd0Directory.getInt(ExifIFD0Directory.TAG_ORIENTATION));
    assertEquals(8, thumbnailDirectory.getInt(ExifThumbnailDirectory.TAG_ORIENTATION));
}
 
Example 12
Source Project: metadata-extractor   Source File: ExifReaderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testReadJpegSegmentWithNoExifData() throws Exception
{
    byte[] badExifData = new byte[]{ 1,2,3,4,5,6,7,8,9,10 };
    Metadata metadata = new Metadata();
    ArrayList<byte[]> segments = new ArrayList<byte[]>();
    segments.add(badExifData);
    new ExifReader().readJpegSegments(segments, metadata, JpegSegmentType.APP1);
    assertEquals(0, metadata.getDirectoryCount());
    assertFalse(metadata.hasErrors());
}
 
Example 13
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 14
@Before
public void setUp() throws Exception
{
    Metadata metadata = ExifReaderTest.processBytes("Tests/Data/nikonMakernoteType1.jpg.app1");

    _nikonDirectory = metadata.getFirstDirectoryOfType(NikonType1MakernoteDirectory.class);
    _exifSubIFDDirectory = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
    _exifIFD0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
    _thumbDirectory = metadata.getFirstDirectoryOfType(ExifThumbnailDirectory.class);
}
 
Example 15
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 16
Source Project: scrimage   Source File: ImageMetadata.java    License: Apache License 2.0 5 votes vote down vote up
public static ImageMetadata fromMetadata(Metadata metadata) {
   Directory[] dirs = StreamSupport.stream(metadata.getDirectories().spliterator(), false).map(dir -> {
      Tag[] tags = dir.getTags().stream().map(tag ->
         new Tag(tag.getTagName(), tag.getTagType(), dir.getString(tag.getTagType()), tag.getDescription())
      ).toArray(Tag[]::new);
      return new Directory(dir.getName(), tags);
   }).toArray(Directory[]::new);
   return new ImageMetadata(dirs);
}
 
Example 17
Source Project: metadata-extractor   Source File: PhotoshopReader.java    License: Apache License 2.0 5 votes vote down vote up
public void readJpegSegments(@NotNull Iterable<byte[]> segments, @NotNull Metadata metadata, @NotNull JpegSegmentType segmentType)
{
    final int preambleLength = JPEG_SEGMENT_PREAMBLE.length();

    for (byte[] segmentBytes : segments) {
        // Ensure data starts with the necessary preamble
        if (segmentBytes.length < preambleLength + 1 || !JPEG_SEGMENT_PREAMBLE.equals(new String(segmentBytes, 0, preambleLength)))
            continue;

        extract(
            new SequentialByteArrayReader(segmentBytes, preambleLength + 1),
            segmentBytes.length - preambleLength - 1,
            metadata);
    }
}
 
Example 18
Source Project: react-native-camera-face-detector   Source File: MutableImage.java    License: MIT License 5 votes vote down vote up
private Metadata originalImageMetaData() throws ImageProcessingException, IOException {
    if(this.originalImageMetaData == null) {//this is expensive, don't do it more than once
        originalImageMetaData = ImageMetadataReader.readMetadata(
                new BufferedInputStream(new ByteArrayInputStream(originalImageData)),
                originalImageData.length
        );
    }
    return originalImageMetaData;
}
 
Example 19
Source Project: scrimage   Source File: ImageMetadata.java    License: Apache License 2.0 5 votes vote down vote up
public static ImageMetadata fromImage(ImmutableImage image) throws IOException {
   try (ByteArrayInputStream stream = new ByteArrayInputStream(image.bytes(PngWriter.NoCompression))) {
      Metadata metadata = ImageMetadataReader.readMetadata(stream);
      return fromMetadata(metadata);
   } catch (ImageProcessingException e) {
      throw new IOException(e);
   }
}
 
Example 20
Source Project: metadata-extractor   Source File: JpegReaderTest.java    License: Apache License 2.0 5 votes vote down vote up
@NotNull
public static JpegDirectory processBytes(String filePath) throws IOException
{
    Metadata metadata = new Metadata();
    new JpegReader().extract(FileUtil.readBytes(filePath), metadata, JpegSegmentType.SOF0);

    JpegDirectory directory = metadata.getFirstDirectoryOfType(JpegDirectory.class);
    assertNotNull(directory);
    return directory;
}
 
Example 21
Source Project: metadata-extractor   Source File: EpsReader.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Decodes a commented hex section, and uses {@link PhotoshopReader} to decode the resulting data.
 */
private static void extractPhotoshopData(@NotNull final Metadata metadata, @NotNull SequentialReader reader) throws IOException
{
    byte[] buffer = decodeHexCommentBlock(reader);

    if (buffer != null)
        new PhotoshopReader().extract(new SequentialByteArrayReader(buffer), buffer.length, metadata);
}
 
Example 22
Source Project: metadata-extractor   Source File: JpegMetadataReader.java    License: Apache License 2.0 5 votes vote down vote up
public static void processJpegSegmentData(Metadata metadata, Iterable<JpegSegmentMetadataReader> readers, JpegSegmentData segmentData)
{
    // Pass the appropriate byte arrays to each reader.
    for (JpegSegmentMetadataReader reader : readers) {
        for (JpegSegmentType segmentType : reader.getSegmentTypes()) {
            reader.readJpegSegments(segmentData.getSegments(segmentType), metadata, segmentType);
        }
    }
}
 
Example 23
Source Project: metadata-extractor   Source File: GifMetadataReader.java    License: Apache License 2.0 5 votes vote down vote up
@NotNull
public static Metadata readMetadata(@NotNull InputStream inputStream)
{
    Metadata metadata = new Metadata();
    new GifReader().extract(new StreamReader(inputStream), metadata);
    return metadata;
}
 
Example 24
Source Project: Pixelitor   Source File: MetaDataPanel.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void changeFile(File file) {
    Metadata metadata = extractMetadata(file);
    if (metadata != null) {
        treeTable.setTreeTableModel(new MetaDataTreeTableModel(metadata));
        setupColumnsWidths();
        JDialog d = (JDialog) SwingUtilities.getWindowAncestor(this);
        d.setTitle("Metadata for " + file.getName());
    }
}
 
Example 25
Source Project: metadata-extractor   Source File: BmpMetadataReader.java    License: Apache License 2.0 5 votes vote down vote up
@NotNull
public static Metadata readMetadata(@NotNull InputStream inputStream)
{
    Metadata metadata = new Metadata();
    new BmpReader().extract(new StreamReader(inputStream), metadata);
    return metadata;
}
 
Example 26
Source Project: metadata-extractor   Source File: BmpReaderTest.java    License: Apache License 2.0 5 votes vote down vote up
@NotNull
public static BmpHeaderDirectory processBytes(@NotNull String file) throws Exception
{
    Metadata metadata = new Metadata();
    InputStream stream = new FileInputStream(file);
    new BmpReader().extract(new StreamReader(stream), metadata);
    stream.close();

    BmpHeaderDirectory directory = metadata.getFirstDirectoryOfType(BmpHeaderDirectory.class);
    assertNotNull(directory);
    return directory;
}
 
Example 27
Source Project: metadata-extractor   Source File: WebpMetadataReader.java    License: Apache License 2.0 5 votes vote down vote up
@NotNull
public static Metadata readMetadata(@NotNull InputStream inputStream) throws IOException, RiffProcessingException
{
    Metadata metadata = new Metadata();
    new RiffReader().processRiff(new StreamReader(inputStream), new WebpRiffHandler(metadata));
    return metadata;
}
 
Example 28
Source Project: metadata-extractor   Source File: AviMetadataReader.java    License: Apache License 2.0 5 votes vote down vote up
@NotNull
public static Metadata readMetadata(@NotNull File file) throws IOException, RiffProcessingException
{
    InputStream inputStream = new FileInputStream(file);
    Metadata metadata;
    try {
        metadata = readMetadata(inputStream);
    } finally {
        inputStream.close();
    }
    new FileSystemMetadataReader().read(file, metadata);
    return metadata;
}
 
Example 29
Source Project: metadata-extractor   Source File: WavMetadataReader.java    License: Apache License 2.0 5 votes vote down vote up
@NotNull
public static Metadata readMetadata(@NotNull File file) throws IOException, RiffProcessingException
{
    InputStream inputStream = new FileInputStream(file);
    Metadata metadata;
    try {
        metadata = readMetadata(inputStream);
    } finally {
        inputStream.close();
    }
    new FileSystemMetadataReader().read(file, metadata);
    return metadata;
}
 
Example 30
@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();
        }
    }
}