View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.imaging.examples;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.util.List;
22  
23  import org.apache.commons.imaging.Imaging;
24  import org.apache.commons.imaging.ImagingException;
25  import org.apache.commons.imaging.common.ImageMetadata;
26  import org.apache.commons.imaging.common.ImageMetadata.ImageMetadataItem;
27  import org.apache.commons.imaging.common.RationalNumber;
28  import org.apache.commons.imaging.formats.jpeg.JpegImageMetadata;
29  import org.apache.commons.imaging.formats.tiff.TiffField;
30  import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
31  import org.apache.commons.imaging.formats.tiff.constants.ExifTagConstants;
32  import org.apache.commons.imaging.formats.tiff.constants.GpsTagConstants;
33  import org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants;
34  import org.apache.commons.imaging.formats.tiff.taginfos.TagInfo;
35  
36  public class MetadataExample {
37      public static void metadataExample(final File file) throws ImagingException, IOException {
38          // get all metadata stored in EXIF format (ie. from JPEG or TIFF).
39          final ImageMetadata metadata = Imaging.getMetadata(file);
40  
41          // System.out.println(metadata);
42  
43          if (metadata instanceof JpegImageMetadata) {
44              final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
45  
46              // Jpeg EXIF metadata is stored in a TIFF-based directory structure
47              // and is identified with TIFF tags.
48              // Here we look for the "x resolution" tag, but
49              // we could just as easily search for any other tag.
50              //
51              // see the TiffConstants file for a list of TIFF tags.
52  
53              System.out.println("file: " + file.getPath());
54  
55              // print out various interesting EXIF tags.
56              printTagValue(jpegMetadata, TiffTagConstants.TIFF_TAG_XRESOLUTION);
57              printTagValue(jpegMetadata, TiffTagConstants.TIFF_TAG_DATE_TIME);
58              printTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL);
59              printTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_DATE_TIME_DIGITIZED);
60              printTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_ISO);
61              printTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_SHUTTER_SPEED_VALUE);
62              printTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_APERTURE_VALUE);
63              printTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_BRIGHTNESS_VALUE);
64              printTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF);
65              printTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LATITUDE);
66              printTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LONGITUDE_REF);
67              printTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LONGITUDE);
68  
69              System.out.println();
70  
71              // simple interface to GPS data
72              final TiffImageMetadata exifMetadata = jpegMetadata.getExif();
73              if (null != exifMetadata) {
74                  final TiffImageMetadata.GpsInfo gpsInfo = exifMetadata.getGpsInfo();
75                  if (null != gpsInfo) {
76                      final String gpsDescription = gpsInfo.toString();
77                      final double longitude = gpsInfo.getLongitudeAsDegreesEast();
78                      final double latitude = gpsInfo.getLatitudeAsDegreesNorth();
79  
80                      System.out.println("    " + "GPS Description: " + gpsDescription);
81                      System.out.println("    " + "GPS Longitude (Degrees East): " + longitude);
82                      System.out.println("    " + "GPS Latitude (Degrees North): " + latitude);
83                  }
84              }
85  
86              // more specific example of how to manually access GPS values
87              final TiffField gpsLatitudeRefField = jpegMetadata.findExifValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF);
88              final TiffField gpsLatitudeField = jpegMetadata.findExifValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LATITUDE);
89              final TiffField gpsLongitudeRefField = jpegMetadata.findExifValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LONGITUDE_REF);
90              final TiffField gpsLongitudeField = jpegMetadata.findExifValueWithExactMatch(GpsTagConstants.GPS_TAG_GPS_LONGITUDE);
91              if (gpsLatitudeRefField != null && gpsLatitudeField != null && gpsLongitudeRefField != null && gpsLongitudeField != null) {
92                  // all of these values are strings.
93                  final String gpsLatitudeRef = (String) gpsLatitudeRefField.getValue();
94                  final RationalNumber[] gpsLatitude = (RationalNumber[]) gpsLatitudeField.getValue();
95                  final String gpsLongitudeRef = (String) gpsLongitudeRefField.getValue();
96                  final RationalNumber[] gpsLongitude = (RationalNumber[]) gpsLongitudeField.getValue();
97  
98                  final RationalNumber gpsLatitudeDegrees = gpsLatitude[0];
99                  final RationalNumber gpsLatitudeMinutes = gpsLatitude[1];
100                 final RationalNumber gpsLatitudeSeconds = gpsLatitude[2];
101 
102                 final RationalNumber gpsLongitudeDegrees = gpsLongitude[0];
103                 final RationalNumber gpsLongitudeMinutes = gpsLongitude[1];
104                 final RationalNumber gpsLongitudeSeconds = gpsLongitude[2];
105 
106                 // This will format the gps info like so:
107                 //
108                 // gpsLatitude: 8 degrees, 40 minutes, 42.2 seconds S
109                 // gpsLongitude: 115 degrees, 26 minutes, 21.8 seconds E
110 
111                 System.out.println("    " + "GPS Latitude: " + gpsLatitudeDegrees.toDisplayString() + " degrees, " + gpsLatitudeMinutes.toDisplayString()
112                         + " minutes, " + gpsLatitudeSeconds.toDisplayString() + " seconds " + gpsLatitudeRef);
113                 System.out.println("    " + "GPS Longitude: " + gpsLongitudeDegrees.toDisplayString() + " degrees, " + gpsLongitudeMinutes.toDisplayString()
114                         + " minutes, " + gpsLongitudeSeconds.toDisplayString() + " seconds " + gpsLongitudeRef);
115 
116             }
117 
118             System.out.println();
119 
120             final List<ImageMetadataItem> items = jpegMetadata.getItems();
121             for (final ImageMetadataItem item : items) {
122                 System.out.println("    " + "item: " + item);
123             }
124 
125             System.out.println();
126         }
127     }
128 
129     private static void printTagValue(final JpegImageMetadata jpegMetadata, final TagInfo tagInfo) {
130         final TiffField field = jpegMetadata.findExifValueWithExactMatch(tagInfo);
131         if (field == null) {
132             System.out.println(tagInfo.name + ": " + "Not Found.");
133         } else {
134             System.out.println(tagInfo.name + ": " + field.getValueDescription());
135         }
136     }
137 
138 }