1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
/*
* Copyright 2002-2017 Drew Noakes
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* More information about this project is available at:
*
* https://drewnoakes.com/code/exif/
* https://github.com/drewnoakes/metadata-extractor
*/
package com.drew.metadata;
import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.imaging.jpeg.JpegSegmentMetadataReader;
import com.drew.metadata.exif.ExifReader;
import com.drew.metadata.iptc.IptcReader;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
/**
* Showcases the most popular ways of using the metadata-extractor library.
* <p>
* For more information, see the project wiki: https://github.com/drewnoakes/metadata-extractor/wiki/GettingStarted
*
* @author Drew Noakes https://drewnoakes.com
*/
public class SampleUsage
{
public static void main(String[] args)
{
File file = new File("Tests/Data/withIptcExifGps.jpg");
// There are multiple ways to get a Metadata object for a file
//
// SCENARIO 1: UNKNOWN FILE TYPE
//
// This is the most generic approach. It will transparently determine the file type and invoke the appropriate
// readers. In most cases, this is the most appropriate usage. This will handle JPEG, TIFF, GIF, BMP and RAW
// (CRW/CR2/NEF/RW2/ORF) files and extract whatever metadata is available and understood.
//
try {
Metadata metadata = ImageMetadataReader.readMetadata(file);
print(metadata, "Using ImageMetadataReader");
} catch (ImageProcessingException e) {
print(e);
} catch (IOException e) {
print(e);
}
//
// SCENARIO 2: SPECIFIC FILE TYPE
//
// If you know the file to be a JPEG, you may invoke the JpegMetadataReader, rather than the generic reader
// used in approach 1. Similarly, if you knew the file to be a TIFF/RAW image you might use TiffMetadataReader,
// PngMetadataReader for PNG files, BmpMetadataReader for BMP files, or GifMetadataReader for GIF files.
//
// Using the specific reader offers a very, very slight performance improvement.
//
try {
Metadata metadata = JpegMetadataReader.readMetadata(file);
print(metadata, "Using JpegMetadataReader");
} catch (JpegProcessingException e) {
print(e);
} catch (IOException e) {
print(e);
}
//
// APPROACH 3: SPECIFIC METADATA TYPE
//
// If you only wish to read a subset of the supported metadata types, you can do this by
// passing the set of readers to use.
//
// This currently only applies to JPEG file processing.
//
try {
// We are only interested in handling
Iterable<JpegSegmentMetadataReader> readers = Arrays.asList(new ExifReader(), new IptcReader());
Metadata metadata = JpegMetadataReader.readMetadata(file, readers);
print(metadata, "Using JpegMetadataReader for Exif and IPTC only");
} catch (JpegProcessingException e) {
print(e);
} catch (IOException e) {
print(e);
}
}
/**
* Write all extracted values to stdout.
*/
private static void print(Metadata metadata, String method)
{
System.out.println();
System.out.println("-------------------------------------------------");
System.out.print(' ');
System.out.print(method);
System.out.println("-------------------------------------------------");
System.out.println();
//
// A Metadata object contains multiple Directory objects
//
for (Directory directory : metadata.getDirectories()) {
//
// Each Directory stores values in Tag objects
//
for (Tag tag : directory.getTags()) {
System.out.println(tag);
}
//
// Each Directory may also contain error messages
//
for (String error : directory.getErrors()) {
System.err.println("ERROR: " + error);
}
}
}
private static void print(Exception exception)
{
System.err.println("EXCEPTION: " + exception);
}
}
|