Ticket #8895: upgrade-metadata-extractor.patch

File upgrade-metadata-extractor.patch, 8.1 KB (added by ebourg, 11 years ago)
  • src/org/openstreetmap/josm/gui/layer/geoimage/GeoImageLayer.java

     
    6464
    6565import com.drew.imaging.jpeg.JpegMetadataReader;
    6666import com.drew.lang.CompoundException;
     67import com.drew.lang.GeoLocation;
    6768import com.drew.lang.Rational;
    6869import com.drew.metadata.Directory;
    6970import com.drew.metadata.Metadata;
    7071import com.drew.metadata.MetadataException;
    71 import com.drew.metadata.exif.ExifDirectory;
     72import com.drew.metadata.exif.ExifIFD0Directory;
    7273import com.drew.metadata.exif.GpsDirectory;
    7374
    7475public class GeoImageLayer extends Layer implements PropertyChangeListener, JumpToMarkerLayer {
     
    507508
    508509    private static void extractExif(ImageEntry e) {
    509510
    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;
    515514
    516515        try {
    517516            metadata = JpegMetadataReader.readMetadata(e.getFile());
    518             dirExif = metadata.getDirectory(ExifDirectory.class);
     517            dirExif = metadata.getDirectory(ExifIFD0Directory.class);
    519518            dirGps = metadata.getDirectory(GpsDirectory.class);
    520519        } catch (CompoundException p) {
    521520            e.setExifCoor(null);
    522521            e.setPos(null);
    523522            return;
     523        } catch (IOException p) {
     524            e.setExifCoor(null);
     525            e.setPos(null);
     526            return;
    524527        }
    525528
    526529        try {
    527             int orientation = dirExif.getInt(ExifDirectory.TAG_ORIENTATION);
     530            int orientation = dirExif.getInt(ExifIFD0Directory.TAG_ORIENTATION);
    528531            e.setExifOrientation(orientation);
    529532        } catch (MetadataException ex) {
    530533        }
     
    540543        }
    541544
    542545        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()));
    596549                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                }
    600564            }
    601565        } catch (Exception ex) { // (other exceptions, e.g. #5271)
    602566            System.err.println("Error reading EXIF from file: "+ex);
  • src/org/openstreetmap/josm/tools/ExifReader.java

     
    22package org.openstreetmap.josm.tools;
    33
    44import java.io.File;
     5import java.io.IOException;
    56import java.text.ParseException;
    67import java.util.Date;
    7 import java.util.Iterator;
    88
    99import com.drew.imaging.jpeg.JpegMetadataReader;
    1010import com.drew.imaging.jpeg.JpegProcessingException;
     
    1212import com.drew.metadata.Metadata;
    1313import com.drew.metadata.MetadataException;
    1414import com.drew.metadata.Tag;
    15 import com.drew.metadata.exif.ExifDirectory;
     15import com.drew.metadata.exif.ExifIFD0Directory;
     16import com.drew.metadata.exif.ExifSubIFDDirectory;
    1617
    1718/**
    1819 * Read out exif file information from a jpeg file
     
    2526            Metadata metadata = JpegMetadataReader.readMetadata(filename);
    2627            String dateStr = null;
    2728            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 */) {
    3232                        dateStr = tag.getDescription();
    3333                        break OUTER; // prefer this tag
    3434                    }
    35                     if (tag.getTagType() == ExifDirectory.TAG_DATETIME /* 0x0132 */ ||
    36                         tag.getTagType() == ExifDirectory.TAG_DATETIME_DIGITIZED /* 0x9004 */) {
     35                    if (tag.getTagType() == ExifIFD0Directory.TAG_DATETIME /* 0x0132 */ ||
     36                        tag.getTagType() == ExifSubIFDDirectory.TAG_DATETIME_DIGITIZED /* 0x9004 */) {
    3737                        dateStr = tag.getDescription();
    3838                    }
    3939                }
     
    5454        Integer orientation = null;
    5555        try {
    5656            final Metadata metadata = JpegMetadataReader.readMetadata(filename);
    57             final Directory dir = metadata.getDirectory(ExifDirectory.class);
    58             orientation = dir.getInt(ExifDirectory.TAG_ORIENTATION);
     57            final Directory dir = metadata.getDirectory(ExifIFD0Directory.class);
     58            orientation = dir.getInt(ExifIFD0Directory.TAG_ORIENTATION);
    5959        } catch (JpegProcessingException e) {
    6060            e.printStackTrace();
    6161        } catch (MetadataException e) {
    6262            e.printStackTrace();
     63        } catch (IOException e) {
     64            e.printStackTrace();
    6365        }
    6466        return orientation;
    6567    }