通过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
# jpeg
# Android编程实现图片的上传和下载功能示例
# android保存Bitmap图片到指定文件夹示例
# 给大家
# 浮点
# 希望能
# 这篇
# 小编
# 转成
# 大家多多
# intPart
# number
# valueOf
# set
# int
# part
# mod
# point
# decimal
# integer
# times
# positive
# Convert
相关文章:
哈尔滨网站建设策划,哈尔滨电工证查询网站?
建站主机选购指南:核心配置与性价比推荐解析
,巨量百应是干嘛的?
导航网站建站方案与优化指南:一站式高效搭建技巧解析
北京专业网站制作设计师招聘,北京白云观官方网站?
动图在线制作网站有哪些,滑动动图图集怎么做?
网站制作免费,什么网站能看正片电影?
如何在Golang中使用encoding/gob序列化对象_存储和传输数据
建站之星图片链接生成指南:自助建站与智能设计教程
网页设计与网站制作内容,怎样注册网站?
如何在橙子建站上传落地页?操作指南详解
官网自助建站平台指南:在线制作、快速建站与模板选择全解析
小捣蛋自助建站系统:数据分析与安全设置双核驱动网站优化
天津个人网站制作公司,天津网约车驾驶员从业资格证官网?
如何通过商城免费建站系统源码自定义网站主题?
深圳网站制作费用多少钱,读秀,深圳文献港这样的网站很多只提供网上试读,但有些人只要提供试读的文章就能全篇下载,这个是怎么弄的?
如何在阿里云虚拟主机上快速搭建个人网站?
东莞专业制作网站的公司,东莞大学生网的网址是什么?
如何获取PHP WAP自助建站系统源码?
网站建设制作需要多少钱费用,自己做一个网站要多少钱,模板一般多少钱?
电商网站制作公司有哪些,1688网是什么意思?
如何通过宝塔面板实现本地网站访问?
建站之星如何一键生成手机站?
c++怎么实现高并发下的无锁队列_c++ std::atomic原子变量与CAS操作【详解】
如何在万网开始建站?分步指南解析
定制建站是什么?如何实现个性化需求?
香港服务器WordPress建站指南:SEO优化与高效部署策略
建站VPS推荐:2025年高性能服务器配置指南
建站ABC备案流程中有哪些关键注意事项?
如何通过IIS搭建网站并配置访问权限?
如何通过主机屋免费建站教程十分钟搭建网站?
微信小程序 五星评分(包括半颗星评分)实例代码
如何设置并定期更换建站之星安全管理员密码?
如何快速配置高效服务器建站软件?
如何通过多用户协作模板快速搭建高效企业网站?
如何制作公司的网站链接,公司想做一个网站,一般需要花多少钱?
个人网站制作流程图片大全,个人网站如何注销?
如何快速建站并高效导出源代码?
高性能网站服务器配置指南:安全稳定与高效建站核心方案
太平洋网站制作公司,网络用语太平洋是什么意思?
沈阳制作网站公司排名,沈阳装饰协会官方网站?
建站主机是什么?如何选择适合的建站主机?
公司网站建设制作费用,想建设一个属于自己的企业网站,该如何去做?
如何通过FTP服务器快速搭建网站?
如何用PHP快速搭建高效网站?分步指南
如何用已有域名快速搭建网站?
为什么Go需要go mod文件_Go go mod文件作用说明
制作网站的网址是什么,请问后缀为.com和.com.cn还有.cn的这三种网站是分别是什么类型的网站?
如何在IIS服务器上快速部署高效网站?
如何解决ASP生成WAP建站中文乱码问题?
*请认真填写需求信息,我们会在24小时内与您取得联系。