当前位置: 移动技术网 > 移动技术>移动开发>Android > android实现将位置信息写入JPEG图片文件

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

通过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图片文件就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网