通过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
相关文章:
如何在香港服务器上快速搭建免备案网站?
教程网站设计制作软件,怎么创建自己的一个网站?
香港服务器网站生成指南:免费资源整合与高速稳定配置方案
武汉网站如何制作,黄黄高铁武穴北站途经哪些村庄?
网站视频怎么制作,哪个网站可以免费收看好莱坞经典大片?
建站之星导航配置指南:自助建站与SEO优化全解析
无锡营销型网站制作公司,无锡网选车牌流程?
建站之星后台密码遗忘?如何快速找回?
邀请函制作网站有哪些,有没有做年会邀请函的网站啊?在线制作,模板很多的那种?
家庭建站与云服务器建站,如何选择更优?
如何快速搭建FTP站点实现文件共享?
,南京靠谱的征婚网站?
上海网站制作网页,上海本地的生活网站有哪些?最好包括生活的各个方面的?
高防服务器:AI智能防御DDoS攻击与数据安全保障
免费视频制作网站,更新又快又好的免费电影网站?
建站之星安装需要哪些步骤及注意事项?
桂林网站制作公司有哪些,桂林马拉松怎么报名?
建站之星会员如何解锁更多建站功能?
如何有效防御Web建站篡改攻击?
建站主机无法访问?如何排查域名与服务器问题
如何通过WDCP绑定主域名及创建子域名站点?
网站制作价目表怎么做,珍爱网婚介费用多少?
如何在万网主机上快速搭建网站?
招商网站制作流程,网站招商广告语?
如何用腾讯建站主机快速创建免费网站?
网站制作话术技巧,网站推广做的好怎么话术?
如何高效利用亚马逊云主机搭建企业网站?
网站好制作吗知乎,网站开发好学吗?有什么技巧?
seo网站制作优化,网站SEO优化步骤有哪些?
建站之星安装路径如何正确选择及配置?
JS中使用new Date(str)创建时间对象不兼容firefox和ie的解决方法(两种)
较简单的网站制作软件有哪些,手机版网页制作用什么软件?
建站之星安装步骤有哪些常见问题?
如何通过云梦建站系统实现SEO快速优化?
开封网站制作公司,网络用语开封是什么意思?
如何快速搭建高效可靠的建站解决方案?
专业企业网站设计制作公司,如何理解商贸企业的统一配送和分销网络建设?
建站之星安装失败:服务器环境不兼容?
如何在七牛云存储上搭建网站并设置自定义域名?
定制建站哪家更专业可靠?推荐榜单揭晓
网站设计制作企业有哪些,抖音官网主页怎么设置?
大同网页,大同瑞慈医院官网?
如何零成本快速生成个人自助网站?
深圳网站制作培训,深圳哪些招聘网站比较好?
如何用PHP工具快速搭建高效网站?
建站之星安装后界面空白如何解决?
如何破解联通资金短缺导致的基站建设难题?
一键网站制作软件,义乌购一件代发流程?
网站制作新手教程,新手建设一个网站需要注意些什么?
东莞专业网站制作公司有哪些,东莞招聘网站哪个好?
*请认真填写需求信息,我们会在24小时内与您取得联系。