source: josm/trunk/src/org/openstreetmap/josm/tools/ExifReader.java@ 6869

Last change on this file since 6869 was 6830, checked in by Don-vip, 10 years ago

javadoc fixes for jdk8 compatibility

  • Property svn:eol-style set to native
File size: 8.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.io.File;
5import java.io.IOException;
6import java.text.ParseException;
7import java.util.Date;
8
9import org.openstreetmap.josm.Main;
10import org.openstreetmap.josm.data.coor.LatLon;
11
12import com.drew.imaging.jpeg.JpegMetadataReader;
13import com.drew.imaging.jpeg.JpegProcessingException;
14import com.drew.lang.Rational;
15import com.drew.metadata.Directory;
16import com.drew.metadata.Metadata;
17import com.drew.metadata.MetadataException;
18import com.drew.metadata.Tag;
19import com.drew.metadata.exif.ExifIFD0Directory;
20import com.drew.metadata.exif.ExifSubIFDDirectory;
21import com.drew.metadata.exif.GpsDirectory;
22
23/**
24 * Read out EXIF information from a JPEG file
25 * @author Imi
26 * @since 99
27 */
28public final class ExifReader {
29
30 private ExifReader() {
31 // Hide default constructor for utils classes
32 }
33
34 /**
35 * Returns the date/time from the given JPEG file.
36 * @param filename The JPEG file to read
37 * @return The date/time read in the EXIF section, or {@code null} if not found
38 * @throws ParseException if {@link DateParser#parse} fails to parse date/time
39 */
40 public static Date readTime(File filename) throws ParseException {
41 try {
42 Metadata metadata = JpegMetadataReader.readMetadata(filename);
43 String dateStr = null;
44 OUTER:
45 for (Directory dirIt : metadata.getDirectories()) {
46 for (Tag tag : dirIt.getTags()) {
47 if (tag.getTagType() == ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL /* 0x9003 */) {
48 dateStr = tag.getDescription();
49 break OUTER; // prefer this tag
50 }
51 if (tag.getTagType() == ExifIFD0Directory.TAG_DATETIME /* 0x0132 */ ||
52 tag.getTagType() == ExifSubIFDDirectory.TAG_DATETIME_DIGITIZED /* 0x9004 */) {
53 dateStr = tag.getDescription();
54 }
55 }
56 }
57 if (dateStr != null) {
58 dateStr = dateStr.replace('/', ':'); // workaround for HTC Sensation bug, see #7228
59 return DateParser.parse(dateStr);
60 }
61 } catch (ParseException e) {
62 throw e;
63 } catch (Exception e) {
64 Main.error(e);
65 }
66 return null;
67 }
68
69 /**
70 * Returns the image orientation of the given JPEG file.
71 * @param filename The JPEG file to read
72 * @return The image orientation as an {@code int}. Default value is 1. Possible values are listed in EXIF spec as follows:<br><ol>
73 * <li>The 0th row is at the visual top of the image, and the 0th column is the visual left-hand side.</li>
74 * <li>The 0th row is at the visual top of the image, and the 0th column is the visual right-hand side.</li>
75 * <li>The 0th row is at the visual bottom of the image, and the 0th column is the visual right-hand side.</li>
76 * <li>The 0th row is at the visual bottom of the image, and the 0th column is the visual left-hand side.</li>
77 * <li>The 0th row is the visual left-hand side of the image, and the 0th column is the visual top.</li>
78 * <li>The 0th row is the visual right-hand side of the image, and the 0th column is the visual top.</li>
79 * <li>The 0th row is the visual right-hand side of the image, and the 0th column is the visual bottom.</li>
80 * <li>The 0th row is the visual left-hand side of the image, and the 0th column is the visual bottom.</li></ol>
81 * @see <a href="http://www.impulseadventure.com/photo/exif-orientation.html">http://www.impulseadventure.com/photo/exif-orientation.html</a>
82 * @see <a href="http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto">http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto</a>
83 */
84 public static Integer readOrientation(File filename) {
85 try {
86 final Metadata metadata = JpegMetadataReader.readMetadata(filename);
87 final Directory dir = metadata.getDirectory(ExifIFD0Directory.class);
88 return dir.getInt(ExifIFD0Directory.TAG_ORIENTATION);
89 } catch (JpegProcessingException e) {
90 Main.error(e);
91 } catch (MetadataException e) {
92 Main.error(e);
93 } catch (IOException e) {
94 Main.error(e);
95 }
96 return null;
97 }
98
99 /**
100 * Returns the geolocation of the given JPEG file.
101 * @param filename The JPEG file to read
102 * @return The lat/lon read in the EXIF section, or {@code null} if not found
103 * @since 6209
104 */
105 public static LatLon readLatLon(File filename) {
106 try {
107 final Metadata metadata = JpegMetadataReader.readMetadata(filename);
108 final GpsDirectory dirGps = metadata.getDirectory(GpsDirectory.class);
109 return readLatLon(dirGps);
110 } catch (JpegProcessingException e) {
111 Main.error(e);
112 } catch (IOException e) {
113 Main.error(e);
114 } catch (MetadataException e) {
115 Main.error(e);
116 }
117 return null;
118 }
119
120 /**
121 * Returns the geolocation of the given EXIF GPS directory.
122 * @param dirGps The EXIF GPS directory
123 * @return The lat/lon read in the EXIF section, or {@code null} if {@code dirGps} is null
124 * @throws MetadataException
125 * @since 6209
126 */
127 public static LatLon readLatLon(GpsDirectory dirGps) throws MetadataException {
128 if (dirGps != null) {
129 double lat = readAxis(dirGps, GpsDirectory.TAG_GPS_LATITUDE, GpsDirectory.TAG_GPS_LATITUDE_REF, 'S');
130 double lon = readAxis(dirGps, GpsDirectory.TAG_GPS_LONGITUDE, GpsDirectory.TAG_GPS_LONGITUDE_REF, 'W');
131 return new LatLon(lat, lon);
132 }
133 return null;
134 }
135
136 /**
137 * Returns the direction of the given JPEG file.
138 * @param filename The JPEG file to read
139 * @return The direction of the image when it was captures (in degrees between 0.0 and 359.99), or {@code null} if missing or if {@code dirGps} is null
140 * @since 6209
141 */
142 public static Double readDirection(File filename) {
143 try {
144 final Metadata metadata = JpegMetadataReader.readMetadata(filename);
145 final GpsDirectory dirGps = metadata.getDirectory(GpsDirectory.class);
146 return readDirection(dirGps);
147 } catch (JpegProcessingException e) {
148 Main.error(e);
149 } catch (IOException e) {
150 Main.error(e);
151 }
152 return null;
153 }
154
155 /**
156 * Returns the direction of the given EXIF GPS directory.
157 * @param dirGps The EXIF GPS directory
158 * @return The direction of the image when it was captures (in degrees between 0.0 and 359.99), or {@code null} if missing or if {@code dirGps} is null
159 * @since 6209
160 */
161 public static Double readDirection(GpsDirectory dirGps) {
162 if (dirGps != null) {
163 Rational direction = dirGps.getRational(GpsDirectory.TAG_GPS_IMG_DIRECTION);
164 if (direction != null) {
165 return direction.doubleValue();
166 }
167 }
168 return null;
169 }
170
171 private static double readAxis(GpsDirectory dirGps, int gpsTag, int gpsTagRef, char cRef) throws MetadataException {
172 double value;
173 Rational[] components = dirGps.getRationalArray(gpsTag);
174 if (components != null) {
175 double deg = components[0].doubleValue();
176 double min = components[1].doubleValue();
177 double sec = components[2].doubleValue();
178
179 if (Double.isNaN(deg) && Double.isNaN(min) && Double.isNaN(sec))
180 throw new IllegalArgumentException();
181
182 value = (Double.isNaN(deg) ? 0 : deg + (Double.isNaN(min) ? 0 : (min / 60)) + (Double.isNaN(sec) ? 0 : (sec / 3600)));
183
184 if (dirGps.getString(gpsTagRef).charAt(0) == cRef) {
185 value = -value;
186 }
187 } else {
188 // Try to read lon/lat as double value (Nonstandard, created by some cameras -> #5220)
189 value = dirGps.getDouble(gpsTag);
190 }
191 return value;
192 }
193}
Note: See TracBrowser for help on using the repository browser.