如何在Android上的exif数据中保存GPS坐标?

Bri*_*n D 23 gps android exif geotagging

我正在为我的JPEG图像写GPS坐标,并且坐标是正确的(正如我的logcat输出所示),但似乎它以某种方式被破坏了.读取exif数据会产生空值,或者在我的GPS情况下:512.976698 degrees, 512.976698 degrees.任何人都可以解释这个问题吗?

写它:

        try {
            ExifInterface exif = new ExifInterface(filename);
            exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, latitude);
            exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, longitude);
            exif.saveAttributes();
            Log.e("LATITUDE: ", latitude);
            Log.e("LONGITUDE: ", longitude);


        } catch (IOException e) {
            e.printStackTrace();
        }
Run Code Online (Sandbox Code Playgroud)

阅读它:

        try {
            ExifInterface exif = new ExifInterface("/sdcard/globetrotter/mytags/"+ TAGS[position]);
            Log.e("LATITUDE EXTRACTED", exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE));
            Log.e("LONGITUDE EXTRACTED", exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE));
        } catch (IOException e) {
            e.printStackTrace();
        }
Run Code Online (Sandbox Code Playgroud)

它会在(例如)37.715183,-117.260489和出来33619970/65540, 14811136/3368550,33619970/65540, 14811136/3368550.我做错了吗?

编辑:

所以,问题是我没有以正确定义的格式对其进行编码,这就像你在这里看到的那样:

在此输入图像描述

任何人都可以解释这种格式是什么?显然第一个数字是22/1 = 22度,但我无法弄清楚如何计算那里的小数.

Bri*_*n D 23

GPSLatitude

表示纬度.纬度表示为三个 RATIONAL 值,分别给出度,分和秒.如果纬度表示为度,分和秒,则典型格式为dd/1,mm/1,ss/1.当使用度数和分钟时,例如,分数的分数最多给出两位小数,格式为dd/1,mmmm/100,0/1.

https://docs.google.com/viewer?url=http%3A%2F%2Fwww.exif.org%2FExif2-2.PDF

Android文档在没有解释的情况下指定了这一点:http://developer.android.com/reference/android/media/ExifInterface.html#TAG_GPS_LATITUDE

Exif数据是标准化的,并且GPS数据必须使用上述地理坐标(分钟,秒等)而不是分数来编码.除非它在exif标签中以该格式编码,否则它不会粘住.

如何编码:http://en.wikipedia.org/wiki/Geographic_coordinate_conversion

如何解码:http://android-er.blogspot.com/2010/01/convert-exif-gps-info-to-degree-format.html

  • Android 决定支持这种纬度/经度到度数转换已经有很长一段时间了。现在您只需调用 ExifInterface(file).setGpsInfo(location) 和 saveAttributes() 即可完成这项工作。并确保从文件而不是 Uri 创建 ExifInterface,否则您将得到 IOException(“ExifInterface 不支持保存当前输入的属性。”)。https://developer.android.com/reference/android/support/media/ExifInterface.html#setGpsInfo(android.location.Location) (3认同)

Fab*_*yen 11

这是我为地理标记图片所做的一些代码.它尚未经过严格测试,但它似乎没问题(JOSM编辑器和exiftool读取位置).

ExifInterface exif = new ExifInterface(filePath.getAbsolutePath());
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, GPS.convert(latitude));
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, GPS.latitudeRef(latitude));
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, GPS.convert(longitude));
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, GPS.longitudeRef(longitude));
exif.saveAttributes();
Run Code Online (Sandbox Code Playgroud)

GPS类就在这里.(方法可能更短,但至少可读)

/*
 * @author fabien
 */
public class GPS {
    private static StringBuilder sb = new StringBuilder(20);

    /**
     * returns ref for latitude which is S or N.
     * @param latitude
     * @return S or N
     */
    public static String latitudeRef(double latitude) {
        return latitude<0.0d?"S":"N";
    }

    /**
     * returns ref for latitude which is S or N.
     * @param latitude
     * @return S or N
     */
    public static String longitudeRef(double longitude) {
        return longitude<0.0d?"W":"E";
    }

    /**
     * convert latitude into DMS (degree minute second) format. For instance<br/>
     * -79.948862 becomes<br/>
     *  79/1,56/1,55903/1000<br/>
     * It works for latitude and longitude<br/>
     * @param latitude could be longitude.
     * @return
     */
    synchronized public static final String convert(double latitude) {
        latitude=Math.abs(latitude);
        int degree = (int) latitude;
        latitude *= 60;
        latitude -= (degree * 60.0d);
        int minute = (int) latitude;
        latitude *= 60;
        latitude -= (minute * 60.0d);
        int second = (int) (latitude*1000.0d);

        sb.setLength(0);
        sb.append(degree);
        sb.append("/1,");
        sb.append(minute);
        sb.append("/1,");
        sb.append(second);
        sb.append("/1000");
        return sb.toString();
    }
}
Run Code Online (Sandbox Code Playgroud)


aPr*_*ger 5

其他答案提供了很好的背景信息,甚至是一个例子。这不是问题的直接答案,但我想添加一个更简单的示例,而不需要进行任何数学运算。Location类提供一个很好的转换函数:

public String getLonGeoCoordinates(Location location) {

    if (location == null) return "0/1,0/1,0/1000";
    // You can adapt this to latitude very easily by passing location.getLatitude()
    String[] degMinSec = Location.convert(location.getLongitude(), Location.FORMAT_SECONDS).split(":");
    return degMinSec[0] + "/1," + degMinSec[1] + "/1," + degMinSec[2] + "/1000";
}
Run Code Online (Sandbox Code Playgroud)

我将返回值存储在图像中,并且标签解析得很好。您可以在此处检查您的图像和地理坐标:http://regex.info/exif.cgi

编辑

@ratanas 评论翻译成代码:

public boolean storeGeoCoordsToImage(File imagePath, Location location) {

    // Avoid NullPointer
    if (imagePath == null || location == null) return false;

    // If we use Location.convert(), we do not have to worry about absolute values.

    try {
        // c&p and adapted from @Fabyen (sorry for being lazy)
        ExifInterface exif = new ExifInterface(imagePath.getAbsolutePath());
        exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, getLatGeoCoordinates(location));
        exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, location.getLatitude() < 0 ? "S" : "N");
        exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, getLonGeoCoordinates(location));
        exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, location.getLongitude() < 0 ? "W" : "E");
        exif.saveAttributes();
    } catch (IOException e) {
        // do something
        return false;
    }

    // Data was likely written. For sure no NullPointer. 
    return true;
}
Run Code Online (Sandbox Code Playgroud)

这里有一些不错的 LatLong 转换器:latlong.net

  • 这确实很有帮助,但还应该注意的是,要将其用于 Exif,必须使用纬度和经度的绝对值,并确保还以 N、S、W 的形式提供 TAG_GPS_*_REF Exif 属性,E 根据纬度和经度的符号。 (2认同)