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.ImageReadException;
24  import org.apache.commons.imaging.Imaging;
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 ImageReadException,
38              IOException {
39          // get all metadata stored in EXIF format (ie. from JPEG or TIFF).
40          final ImageMetadata metadata = Imaging.getMetadata(file);
41  
42          // System.out.println(metadata);
43  
44          if (metadata instanceof JpegImageMetadata) {
45              final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
46  
47              // Jpeg EXIF metadata is stored in a TIFF-based directory structure
48              // and is identified with TIFF tags.
49              // Here we look for the "x resolution" tag, but
50              // we could just as easily search for any other tag.
51              //
52              // see the TiffConstants file for a list of TIFF tags.
53  
54              System.out.println("file: " + file.getPath());
55  
56              // print out various interesting EXIF tags.
57              printTagValue(jpegMetadata, TiffTagConstants.TIFF_TAG_XRESOLUTION);
58              printTagValue(jpegMetadata, TiffTagConstants.TIFF_TAG_DATE_TIME);
59              printTagValue(jpegMetadata,
60                      ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL);
61              printTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_DATE_TIME_DIGITIZED);
62              printTagValue(jpegMetadata, ExifTagConstants.EXIF_TAG_ISO);
63              printTagValue(jpegMetadata,
64                      ExifTagConstants.EXIF_TAG_SHUTTER_SPEED_VALUE);
65              printTagValue(jpegMetadata,
66                      ExifTagConstants.EXIF_TAG_APERTURE_VALUE);
67              printTagValue(jpegMetadata,
68                      ExifTagConstants.EXIF_TAG_BRIGHTNESS_VALUE);
69              printTagValue(jpegMetadata,
70                      GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF);
71              printTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LATITUDE);
72              printTagValue(jpegMetadata,
73                      GpsTagConstants.GPS_TAG_GPS_LONGITUDE_REF);
74              printTagValue(jpegMetadata, GpsTagConstants.GPS_TAG_GPS_LONGITUDE);
75  
76              System.out.println();
77  
78              // simple interface to GPS data
79              final TiffImageMetadata exifMetadata = jpegMetadata.getExif();
80              if (null != exifMetadata) {
81                  final TiffImageMetadata.GPSInfo gpsInfo = exifMetadata.getGPS();
82                  if (null != gpsInfo) {
83                      final String gpsDescription = gpsInfo.toString();
84                      final double longitude = gpsInfo.getLongitudeAsDegreesEast();
85                      final double latitude = gpsInfo.getLatitudeAsDegreesNorth();
86  
87                      System.out.println("    " + "GPS Description: "
88                              + gpsDescription);
89                      System.out.println("    "
90                              + "GPS Longitude (Degrees East): " + longitude);
91                      System.out.println("    "
92                              + "GPS Latitude (Degrees North): " + latitude);
93                  }
94              }
95  
96              // more specific example of how to manually access GPS values
97              final TiffField gpsLatitudeRefField = jpegMetadata.findEXIFValueWithExactMatch(
98                      GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF);
99              final TiffField gpsLatitudeField = jpegMetadata.findEXIFValueWithExactMatch(
100                     GpsTagConstants.GPS_TAG_GPS_LATITUDE);
101             final TiffField gpsLongitudeRefField = jpegMetadata.findEXIFValueWithExactMatch(
102                     GpsTagConstants.GPS_TAG_GPS_LONGITUDE_REF);
103             final TiffField gpsLongitudeField = jpegMetadata.findEXIFValueWithExactMatch(
104                     GpsTagConstants.GPS_TAG_GPS_LONGITUDE);
105             if (gpsLatitudeRefField != null && gpsLatitudeField != null &&
106                     gpsLongitudeRefField != null &&
107                     gpsLongitudeField != null) {
108                 // all of these values are strings.
109                 final String gpsLatitudeRef = (String) gpsLatitudeRefField.getValue();
110                 final RationalNumber gpsLatitude[] = (RationalNumber[]) (gpsLatitudeField.getValue());
111                 final String gpsLongitudeRef = (String) gpsLongitudeRefField.getValue();
112                 final RationalNumber gpsLongitude[] = (RationalNumber[]) gpsLongitudeField.getValue();
113 
114                 final RationalNumber gpsLatitudeDegrees = gpsLatitude[0];
115                 final RationalNumber gpsLatitudeMinutes = gpsLatitude[1];
116                 final RationalNumber gpsLatitudeSeconds = gpsLatitude[2];
117 
118                 final RationalNumber gpsLongitudeDegrees = gpsLongitude[0];
119                 final RationalNumber gpsLongitudeMinutes = gpsLongitude[1];
120                 final RationalNumber gpsLongitudeSeconds = gpsLongitude[2];
121 
122                 // This will format the gps info like so:
123                 //
124                 // gpsLatitude: 8 degrees, 40 minutes, 42.2 seconds S
125                 // gpsLongitude: 115 degrees, 26 minutes, 21.8 seconds E
126 
127                 System.out.println("    " + "GPS Latitude: "
128                         + gpsLatitudeDegrees.toDisplayString() + " degrees, "
129                         + gpsLatitudeMinutes.toDisplayString() + " minutes, "
130                         + gpsLatitudeSeconds.toDisplayString() + " seconds "
131                         + gpsLatitudeRef);
132                 System.out.println("    " + "GPS Longitude: "
133                         + gpsLongitudeDegrees.toDisplayString() + " degrees, "
134                         + gpsLongitudeMinutes.toDisplayString() + " minutes, "
135                         + gpsLongitudeSeconds.toDisplayString() + " seconds "
136                         + gpsLongitudeRef);
137 
138             }
139 
140             System.out.println();
141 
142             final List<ImageMetadataItem> items = jpegMetadata.getItems();
143             for (int i = 0; i < items.size(); i++) {
144                 final ImageMetadataItem item = items.get(i);
145                 System.out.println("    " + "item: " + item);
146             }
147 
148             System.out.println();
149         }
150     }
151 
152     private static void printTagValue(final JpegImageMetadata jpegMetadata,
153             final TagInfo tagInfo) {
154         final TiffField field = jpegMetadata.findEXIFValueWithExactMatch(tagInfo);
155         if (field == null) {
156             System.out.println(tagInfo.name + ": " + "Not Found.");
157         } else {
158             System.out.println(tagInfo.name + ": "
159                     + field.getValueDescription());
160         }
161     }
162 
163 }