4

我正在编写一个 Java 程序,它实际上读取图像的所有元数据(例如纬度、经度和日期时间)。以下是我正在运行的示例代码。

public static void findLatLong(File jpg){
    try {
        Metadata metadata = ImageMetadataReader.readMetadata(jpg);    
        if (metadata.containsDirectory(GpsDirectory.class)) {
            GpsDirectory gpsDir =(GpsDirectory)metadata.getDirectory(GpsDirectory.class);
            GpsDescriptor gpsDesc = new GpsDescriptor(gpsDir);
            System.out.println("Latitude: " + gpsDesc.getGpsLatitudeDescription());
            System.out.println("Longitude : " + gpsDesc.getGpsLongitudeDescription());
            System.out.println("Date : " + gpsDesc.getGpsTimeStampDescription());
            System.out.println("Minute  : " + gpsDesc.getDegreesMinutesSecondsDescription());
        }else{
        //  System.out.println("----- Did not find GPS Information-------------for file " + jpg.getName() );
        }

    } catch (ImageProcessingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

输出: - 纬度:21.0°8.0'22.99999999998693“经度:79.0°3.0”12.999999999994998“日期:14:16:30 UTC分钟:21.0°8.0'22.99999999998693”,79.0°3.0'12.99999999994998“

我想这都是学位格式。谁能指出我如何获得纬度和经度的真正价值。并获得适当的日期。

4

1 回答 1

3

如果使用 metadata-extractor >= 2.6.0,您可以使用新com.drew.lang.GeoLocation类(changelog)。

GpsDirectory gpsDirectory = metadata.getDirectory(GpsDirectory.class);
GeoLocation location = gpsDirectory.getGeoLocation();
double lat = location.getLatitude();
double lng = location.getLongitude();

如果没有,这就是新类源中正在做的事情:

   /**
     * Converts DMS (degrees-minutes-seconds) rational values, as given 
     * in {@link com.drew.metadata.exif.GpsDirectory},
     * into a single value in degrees, as a double.
     */
    @Nullable
    public static Double degreesMinutesSecondsToDecimal(
            @NotNull final Rational degs, @NotNull final Rational mins, 
            @NotNull final Rational secs, final boolean isNegative)  {

        double decimal = Math.abs(degs.doubleValue())
                + mins.doubleValue() / 60.0d
                + secs.doubleValue() / 3600.0d;

        if (Double.isNaN(decimal))
            return null;

        if (isNegative)
            decimal *= -1;

        return decimal;
    }

方法参数来自哪里:

Rational[] latitudes = getRationalArray(GpsDirectory.TAG_LATITUDE);
Rational[] longitudes = getRationalArray(GpsDirectory.TAG_LONGITUDE);
String latitudeRef = getString(GpsDirectory.TAG_LATITUDE_REF);
String longitudeRef = getString(GpsDirectory.TAG_LONGITUDE_REF);
于 2013-03-12T03:27:57.933 回答