]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/jpeg/ExternalExifLibrary.java
b9cd1fdadf33bb739868d98c716c8d8b4d44fc16
[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                                         data.setGpsDatestamp(new int[] {dates[0].intValue(), dates[1].intValue(), dates[2].intValue()});
70                                 }
71                         }
72
73                         // Tags from Exif directory
74                         if (metadata.containsDirectory(ExifDirectory.class))
75                         {
76                                 Directory exifdir = metadata.getDirectory(ExifDirectory.class);
77
78                                 // Take time and date from exif tags if haven't got it already from GPS
79                                 if (data.getGpsDatestamp() == null && exifdir.containsTag(ExifDirectory.TAG_DATETIME_ORIGINAL)) {
80                                         data.setOriginalTimestamp(exifdir.getString(ExifDirectory.TAG_DATETIME_ORIGINAL));
81                                 }
82
83                                 // Photo rotation code
84                                 if (exifdir.containsTag(ExifDirectory.TAG_ORIENTATION)) {
85                                         data.setOrientationCode(exifdir.getInt(ExifDirectory.TAG_ORIENTATION));
86                                         // NOTE: this presumably takes the _last_ orientation value found, not the first.
87                                 }
88
89                                 // Thumbnail
90                                 if (exifdir.containsTag(ExifDirectory.TAG_THUMBNAIL_DATA))
91                                 {
92                                         // Make a copy of the byte data rather than keeping a reference to extracted array
93                                         byte[] tdata = exifdir.getByteArray(ExifDirectory.TAG_THUMBNAIL_DATA);
94                                         byte[] thumb = new byte[tdata.length];
95                                         System.arraycopy(tdata, 0, thumb, 0, tdata.length);
96                                         data.setThumbnailImage(thumb);
97                                 }
98                         }
99
100                 }
101                 catch (Exception e) {
102                         // Exception reading metadata, just ignore it
103                 }
104                 return data;
105         }
106
107         /**
108          * Check whether the exifreader class can be correctly resolved
109          * @return true if it looks ok
110          */
111         public boolean looksOK()
112         {
113                 try {
114                         String test = ExifReader.class.getName();
115                         if (test != null) return true;
116                 }
117                 catch (LinkageError le) {}
118                 return false;
119         }
120 }