Changeset 6209 in josm for trunk/src/org/openstreetmap/josm/tools
- Timestamp:
- 2013-08-30T11:36:28+02:00 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/ExifReader.java
r6142 r6209 7 7 import java.util.Date; 8 8 9 import org.openstreetmap.josm.data.coor.LatLon; 10 9 11 import com.drew.imaging.jpeg.JpegMetadataReader; 10 12 import com.drew.imaging.jpeg.JpegProcessingException; 13 import com.drew.lang.Rational; 11 14 import com.drew.metadata.Directory; 12 15 import com.drew.metadata.Metadata; … … 15 18 import com.drew.metadata.exif.ExifIFD0Directory; 16 19 import com.drew.metadata.exif.ExifSubIFDDirectory; 20 import com.drew.metadata.exif.GpsDirectory; 17 21 18 22 /** 19 * Read out exif fileinformation from ajpegfile23 * Read out EXIF information from a JPEG file 20 24 * @author Imi 25 * @since 99 21 26 */ 22 27 public class ExifReader { 23 28 24 @SuppressWarnings("unchecked") public static Date readTime(File filename) throws ParseException { 29 /** 30 * Returns the date/time from the given JPEG file. 31 * @param filename The JPEG file to read 32 * @return The date/time read in the EXIF section, or {@code null} if not found 33 * @throws ParseException if {@link DateParser#parse} fails to parse date/time 34 */ 35 public static Date readTime(File filename) throws ParseException { 25 36 try { 26 37 Metadata metadata = JpegMetadataReader.readMetadata(filename); … … 51 62 } 52 63 53 public static Integer readOrientation(File filename) throws ParseException { 54 Integer orientation = null; 64 /** 65 * Returns the image orientation of the given JPEG file. 66 * @param filename The JPEG file to read 67 * @return The image orientation as an {@code int}. Default value is 1. Possible values are listed in EXIF spec as follows:<br> 68 * <ul>1. The 0th row is at the visual top of the image, and the 0th column is the visual left-hand side.</ul> 69 * <ul>2. The 0th row is at the visual top of the image, and the 0th column is the visual right-hand side.</ul> 70 * <ul>3. The 0th row is at the visual bottom of the image, and the 0th column is the visual right-hand side.</ul> 71 * <ul>4. The 0th row is at the visual bottom of the image, and the 0th column is the visual left-hand side.</ul> 72 * <ul>5. The 0th row is the visual left-hand side of the image, and the 0th column is the visual top.</ul> 73 * <ul>6. The 0th row is the visual right-hand side of the image, and the 0th column is the visual top.</ul> 74 * <ul>7. The 0th row is the visual right-hand side of the image, and the 0th column is the visual bottom.</ul> 75 * <ul>8. The 0th row is the visual left-hand side of the image, and the 0th column is the visual bottom.</ul> 76 * @see <a href="http://www.impulseadventure.com/photo/exif-orientation.html">http://www.impulseadventure.com/photo/exif-orientation.html</a> 77 * @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> 78 */ 79 public static Integer readOrientation(File filename) { 55 80 try { 56 81 final Metadata metadata = JpegMetadataReader.readMetadata(filename); 57 82 final Directory dir = metadata.getDirectory(ExifIFD0Directory.class); 58 orientation =dir.getInt(ExifIFD0Directory.TAG_ORIENTATION);83 return dir.getInt(ExifIFD0Directory.TAG_ORIENTATION); 59 84 } catch (JpegProcessingException e) { 60 85 e.printStackTrace(); … … 64 89 e.printStackTrace(); 65 90 } 66 return orientation;91 return null; 67 92 } 68 93 94 /** 95 * Returns the geolocation of the given JPEG file. 96 * @param filename The JPEG file to read 97 * @return The lat/lon read in the EXIF section, or {@code null} if not found 98 * @since 6209 99 */ 100 public static LatLon readLatLon(File filename) { 101 try { 102 final Metadata metadata = JpegMetadataReader.readMetadata(filename); 103 final GpsDirectory dirGps = metadata.getDirectory(GpsDirectory.class); 104 return readLatLon(dirGps); 105 } catch (JpegProcessingException e) { 106 e.printStackTrace(); 107 } catch (IOException e) { 108 e.printStackTrace(); 109 } catch (MetadataException e) { 110 e.printStackTrace(); 111 } 112 return null; 113 } 114 115 /** 116 * Returns the geolocation of the given EXIF GPS directory. 117 * @param dirGps The EXIF GPS directory 118 * @return The lat/lon read in the EXIF section, or {@code null} if {@code dirGps} is null 119 * @throws MetadataException 120 * @since 6209 121 */ 122 public static LatLon readLatLon(GpsDirectory dirGps) throws MetadataException { 123 if (dirGps != null) { 124 double lat = readAxis(dirGps, GpsDirectory.TAG_GPS_LATITUDE, GpsDirectory.TAG_GPS_LATITUDE_REF, 'S'); 125 double lon = readAxis(dirGps, GpsDirectory.TAG_GPS_LONGITUDE, GpsDirectory.TAG_GPS_LONGITUDE_REF, 'W'); 126 return new LatLon(lat, lon); 127 } 128 return null; 129 } 130 131 /** 132 * Returns the direction of the given JPEG file. 133 * @param filename The JPEG file to read 134 * @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 135 * @since 6209 136 */ 137 public static Double readDirection(File filename) { 138 try { 139 final Metadata metadata = JpegMetadataReader.readMetadata(filename); 140 final GpsDirectory dirGps = metadata.getDirectory(GpsDirectory.class); 141 return readDirection(dirGps); 142 } catch (JpegProcessingException e) { 143 e.printStackTrace(); 144 } catch (IOException e) { 145 e.printStackTrace(); 146 } 147 return null; 148 } 149 150 /** 151 * Returns the direction of the given EXIF GPS directory. 152 * @param dirGps The EXIF GPS directory 153 * @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 154 * @since 6209 155 */ 156 public static Double readDirection(GpsDirectory dirGps) { 157 if (dirGps != null) { 158 Rational direction = dirGps.getRational(GpsDirectory.TAG_GPS_IMG_DIRECTION); 159 if (direction != null) { 160 return direction.doubleValue(); 161 } 162 } 163 return null; 164 } 165 166 private static double readAxis(GpsDirectory dirGps, int gpsTag, int gpsTagRef, char cRef) throws MetadataException { 167 double value; 168 Rational[] components = dirGps.getRationalArray(gpsTag); 169 if (components != null) { 170 double deg = components[0].doubleValue(); 171 double min = components[1].doubleValue(); 172 double sec = components[2].doubleValue(); 173 174 if (Double.isNaN(deg) && Double.isNaN(min) && Double.isNaN(sec)) 175 throw new IllegalArgumentException(); 176 177 value = (Double.isNaN(deg) ? 0 : deg + (Double.isNaN(min) ? 0 : (min / 60)) + (Double.isNaN(sec) ? 0 : (sec / 3600))); 178 179 if (dirGps.getString(gpsTagRef).charAt(0) == cRef) { 180 value = -value; 181 } 182 } else { 183 // Try to read lon/lat as double value (Nonstandard, created by some cameras -> #5220) 184 value = dirGps.getDouble(gpsTag); 185 } 186 return value; 187 } 69 188 }
Note:
See TracChangeset
for help on using the changeset viewer.
