Advertisement

android照片添加gps,如何利用Exif为图片文件添加GPS坐标信息

阅读量:

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

首先,我们要了解什么是 ExifExifInterface这个类来添加或读取某个文件的 Exif信息。ExifInterface的使用方法非常简单,具体代码如下所示:

/**

  • 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)的作用是添加经纬度坐标信息,存的数据格式如下所示:

// Location类中返回的Latitude和Longitude的数据如下所示,单位为degrees

{

"latitude" : 31.284550

"longitude" : 121.080940

}

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

"GPSLatitude" : "30000000/1000000,1700000/100000,262160/65540"

"GPSLongitude" : "12000000/100000,262160/65540,1717960704/33685504"

看了上面读取出来的数据,大家是不是感觉有点懵啊?其实,大家冷静下来想想,这肯定是在存的时候系统底层做了某种转换,从网上查询一下就能知道上面的数据也是表示经纬度的一种格式dd/1,mm/1,ss/1,分别表示为degrees,minutes,和 seconds转换公式去转换,刚一开始我也是这么做的,但其实ExifInterface已经为我们提供了方法了,具体代码如下所示:

/**

  • 获取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();

}

参考文章

全部评论 (0)

还没有任何评论哟~