Ignore:
Timestamp:
2013-08-09T18:05:11+02:00 (11 years ago)
Author:
bastiK
Message:

applied #8895 - Upgrade metadata-extractor to v. 2.6.4 (patch by ebourg)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/com/drew/metadata/exif/GpsDescriptor.java

    r4231 r6127  
    11/*
    2  * This is public domain software - that is, you can do whatever you want
    3  * with it, and include it software that is licensed under the GNU or the
    4  * BSD license, or whatever other licence you choose, including proprietary
    5  * closed source licenses.  I do ask that you leave this header in tact.
    6  *
    7  * If you make modifications to this code that you think would benefit the
    8  * wider community, please send me a copy and I'll post it on my site.
    9  *
    10  * If you make use of this code, I'd appreciate hearing about it.
    11  *   drew@drewnoakes.com
    12  * Latest version of this software kept at
    13  *   http://drewnoakes.com/
    14  *
    15  * Created by dnoakes on 12-Nov-2002 22:27:52 using IntelliJ IDEA.
     2 * Copyright 2002-2012 Drew Noakes
     3 *
     4 *    Licensed under the Apache License, Version 2.0 (the "License");
     5 *    you may not use this file except in compliance with the License.
     6 *    You may obtain a copy of the License at
     7 *
     8 *        http://www.apache.org/licenses/LICENSE-2.0
     9 *
     10 *    Unless required by applicable law or agreed to in writing, software
     11 *    distributed under the License is distributed on an "AS IS" BASIS,
     12 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 *    See the License for the specific language governing permissions and
     14 *    limitations under the License.
     15 *
     16 * More information about this project is available at:
     17 *
     18 *    http://drewnoakes.com/code/exif/
     19 *    http://code.google.com/p/metadata-extractor/
    1620 */
    1721package com.drew.metadata.exif;
    1822
     23import com.drew.lang.GeoLocation;
    1924import com.drew.lang.Rational;
    20 import com.drew.metadata.Directory;
    21 import com.drew.metadata.MetadataException;
     25import com.drew.lang.annotations.NotNull;
     26import com.drew.lang.annotations.Nullable;
    2227import com.drew.metadata.TagDescriptor;
    2328
     29import java.text.DecimalFormat;
     30
    2431/**
    25  *
     32 * Provides human-readable string representations of tag values stored in a <code>GpsDirectory</code>.
     33 *
     34 * @author Drew Noakes http://drewnoakes.com
    2635 */
    27 public class GpsDescriptor extends TagDescriptor
     36public class GpsDescriptor extends TagDescriptor<GpsDirectory>
    2837{
    29     public GpsDescriptor(Directory directory)
     38    public GpsDescriptor(@NotNull GpsDirectory directory)
    3039    {
    3140        super(directory);
    3241    }
    3342
    34     public String getDescription(int tagType) throws MetadataException
     43    @Nullable
     44    public String getDescription(int tagType)
    3545    {
    3646        switch (tagType) {
     47            case GpsDirectory.TAG_GPS_VERSION_ID:
     48                return getGpsVersionIdDescription();
    3749            case GpsDirectory.TAG_GPS_ALTITUDE:
    3850                return getGpsAltitudeDescription();
     
    5769            case GpsDirectory.TAG_GPS_TIME_STAMP:
    5870                return getGpsTimeStampDescription();
     71            case GpsDirectory.TAG_GPS_LONGITUDE:
    5972                // three rational numbers -- displayed in HH"MM"SS.ss
    60             case GpsDirectory.TAG_GPS_LONGITUDE:
    6173                return getGpsLongitudeDescription();
    6274            case GpsDirectory.TAG_GPS_LATITUDE:
     75                // three rational numbers -- displayed in HH"MM"SS.ss
    6376                return getGpsLatitudeDescription();
     77            case GpsDirectory.TAG_GPS_DIFFERENTIAL:
     78                return getGpsDifferentialDescription();
    6479            default:
    65                 return _directory.getString(tagType);
    66         }
    67     }
    68 
    69     public String getGpsLatitudeDescription() throws MetadataException
    70     {
    71         if (!_directory.containsTag(GpsDirectory.TAG_GPS_LATITUDE)) return null;
    72         return getHoursMinutesSecondsDescription(GpsDirectory.TAG_GPS_LATITUDE);
    73     }
    74 
    75     public String getGpsLongitudeDescription() throws MetadataException
    76     {
    77         if (!_directory.containsTag(GpsDirectory.TAG_GPS_LONGITUDE)) return null;
    78         return getHoursMinutesSecondsDescription(GpsDirectory.TAG_GPS_LONGITUDE);
    79     }
    80 
    81     public String getHoursMinutesSecondsDescription(int tagType) throws MetadataException
    82     {
    83         Rational[] components = _directory.getRationalArray(tagType);
    84         // TODO create an HoursMinutesSecods class ??
    85         int deg = components[0].intValue();
    86         float min = components[1].floatValue();
    87         float sec = components[2].floatValue();
    88         // carry fractions of minutes into seconds -- thanks Colin Briton
    89         sec += (min % 1) * 60;
    90         return String.valueOf(deg) + "\"" + String.valueOf((int)min) + "'" + String.valueOf(sec);
    91     }
    92 
    93     public String getGpsTimeStampDescription() throws MetadataException
     80                return super.getDescription(tagType);
     81        }
     82    }
     83
     84    @Nullable
     85    private String getGpsVersionIdDescription()
     86    {
     87        return convertBytesToVersionString(_directory.getIntArray(GpsDirectory.TAG_GPS_VERSION_ID), 1);
     88    }
     89
     90    @Nullable
     91    public String getGpsLatitudeDescription()
     92    {
     93        GeoLocation location = _directory.getGeoLocation();
     94
     95        if (location == null)
     96            return null;
     97
     98        return GeoLocation.decimalToDegreesMinutesSecondsString(location.getLatitude());
     99    }
     100
     101    @Nullable
     102    public String getGpsLongitudeDescription()
     103    {
     104        GeoLocation location = _directory.getGeoLocation();
     105
     106        if (location == null)
     107            return null;
     108
     109        return GeoLocation.decimalToDegreesMinutesSecondsString(location.getLongitude());
     110    }
     111
     112    @Nullable
     113    public String getGpsTimeStampDescription()
    94114    {
    95115        // time in hour, min, sec
    96         if (!_directory.containsTag(GpsDirectory.TAG_GPS_TIME_STAMP)) return null;
    97116        int[] timeComponents = _directory.getIntArray(GpsDirectory.TAG_GPS_TIME_STAMP);
    98         StringBuffer sbuffer = new StringBuffer();
    99         sbuffer.append(timeComponents[0]);
    100         sbuffer.append(":");
    101         sbuffer.append(timeComponents[1]);
    102         sbuffer.append(":");
    103         sbuffer.append(timeComponents[2]);
    104         sbuffer.append(" UTC");
    105         return sbuffer.toString();
    106     }
    107 
     117        if (timeComponents==null)
     118            return null;
     119        StringBuilder description = new StringBuilder();
     120        description.append(timeComponents[0]);
     121        description.append(":");
     122        description.append(timeComponents[1]);
     123        description.append(":");
     124        description.append(timeComponents[2]);
     125        description.append(" UTC");
     126        return description.toString();
     127    }
     128
     129    @Nullable
    108130    public String getGpsDestinationReferenceDescription()
    109131    {
    110         if (!_directory.containsTag(GpsDirectory.TAG_GPS_DEST_DISTANCE_REF)) return null;
    111         String destRef = _directory.getString(GpsDirectory.TAG_GPS_DEST_DISTANCE_REF).trim();
    112         if ("K".equalsIgnoreCase(destRef)) {
     132        final String value = _directory.getString(GpsDirectory.TAG_GPS_DEST_DISTANCE_REF);
     133        if (value==null)
     134            return null;
     135        String distanceRef = value.trim();
     136        if ("K".equalsIgnoreCase(distanceRef)) {
    113137            return "kilometers";
    114         } else if ("M".equalsIgnoreCase(destRef)) {
     138        } else if ("M".equalsIgnoreCase(distanceRef)) {
    115139            return "miles";
    116         } else if ("N".equalsIgnoreCase(destRef)) {
     140        } else if ("N".equalsIgnoreCase(distanceRef)) {
    117141            return "knots";
    118142        } else {
    119             return "Unknown (" + destRef + ")";
    120         }
    121     }
    122 
     143            return "Unknown (" + distanceRef + ")";
     144        }
     145    }
     146
     147    @Nullable
    123148    public String getGpsDirectionDescription(int tagType)
    124149    {
    125         if (!_directory.containsTag(tagType)) return null;
    126         String gpsDirection = _directory.getString(tagType).trim();
    127         return gpsDirection + " degrees";
    128     }
    129 
     150        Rational angle = _directory.getRational(tagType);
     151        // provide a decimal version of rational numbers in the description, to avoid strings like "35334/199 degrees"
     152        String value = angle != null
     153                ? new DecimalFormat("0.##").format(angle.doubleValue())
     154                : _directory.getString(tagType);
     155        if (value==null || value.trim().length()==0)
     156            return null;
     157        return value.trim() + " degrees";
     158    }
     159
     160    @Nullable
    130161    public String getGpsDirectionReferenceDescription(int tagType)
    131162    {
    132         if (!_directory.containsTag(tagType)) return null;
    133         String gpsDistRef = _directory.getString(tagType).trim();
     163        final String value = _directory.getString(tagType);
     164        if (value==null)
     165            return null;
     166        String gpsDistRef = value.trim();
    134167        if ("T".equalsIgnoreCase(gpsDistRef)) {
    135168            return "True direction";
     
    141174    }
    142175
     176    @Nullable
    143177    public String getGpsSpeedRefDescription()
    144178    {
    145         if (!_directory.containsTag(GpsDirectory.TAG_GPS_SPEED_REF)) return null;
    146         String gpsSpeedRef = _directory.getString(GpsDirectory.TAG_GPS_SPEED_REF).trim();
     179        final String value = _directory.getString(GpsDirectory.TAG_GPS_SPEED_REF);
     180        if (value==null)
     181            return null;
     182        String gpsSpeedRef = value.trim();
    147183        if ("K".equalsIgnoreCase(gpsSpeedRef)) {
    148184            return "kph";
     
    156192    }
    157193
     194    @Nullable
    158195    public String getGpsMeasureModeDescription()
    159196    {
    160         if (!_directory.containsTag(GpsDirectory.TAG_GPS_MEASURE_MODE)) return null;
    161         String gpsSpeedMeasureMode = _directory.getString(GpsDirectory.TAG_GPS_MEASURE_MODE).trim();
     197        final String value = _directory.getString(GpsDirectory.TAG_GPS_MEASURE_MODE);
     198        if (value==null)
     199            return null;
     200        String gpsSpeedMeasureMode = value.trim();
    162201        if ("2".equalsIgnoreCase(gpsSpeedMeasureMode)) {
    163202            return "2-dimensional measurement";
     
    169208    }
    170209
     210    @Nullable
    171211    public String getGpsStatusDescription()
    172212    {
    173         if (!_directory.containsTag(GpsDirectory.TAG_GPS_STATUS)) return null;
    174         String gpsStatus = _directory.getString(GpsDirectory.TAG_GPS_STATUS).trim();
     213        final String value = _directory.getString(GpsDirectory.TAG_GPS_STATUS);
     214        if (value==null)
     215            return null;
     216        String gpsStatus = value.trim();
    175217        if ("A".equalsIgnoreCase(gpsStatus)) {
    176             return "Measurement in progess";
     218            return "Active (Measurement in progress)";
    177219        } else if ("V".equalsIgnoreCase(gpsStatus)) {
    178             return "Measurement Interoperability";
     220            return "Void (Measurement Interoperability)";
    179221        } else {
    180222            return "Unknown (" + gpsStatus + ")";
     
    182224    }
    183225
    184     public String getGpsAltitudeRefDescription() throws MetadataException
    185     {
    186         if (!_directory.containsTag(GpsDirectory.TAG_GPS_ALTITUDE_REF)) return null;
    187         int alititudeRef = _directory.getInt(GpsDirectory.TAG_GPS_ALTITUDE_REF);
    188         if (alititudeRef == 0) {
     226    @Nullable
     227    public String getGpsAltitudeRefDescription()
     228    {
     229        Integer value = _directory.getInteger(GpsDirectory.TAG_GPS_ALTITUDE_REF);
     230        if (value==null)
     231            return null;
     232        if (value == 0)
    189233            return "Sea level";
    190         } else {
    191             return "Unknown (" + alititudeRef + ")";
    192         }
    193     }
    194 
    195     public String getGpsAltitudeDescription() throws MetadataException
    196     {
    197         if (!_directory.containsTag(GpsDirectory.TAG_GPS_ALTITUDE)) return null;
    198         String alititude = _directory.getRational(GpsDirectory.TAG_GPS_ALTITUDE).toSimpleString(true);
    199         return alititude + " metres";
     234        if (value == 1)
     235            return "Below sea level";
     236        return "Unknown (" + value + ")";
     237    }
     238
     239    @Nullable
     240    public String getGpsAltitudeDescription()
     241    {
     242        final Rational value = _directory.getRational(GpsDirectory.TAG_GPS_ALTITUDE);
     243        if (value==null)
     244            return null;
     245        return value.intValue() + " metres";
     246    }
     247
     248    @Nullable
     249    public String getGpsDifferentialDescription()
     250    {
     251        final Integer value = _directory.getInteger(GpsDirectory.TAG_GPS_DIFFERENTIAL);
     252        if (value==null)
     253            return null;
     254        if (value == 0)
     255            return "No Correction";
     256        if (value == 1)
     257            return "Differential Corrected";
     258        return "Unknown (" + value + ")";
     259    }
     260
     261    @Nullable
     262    public String getDegreesMinutesSecondsDescription()
     263    {
     264        GeoLocation location = _directory.getGeoLocation();
     265
     266        if (location == null)
     267            return null;
     268
     269        return location.toDMSString();
    200270    }
    201271}
Note: See TracChangeset for help on using the changeset viewer.