android实现将位置信息写入JPEG图片文件

所属分类: 软件编程 / Android 阅读数: 36
收藏 0 赞 0 分享

通过ExifInterface可以将拍照时的一些属性信息写入图片文件里,其中包括经纬度信息。本文介绍一种将经纬度坐标写入JPEG图片文件的方法!

核心代码

/**
* 浮点型经纬度值转成度分秒格式
* 
* @param coord
* @return
*/
	public String decimalToDMS(double coord) {
	String output, degrees, minutes, seconds;

// gets the modulus the coordinate divided by one (MOD1).
// in other words gets all the numbers after the decimal point.
// e.g. mod := -79.982195 % 1 == 0.982195
//
// next get the integer part of the coord. On other words the whole
// number part.
// e.g. intPart := -79

	double mod = coord % 1;
	int intPart = (int) coord;

// set degrees to the value of intPart
// e.g. degrees := "-79"

	degrees = String.valueOf(intPart);

// next times the MOD1 of degrees by 60 so we can find the integer part
// for minutes.
// get the MOD1 of the new coord to find the numbers after the decimal
// point.
// e.g. coord := 0.982195 * 60 == 58.9317
// mod := 58.9317 % 1 == 0.9317
//
// next get the value of the integer part of the coord.
// e.g. intPart := 58

	coord = mod * 60;
	mod = coord % 1;
	intPart = (int) coord;
	if (intPart < 0) {
		// Convert number to positive if it's negative.
		intPart *= -1;
}

// set minutes to the value of intPart.
// e.g. minutes = "58"
	minutes = String.valueOf(intPart);

// do the same again for minutes
// e.g. coord := 0.9317 * 60 == 55.902
// e.g. intPart := 55
	coord = mod * 60;
	intPart = (int) coord;
	if (intPart < 0) {
		// Convert number to positive if it's negative.
		intPart *= -1;
	}

// set seconds to the value of intPart.
// e.g. seconds = "55"
	seconds = String.valueOf(intPart);

// I used this format for android but you can change it
// to return in whatever format you like
// e.g. output = "-79/1,58/1,56/1"
	output = degrees + "/1," + minutes + "/1," + seconds + "/1";

// Standard output of D°M′S″
// output = degrees + "°" + minutes + "'" + seconds + "\"";

	return output;
	}

/**
* 将经纬度信息写入JPEG图片文件里
* 
* @param picPath
*      JPEG图片文件路径
* @param dLat
*      纬度
* @param dLon
*      经度
*/
	public void writeLatLonIntoJpeg(String picPath, double dLat, double dLon) {
	File file = new File(picPath);
	if (file.exists()) {
	try {
	ExifInterface exif = new ExifInterface(picPath);
	String tagLat = exif
	.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
	String tagLon = exif
	.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
	if (tagLat == null && tagLon == null) // 无经纬度信息
{
	exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE,
	decimalToDMS(dLat));
	exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF,
	dLat > 0 ? "N" : "S"); // 区分南北半球
	exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE,
	decimalToDMS(dLon));
	exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF,
	dLon > 0 ? "E" : "W"); // 区分东经西经

	exif.saveAttributes();
}
	} catch (Exception e) {

	}
}
	}

测试代码

String strImgPath = getImageCachePath() + File.separator + "1.jpg";

ExifInterface eif = new ExifInterface(strImgPath);
String lat = eif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
String latRef = eif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
String lon = eif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
String lonRef = eif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);

System.out.println("Latitude Ref - " + latRef);
System.out.println("Latitude - " + lat);
System.out.println("Longitude Ref - " + lonRef);
System.out.println("Longitude - " + lon);

if (lat == null && lon == null) // 没有位置信息才写入
{
 writeLatLonIntoJpeg(strImgPath, 39.23456, 116.123456);
}

第一次运行结果

05-22 17:36:24.566: I/System.out(17966): Latitude Ref - null
05-22 17:36:24.566: I/System.out(17966): Latitude - null
05-22 17:36:24.566: I/System.out(17966): Longitude Ref - null
05-22 17:36:24.566: I/System.out(17966): Longitude - null

原始图片没有位置信息,通过调用writeLatLonIntoJpeg(strImgPath, 39.23456, 116.123456)来模拟写入一个位置。

第二次运行结果

05-22 17:37:11.446: I/System.out(17966): Latitude Ref - N
05-22 17:37:11.446: I/System.out(17966): Latitude - 39/1,14/1,4/1
05-22 17:37:11.446: I/System.out(17966): Longitude Ref - E
05-22 17:37:11.446: I/System.out(17966): Longitude - 116/1,7/1,24/1

以上这篇android实现将位置信息写入JPEG图片文件就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android实现悬浮窗体效果

这篇文章主要为大家详细介绍了Android实现悬浮窗体效果,显示悬浮窗口,窗口可以拖动,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Andriod studio 打包aar 的方法

这篇文章主要介绍了Andriod studio 打包aar的方法,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

Android加载loading对话框的功能及实例代码(不退出沉浸式效果)

这篇文章主要介绍了Android加载loading对话框的功能及实例代码,不退出沉浸式效果,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中LayoutInflater.inflater()的正确打开方式

这篇文章主要给大家介绍了关于Android中LayoutInflater.inflater()的正确打开方式,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Delphi在Android下使用Java库的方法

这篇文章主要介绍了Delphi在Android下使用Java库的方法,本文以Android的USB串口通讯库为例,给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多