如何运用Exif为照片文件添加GPS坐标信息

电脑杂谈  发布时间:2019-11-01 01:04:42  来源:网络整理

exif中gps数据的格式_exif格式信息_exif中gps数据的格式

今天主要给你们分享一个知识点——如何运用Exif为照片文件添加GPS坐标信息,关于这个知识点网上的资料太多,而且这个知识点本身需要是不难的,但是当我在项目使用时却遭遇了一个问题:在读取某个照片的坐标信息时,发现与之前写入的坐标信息有多大的差别?下面让我们来预测一下这个问题吧:

首先,我们要知道哪些是 Exif1 (可交换图像文件格式)?根据维基百科的解释:它是专门为数码相机的图片设置的,可以记录数码相片的属性信息和拍摄数据。通俗一点说,它的存在就是为了描述一个照片文件的一些信息,例如,GPS经纬度坐标exif中gps数据的格式,图像方向,图像分辨率等等。而我们的需求添加GPS经纬度坐标信息到相应的图像的 Exif 中,在 Android 中我们可以借助ExifInterface这个类来添加或调用某个文件的 Exif信息。ExifInterface的使用方式比较简洁,具体代码如下所示:

exif中gps数据的格式_exif格式信息_exif中gps数据的格式

/**
 * add exif info for file
 *
 * @param filePath the path of file
 * @param location the location info
 */
public static void addExif(String filePath, Location location) {
    try {
        ExifInterface exif = new ExifInterface(filePath);
        if (location != null) {
            exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, location.getLongitude());
            exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, location.getLongitude() > 0.0f ? "E" : "W");
            exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, location.getLatitude());
            exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, location.getLatitude() > 0.0f ? "N" : "S");
        }
        exif.saveAttributes();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
/**
 * read exif info
 * 
 * @param filePath the path of file
 */
public static void readExif(String filePath) {
    try {
        ExifInterface exif = new ExifInterface(filePath);
        String lat = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
        String lng = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

在上述代码中,方法addExif(String filePath, Location location)的作用是添加经纬度坐标信息,存的数据格式如下所示:

exif中gps数据的格式_exif格式信息_exif中gps数据的格式

// Location类中返回的Latitude和Longitude的数据如下所示,单位为degrees
{
    "latitude" : 31.284550
    "longitude" : 121.080940
}

方法readExif(String filePath)的作用是用于抓取经纬度坐标信息,上面展示了经纬度数据存出来的格式,而后面是用该方式读取出来的数据:

exif格式信息_exif中gps数据的格式_exif中gps数据的格式

"GPSLatitude" : "30000000/1000000,1700000/100000,262160/65540"
"GPSLongitude" : "12000000/100000,262160/65540,1717960704/33685504"

看了前面读取出去的数据,大家是不是感觉有点懵啊?其实,大家理智出来想想,这显然是在存的之后平台底层做了某些转换,从网上查询一下就能明白里面的数据只是表示经纬度的一种格式dd/1,mm/1,ss/1,分别表示为degrees,minutes,和 seconds2exif中gps数据的格式,所以我们即使把他们再转换出来就能,我们可以按照转换公式去转换,刚一开始我也有如此做的,但毕竟ExifInterface已经为我们提供了办法了,具体代码如下所示:

exif格式信息_exif中gps数据的格式_exif中gps数据的格式

/**
 * 获取GPS exif信息
 *
 * @param filePath
 * @return
 */
public static float[] getGpsEXif(String filePath) {
    float[] result = new float[2];
    ExifInterface exif = null;
    try {
        exif = new ExifInterface(filePath);
        exif.getLatLong(result);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    return result;
}

到了前面这一步,大家是不是觉得终于万事大吉了,别兴奋早哦,上面的方式的确可以把dd/1,mm/1,ss/1这种格式转为degrees,但是转化的结果与最初的数据差距巨大,根据实验结果是有相差1°左右,所以这种还是有问题,我们需要在存的之后先做一下转换,把degrees转为dd/1,mm/1,ss/1,算法很简单,如下所示:

/**
 * 转换维度
 * 在转成字符串的时候,有强转为int型,会有一定精度的影响
 *
 * @param decimalDegrees
 * @return
 */
private static String covertLatToDMS(double decimalDegrees) {
    StringBuilder sb = new StringBuilder();
    float degrees = (float) Math.floor(decimalDegrees);
    float minutes = (float) Math.floor(60 * (decimalDegrees - degrees));
    float seconds = (float) (3600 * (decimalDegrees - degrees) - 60 * minutes);
    sb.append((int) degrees * 1000000).append("/1000000,").append((int) minutes * 100000)
            .append("/100000,").append((int) seconds * 65540).append("/65540");
    return sb.toString();
}
/**
 * 转换经度
 *
 * @param decimalDegrees
 * @return
 */
private static String covertLongToDMS(double decimalDegrees) {
    StringBuilder sb = new StringBuilder();
    float degrees = (float) Math.floor(decimalDegrees);
    float minutes = (float) Math.floor(60 * (decimalDegrees - degrees));
    float seconds = (float) (3600 * (decimalDegrees - degrees) - 60 * minutes);
    sb.append((int) degrees * 100000).append("/100000,").append((int) minutes * 65540)
    .append("/65540,").append((int) seconds * 33685504).append("/33685504");
    return sb.toString();
}

[1] 维基百科 EXIF

[2] How to save GPS coordinates in exif data on Android


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/ruanjian/article-128381-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      • 莫艳鸳
        莫艳鸳

        时以为只有万恶的旧社会才会多男找一个老婆

      • 薛瑞阳
        薛瑞阳

        是不是很熟悉的一句广告语

      • 李权锋
        李权锋

        说的再有道理也是放屁

      热点图片
      拼命载入中...