]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/jpeg/ExternalExifLibrary.java
Version 11, August 2010
[GpsPrune.git] / tim / prune / jpeg / ExternalExifLibrary.java
1 package tim.prune.jpeg;
2
3 import java.io.File;
4
5 import com.drew.lang.Rational;
6 import com.drew.metadata.Directory;
7 import com.drew.metadata.Metadata;
8 import com.drew.metadata.exif.ExifDirectory;
9 import com.drew.metadata.exif.ExifReader;
10 import com.drew.metadata.exif.GpsDirectory;
11
12 /**
13  * Class to act as a gateway into the external exif library functions.
14  * This should be the only class with dependence on the lib-metadata-extractor-java
15  * classes (which are NOT delivered with Prune).
16  * This class will not compile without this extra dependency (but is not required if
17  * the ExifGateway uses the InternalExifLibrary instead).
18  * Should not be included if the internal library will be used (from jpeg.drew package).
19  */
20 public class ExternalExifLibrary implements ExifLibrary
21 {
22         /**
23          * Use the _external_ exif library to get the data from the given file
24          * @param inFile file to access
25          * @return Jpeg data if available, otherwise null
26          */
27         public JpegData getJpegData(File inFile)
28         {
29                 JpegData data = new JpegData();
30                 // Read exif data from picture
31                 try
32                 {
33                         Metadata metadata = new Metadata();
34                         new ExifReader(inFile).extract(metadata);
35                         if (metadata.containsDirectory(GpsDirectory.class))
36                         {
37                                 Directory gpsdir = metadata.getDirectory(GpsDirectory.class);
38                                 if (gpsdir.containsTag(GpsDirectory.TAG_GPS_LATITUDE)
39                                         && gpsdir.containsTag(GpsDirectory.TAG_GPS_LONGITUDE)
40                                         && gpsdir.containsTag(GpsDirectory.TAG_GPS_LATITUDE_REF)
41                                         && gpsdir.containsTag(GpsDirectory.TAG_GPS_LONGITUDE_REF))
42                                 {
43                                         data.setLatitudeRef(gpsdir.getString(GpsDirectory.TAG_GPS_LATITUDE_REF));
44                                         Rational[] latRats = gpsdir.getRationalArray(GpsDirectory.TAG_GPS_LATITUDE);
45                                         data.setLatitude(new double[] {latRats[0].doubleValue(),
46                                                 latRats[1].doubleValue(), latRats[2].doubleValue()});
47                                         data.setLongitudeRef(gpsdir.getString(GpsDirectory.TAG_GPS_LONGITUDE_REF));
48                                         Rational[] lonRats = gpsdir.getRationalArray(GpsDirectory.TAG_GPS_LONGITUDE);
49                                         data.setLongitude(new double[] {lonRats[0].doubleValue(),
50                                                 lonRats[1].doubleValue(), lonRats[2].doubleValue()});
51                                 }
52
53                                 // Altitude (if present)
54                                 if (gpsdir.containsTag(GpsDirectory.TAG_GPS_ALTITUDE) && gpsdir.containsTag(GpsDirectory.TAG_GPS_ALTITUDE_REF))
55                                 {
56                                         data.setAltitude(gpsdir.getRational(GpsDirectory.TAG_GPS_ALTITUDE).intValue());
57                                         byte altRef = (byte) gpsdir.getInt(GpsDirectory.TAG_GPS_ALTITUDE_REF);
58                                         data.setAltitudeRef(altRef);
59                                 }
60
61                                 // Timestamp and datestamp (if present)
62                                 final int TAG_GPS_DATESTAMP = 0x001d;
63                                 if (gpsdir.containsTag(GpsDirectory.TAG_GPS_TIME_STAMP) && gpsdir.containsTag(TAG_GPS_DATESTAMP))
64                                 {
65                                         Rational[] times = gpsdir.getRationalArray(GpsDirectory.TAG_GPS_TIME_STAMP);
66                                         data.setGpsTimestamp(new int[] {times[0].intValue(), times[1].intValue(),
67                                                 times[2].intValue()});
68                                         Rational[] dates = gpsdir.getRationalArray(TAG_GPS_DATESTAMP);
69                                         if (dates != null) {
70                                                 data.setGpsDatestamp(new int[] {dates[0].intValue(), dates[1].intValue(), dates[2].intValue()});
71                                         }
72                                 }
73                         }
74
75                         // Tags from Exif directory
76                         if (metadata.containsDirectory(ExifDirectory.class))
77                         {
78                                 Directory exifdir = metadata.getDirectory(ExifDirectory.class);
79
80                                 // Take time and date from exif tags
81                                 if (exifdir.containsTag(ExifDirectory.TAG_DATETIME_ORIGINAL)) {
82                                         data.setOriginalTimestamp(exifdir.getString(ExifDirectory.TAG_DATETIME_ORIGINAL));
83                                 }
84                                 // Also take "digitized" timestamp
85                                 if (exifdir.containsTag(ExifDirectory.TAG_DATETIME_DIGITIZED)) {
86                                         data.setDigitizedTimestamp(exifdir.getString(ExifDirectory.TAG_DATETIME_DIGITIZED));
87                                 }
88
89                                 // Photo rotation code
90                                 if (exifdir.containsTag(ExifDirectory.TAG_ORIENTATION)) {
91                                         data.setOrientationCode(exifdir.getInt(ExifDirectory.TAG_ORIENTATION));
92                                         // NOTE: this presumably takes the _last_ orientation value found, not the first.
93                                 }
94
95                                 // Thumbnail
96                                 if (exifdir.containsTag(ExifDirectory.TAG_THUMBNAIL_DATA))
97                                 {
98                                         // Make a copy of the byte data rather than keeping a reference to extracted array
99                                         byte[] tdata = exifdir.getByteArray(ExifDirectory.TAG_THUMBNAIL_DATA);
100                                         byte[] thumb = new byte[tdata.length];
101                                         System.arraycopy(tdata, 0, thumb, 0, tdata.length);
102                                         data.setThumbnailImage(thumb);
103                                 }
104                         }
105
106                 }
107                 catch (Exception e) {
108                         // Exception reading metadata, just ignore it
109                 }
110                 return data;
111         }
112
113         /**
114          * Check whether the exifreader class can be correctly resolved
115          * @return true if it looks ok
116          */
117         public boolean looksOK()
118         {
119                 try {
120                         String test = ExifReader.class.getName();
121                         if (test != null) return true;
122                 }
123                 catch (LinkageError le) {}
124                 return false;
125         }
126 }