Ticket #8895: upgrade-metadata-extractor.patch
File upgrade-metadata-extractor.patch, 8.1 KB (added by , 12 years ago) |
---|
-
src/org/openstreetmap/josm/gui/layer/geoimage/GeoImageLayer.java
64 64 65 65 import com.drew.imaging.jpeg.JpegMetadataReader; 66 66 import com.drew.lang.CompoundException; 67 import com.drew.lang.GeoLocation; 67 68 import com.drew.lang.Rational; 68 69 import com.drew.metadata.Directory; 69 70 import com.drew.metadata.Metadata; 70 71 import com.drew.metadata.MetadataException; 71 import com.drew.metadata.exif.Exif Directory;72 import com.drew.metadata.exif.ExifIFD0Directory; 72 73 import com.drew.metadata.exif.GpsDirectory; 73 74 74 75 public class GeoImageLayer extends Layer implements PropertyChangeListener, JumpToMarkerLayer { … … 507 508 508 509 private static void extractExif(ImageEntry e) { 509 510 510 double deg; 511 double min, sec; 512 double lon, lat; 513 Metadata metadata = null; 514 Directory dirExif = null, dirGps = null; 511 Metadata metadata; 512 Directory dirExif; 513 GpsDirectory dirGps; 515 514 516 515 try { 517 516 metadata = JpegMetadataReader.readMetadata(e.getFile()); 518 dirExif = metadata.getDirectory(Exif Directory.class);517 dirExif = metadata.getDirectory(ExifIFD0Directory.class); 519 518 dirGps = metadata.getDirectory(GpsDirectory.class); 520 519 } catch (CompoundException p) { 521 520 e.setExifCoor(null); 522 521 e.setPos(null); 523 522 return; 523 } catch (IOException p) { 524 e.setExifCoor(null); 525 e.setPos(null); 526 return; 524 527 } 525 528 526 529 try { 527 int orientation = dirExif.getInt(Exif Directory.TAG_ORIENTATION);530 int orientation = dirExif.getInt(ExifIFD0Directory.TAG_ORIENTATION); 528 531 e.setExifOrientation(orientation); 529 532 } catch (MetadataException ex) { 530 533 } … … 540 543 } 541 544 542 545 try { 543 // longitude 544 545 Rational[] components = dirGps.getRationalArray(GpsDirectory.TAG_GPS_LONGITUDE); 546 547 deg = components[0].doubleValue(); 548 min = components[1].doubleValue(); 549 sec = components[2].doubleValue(); 550 551 if (Double.isNaN(deg) && Double.isNaN(min) && Double.isNaN(sec)) 552 throw new IllegalArgumentException(); 553 554 lon = (Double.isNaN(deg) ? 0 : deg + (Double.isNaN(min) ? 0 : (min / 60)) + (Double.isNaN(sec) ? 0 : (sec / 3600))); 555 556 if (dirGps.getString(GpsDirectory.TAG_GPS_LONGITUDE_REF).charAt(0) == 'W') { 557 lon = -lon; 558 } 559 560 // latitude 561 562 components = dirGps.getRationalArray(GpsDirectory.TAG_GPS_LATITUDE); 563 564 deg = components[0].doubleValue(); 565 min = components[1].doubleValue(); 566 sec = components[2].doubleValue(); 567 568 if (Double.isNaN(deg) && Double.isNaN(min) && Double.isNaN(sec)) 569 throw new IllegalArgumentException(); 570 571 lat = (Double.isNaN(deg) ? 0 : deg + (Double.isNaN(min) ? 0 : (min / 60)) + (Double.isNaN(sec) ? 0 : (sec / 3600))); 572 573 if (Double.isNaN(lat)) 574 throw new IllegalArgumentException(); 575 576 if (dirGps.getString(GpsDirectory.TAG_GPS_LATITUDE_REF).charAt(0) == 'S') { 577 lat = -lat; 578 } 579 580 // Store values 581 582 e.setExifCoor(new LatLon(lat, lon)); 583 e.setPos(e.getExifCoor()); 584 585 } catch (CompoundException p) { 586 // Try to read lon/lat as double value (Nonstandard, created by some cameras -> #5220) 587 try { 588 Double longitude = dirGps.getDouble(GpsDirectory.TAG_GPS_LONGITUDE); 589 Double latitude = dirGps.getDouble(GpsDirectory.TAG_GPS_LATITUDE); 590 if (longitude == null || latitude == null) 591 throw new CompoundException(""); 592 593 // Store values 594 595 e.setExifCoor(new LatLon(latitude, longitude)); 546 GeoLocation location = dirGps.getGeoLocation(); 547 if (location != null) { 548 e.setExifCoor(new LatLon(location.getLatitude(), location.getLongitude())); 596 549 e.setPos(e.getExifCoor()); 597 } catch (CompoundException ex) { 598 e.setExifCoor(null); 599 e.setPos(null); 550 } else { 551 // Try to read lon/lat as double value (Nonstandard, created by some cameras -> #5220) 552 try { 553 Double longitude = dirGps.getDouble(GpsDirectory.TAG_GPS_LONGITUDE); 554 Double latitude = dirGps.getDouble(GpsDirectory.TAG_GPS_LATITUDE); 555 556 // Store values 557 558 e.setExifCoor(new LatLon(latitude, longitude)); 559 e.setPos(e.getExifCoor()); 560 } catch (CompoundException ex) { 561 e.setExifCoor(null); 562 e.setPos(null); 563 } 600 564 } 601 565 } catch (Exception ex) { // (other exceptions, e.g. #5271) 602 566 System.err.println("Error reading EXIF from file: "+ex); -
src/org/openstreetmap/josm/tools/ExifReader.java
2 2 package org.openstreetmap.josm.tools; 3 3 4 4 import java.io.File; 5 import java.io.IOException; 5 6 import java.text.ParseException; 6 7 import java.util.Date; 7 import java.util.Iterator;8 8 9 9 import com.drew.imaging.jpeg.JpegMetadataReader; 10 10 import com.drew.imaging.jpeg.JpegProcessingException; … … 12 12 import com.drew.metadata.Metadata; 13 13 import com.drew.metadata.MetadataException; 14 14 import com.drew.metadata.Tag; 15 import com.drew.metadata.exif.ExifDirectory; 15 import com.drew.metadata.exif.ExifIFD0Directory; 16 import com.drew.metadata.exif.ExifSubIFDDirectory; 16 17 17 18 /** 18 19 * Read out exif file information from a jpeg file … … 25 26 Metadata metadata = JpegMetadataReader.readMetadata(filename); 26 27 String dateStr = null; 27 28 OUTER: 28 for (Iterator<Directory> dirIt = metadata.getDirectoryIterator(); dirIt.hasNext();) { 29 for (Iterator<Tag> tagIt = dirIt.next().getTagIterator(); tagIt.hasNext();) { 30 Tag tag = tagIt.next(); 31 if (tag.getTagType() == ExifDirectory.TAG_DATETIME_ORIGINAL /* 0x9003 */) { 29 for (Directory dirIt : metadata.getDirectories()) { 30 for (Tag tag : dirIt.getTags()) { 31 if (tag.getTagType() == ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL /* 0x9003 */) { 32 32 dateStr = tag.getDescription(); 33 33 break OUTER; // prefer this tag 34 34 } 35 if (tag.getTagType() == Exif Directory.TAG_DATETIME /* 0x0132 */ ||36 tag.getTagType() == Exif Directory.TAG_DATETIME_DIGITIZED /* 0x9004 */) {35 if (tag.getTagType() == ExifIFD0Directory.TAG_DATETIME /* 0x0132 */ || 36 tag.getTagType() == ExifSubIFDDirectory.TAG_DATETIME_DIGITIZED /* 0x9004 */) { 37 37 dateStr = tag.getDescription(); 38 38 } 39 39 } … … 54 54 Integer orientation = null; 55 55 try { 56 56 final Metadata metadata = JpegMetadataReader.readMetadata(filename); 57 final Directory dir = metadata.getDirectory(Exif Directory.class);58 orientation = dir.getInt(Exif Directory.TAG_ORIENTATION);57 final Directory dir = metadata.getDirectory(ExifIFD0Directory.class); 58 orientation = dir.getInt(ExifIFD0Directory.TAG_ORIENTATION); 59 59 } catch (JpegProcessingException e) { 60 60 e.printStackTrace(); 61 61 } catch (MetadataException e) { 62 62 e.printStackTrace(); 63 } catch (IOException e) { 64 e.printStackTrace(); 63 65 } 64 66 return orientation; 65 67 }