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

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

see #8465 - use multi-catch where applicable

  • Property svn:eol-style set to native
File size: 7.9 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 | MetadataException | IOException e) {
90 Main.error(e);
91 }
92 return null;
93 }
94
95 /**
96 * Returns the geolocation of the given JPEG file.
97 * @param filename The JPEG file to read
98 * @return The lat/lon read in the EXIF section, or {@code null} if not found
99 * @since 6209
100 */
101 public static LatLon readLatLon(File filename) {
102 try {
103 final Metadata metadata = JpegMetadataReader.readMetadata(filename);
104 final GpsDirectory dirGps = metadata.getDirectory(GpsDirectory.class);
105 return readLatLon(dirGps);
106 } catch (JpegProcessingException e) {
107 Main.error(e);
108 } catch (IOException e) {
109 Main.error(e);
110 } catch (MetadataException e) {
111 Main.error(e);
112 }
113 return null;
114 }
115
116 /**
117 * Returns the geolocation of the given EXIF GPS directory.
118 * @param dirGps The EXIF GPS directory
119 * @return The lat/lon read in the EXIF section, or {@code null} if {@code dirGps} is null
120 * @throws MetadataException
121 * @since 6209
122 */
123 public static LatLon readLatLon(GpsDirectory dirGps) throws MetadataException {
124 if (dirGps != null) {
125 double lat = readAxis(dirGps, GpsDirectory.TAG_GPS_LATITUDE, GpsDirectory.TAG_GPS_LATITUDE_REF, 'S');
126 double lon = readAxis(dirGps, GpsDirectory.TAG_GPS_LONGITUDE, GpsDirectory.TAG_GPS_LONGITUDE_REF, 'W');
127 return new LatLon(lat, lon);
128 }
129 return null;
130 }
131
132 /**
133 * Returns the direction of the given JPEG file.
134 * @param filename The JPEG file to read
135 * @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
136 * @since 6209
137 */
138 public static Double readDirection(File filename) {
139 try {
140 final Metadata metadata = JpegMetadataReader.readMetadata(filename);
141 final GpsDirectory dirGps = metadata.getDirectory(GpsDirectory.class);
142 return readDirection(dirGps);
143 } catch (JpegProcessingException e) {
144 Main.error(e);
145 } catch (IOException e) {
146 Main.error(e);
147 }
148 return null;
149 }
150
151 /**
152 * Returns the direction of the given EXIF GPS directory.
153 * @param dirGps The EXIF GPS directory
154 * @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
155 * @since 6209
156 */
157 public static Double readDirection(GpsDirectory dirGps) {
158 if (dirGps != null) {
159 Rational direction = dirGps.getRational(GpsDirectory.TAG_GPS_IMG_DIRECTION);
160 if (direction != null) {
161 return direction.doubleValue();
162 }
163 }
164 return null;
165 }
166
167 private static double readAxis(GpsDirectory dirGps, int gpsTag, int gpsTagRef, char cRef) throws MetadataException {
168 double value;
169 Rational[] components = dirGps.getRationalArray(gpsTag);
170 if (components != null) {
171 double deg = components[0].doubleValue();
172 double min = components[1].doubleValue();
173 double sec = components[2].doubleValue();
174
175 if (Double.isNaN(deg) && Double.isNaN(min) && Double.isNaN(sec))
176 throw new IllegalArgumentException();
177
178 value = (Double.isNaN(deg) ? 0 : deg + (Double.isNaN(min) ? 0 : (min / 60)) + (Double.isNaN(sec) ? 0 : (sec / 3600)));
179
180 if (dirGps.getString(gpsTagRef).charAt(0) == cRef) {
181 value = -value;
182 }
183 } else {
184 // Try to read lon/lat as double value (Nonstandard, created by some cameras -> #5220)
185 value = dirGps.getDouble(gpsTag);
186 }
187 return value;
188 }
189}
Note: See TracBrowser for help on using the repository browser.