1 package tim.prune.jpeg;
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;
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).
20 public class ExternalExifLibrary implements ExifLibrary
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
27 public JpegData getJpegData(File inFile)
29 JpegData data = new JpegData();
30 // Read exif data from picture
33 Metadata metadata = new Metadata();
34 new ExifReader(inFile).extract(metadata);
35 if (metadata.containsDirectory(GpsDirectory.class))
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))
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()});
53 // Altitude (if present)
54 if (gpsdir.containsTag(GpsDirectory.TAG_GPS_ALTITUDE) && gpsdir.containsTag(GpsDirectory.TAG_GPS_ALTITUDE_REF))
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);
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))
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);
70 data.setGpsDatestamp(new int[] {dates[0].intValue(), dates[1].intValue(), dates[2].intValue()});
75 // Tags from Exif directory
76 if (metadata.containsDirectory(ExifDirectory.class))
78 Directory exifdir = metadata.getDirectory(ExifDirectory.class);
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));
84 // Also take "digitized" timestamp
85 if (exifdir.containsTag(ExifDirectory.TAG_DATETIME_DIGITIZED)) {
86 data.setDigitizedTimestamp(exifdir.getString(ExifDirectory.TAG_DATETIME_DIGITIZED));
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.
96 if (exifdir.containsTag(ExifDirectory.TAG_THUMBNAIL_DATA))
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);
107 catch (Exception e) {
108 // Exception reading metadata, just ignore it
114 * Check whether the exifreader class can be correctly resolved
115 * @return true if it looks ok
117 public boolean looksOK()
120 String test = ExifReader.class.getName();
121 if (test != null) return true;
123 catch (LinkageError le) {}