Ticket #8895: upgrade-metadata-extractor2.patch
File upgrade-metadata-extractor2.patch, 8.7 KB (added by , 12 years ago) |
---|
-
src/org/openstreetmap/josm/gui/layer/geoimage/GeoImageLayer.java
68 68 import com.drew.metadata.Directory; 69 69 import com.drew.metadata.Metadata; 70 70 import com.drew.metadata.MetadataException; 71 import com.drew.metadata.exif.Exif Directory;71 import com.drew.metadata.exif.ExifIFD0Directory; 72 72 import com.drew.metadata.exif.GpsDirectory; 73 73 74 74 public class GeoImageLayer extends Layer implements PropertyChangeListener, JumpToMarkerLayer { … … 510 510 double deg; 511 511 double min, sec; 512 512 double lon, lat; 513 Metadata metadata = null; 514 Directory dirExif = null, dirGps = null; 513 Metadata metadata; 514 Directory dirExif; 515 GpsDirectory dirGps; 515 516 516 517 try { 517 518 metadata = JpegMetadataReader.readMetadata(e.getFile()); 518 dirExif = metadata.getDirectory(Exif Directory.class);519 dirExif = metadata.getDirectory(ExifIFD0Directory.class); 519 520 dirGps = metadata.getDirectory(GpsDirectory.class); 520 521 } catch (CompoundException p) { 521 522 e.setExifCoor(null); 522 523 e.setPos(null); 523 524 return; 525 } catch (IOException p) { 526 e.setExifCoor(null); 527 e.setPos(null); 528 return; 524 529 } 525 530 526 531 try { 527 int orientation = dirExif.getInt(Exif Directory.TAG_ORIENTATION);532 int orientation = dirExif.getInt(ExifIFD0Directory.TAG_ORIENTATION); 528 533 e.setExifOrientation(orientation); 529 534 } catch (MetadataException ex) { 530 535 } … … 543 548 // longitude 544 549 545 550 Rational[] components = dirGps.getRationalArray(GpsDirectory.TAG_GPS_LONGITUDE); 551 if (components != null) { 552 deg = components[0].doubleValue(); 553 min = components[1].doubleValue(); 554 sec = components[2].doubleValue(); 546 555 547 deg = components[0].doubleValue(); 548 min = components[1].doubleValue(); 549 sec = components[2].doubleValue(); 556 if (Double.isNaN(deg) && Double.isNaN(min) && Double.isNaN(sec)) 557 throw new IllegalArgumentException(); 550 558 551 if (Double.isNaN(deg) && Double.isNaN(min) && Double.isNaN(sec)) 552 throw new IllegalArgumentException(); 559 lon = (Double.isNaN(deg) ? 0 : deg + (Double.isNaN(min) ? 0 : (min / 60)) + (Double.isNaN(sec) ? 0 : (sec / 3600))); 553 560 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; 561 if (dirGps.getString(GpsDirectory.TAG_GPS_LONGITUDE_REF).charAt(0) == 'W') { 562 lon = -lon; 563 } 564 } else { 565 // Try to read lon/lat as double value (Nonstandard, created by some cameras -> #5220) 566 lon = dirGps.getDouble(GpsDirectory.TAG_GPS_LONGITUDE); 558 567 } 559 568 560 569 // latitude 561 570 562 571 components = dirGps.getRationalArray(GpsDirectory.TAG_GPS_LATITUDE); 572 if (components != null) { 573 deg = components[0].doubleValue(); 574 min = components[1].doubleValue(); 575 sec = components[2].doubleValue(); 563 576 564 deg = components[0].doubleValue(); 565 min = components[1].doubleValue(); 566 sec = components[2].doubleValue(); 577 if (Double.isNaN(deg) && Double.isNaN(min) && Double.isNaN(sec)) 578 throw new IllegalArgumentException(); 567 579 568 if (Double.isNaN(deg) && Double.isNaN(min) && Double.isNaN(sec)) 569 throw new IllegalArgumentException(); 580 lat = (Double.isNaN(deg) ? 0 : deg + (Double.isNaN(min) ? 0 : (min / 60)) + (Double.isNaN(sec) ? 0 : (sec / 3600))); 570 581 571 lat = (Double.isNaN(deg) ? 0 : deg + (Double.isNaN(min) ? 0 : (min / 60)) + (Double.isNaN(sec) ? 0 : (sec / 3600))); 582 if (Double.isNaN(lat)) 583 throw new IllegalArgumentException(); 572 584 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;585 if (dirGps.getString(GpsDirectory.TAG_GPS_LATITUDE_REF).charAt(0) == 'S') { 586 lat = -lat; 587 } 588 } else { 589 lat = dirGps.getDouble(GpsDirectory.TAG_GPS_LATITUDE); 578 590 } 579 591 580 592 // Store values … … 582 594 e.setExifCoor(new LatLon(lat, lon)); 583 595 e.setPos(e.getExifCoor()); 584 596 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 values594 595 e.setExifCoor(new LatLon(latitude, longitude));596 e.setPos(e.getExifCoor());597 } catch (CompoundException ex) {598 e.setExifCoor(null);599 e.setPos(null);600 }601 597 } catch (Exception ex) { // (other exceptions, e.g. #5271) 602 598 System.err.println("Error reading EXIF from file: "+ex); 603 599 e.setExifCoor(null); -
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 }