android实现将位置信息写入JPEG图片文件
作者:jingxian 发布时间:2023-04-16 21:26:00
标签:android,jpeg
通过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


猜你喜欢
- SpringDataJpa like查询无效这里写自定义目录标题SpringDataJpa like查询@Query(value = &qu
- 表单代码<!DOCTYPE html><html lang="en" xmlns="http
- 前段时间在项目开发中,有listview实现单选和多选的效果,特别是listview的单选效果,一开始项目比较紧,自己考虑的是用listvi
- 目录前提第一步、去官网创建高德Key第二步 通过Gradle集成SDK(方便):第三步 配置
- java文字识别程序的关键是寻找一个可以调用的OCR引擎。tesseract-ocr就是一个这样的OCR引擎,在1985年到1995年由HP
- //执行顺序:(优先级从高到低。)静态代码块>mian方法>构造代码块>构造方法。其中静态代码块只执行一次。构造代码块在每
- 本文实例分析了C#动态生成DropDownList执行失败原因。分享给大家供大家参考。具体如下:今天研究DDL控件的动态生成的时候遇到了点问
- 1.组件添加1.1@Configuration@Configuration:告诉SpringBoot这是一个配置类配置类里面使用@Bean标
- 本文为大家分享了一个满足在线网页交流需求的实例,由于java Socket实现的网页版在线聊天功能,供大家参考,具体内容如下实现步骤:1、使
- 问题背景通常我们开发的时候,需要联合控制台和Navicat/PLSQL等工具进行语句的拼接检查,如果只是输出了一堆???,那么将极大降低我们
- 一、简介SHA-256 是 SHA-2 下细分出的一种算法。截止目前(2023-03)未出现“碰撞”
- PostgreSQL是一种特性非常齐全的自由软件的对象-关系型数据库管理系统(ORDBMS),是以加州大学计算机系开发的POSTGRES,4
- 一、前言1、简单的登录验证可以通过Session或者Cookie实现。2、每次登录的时候都要进数据库校验下账户名和密码,只是加了cookie
- RecyclerView 是 android-support-v7-21 版本中新增的一个 Widgets, 还有一个 CardView 会
- yaml介绍YAML(YAML Ain't Markup Language),一种数据序列化格式优点:容易阅读容易与脚本语言交互以数
- 在程序开发中通常有推送消息的需求,通常为短信服务,邮件,电话提醒。短信及电话提醒通常需要向运营商购买服务调用接口,比较麻烦。邮件信息推送也是
- ViewStub可以在运行时动态的添加布局。帮助文档给定的定义是:"A ViewStub is an invisible, zer
- SpringMVC在接收集合请求参数时,需要在Controller方法的集合参数里前添加@RequestBody,而@RequestBody
- 本文实例为大家分享了Android系统工具类的具体代码,供大家参考,具体内容如下系统工具类public class systemUtil {
- Druid动态数据源配置 主要是继承AbstractRoutingDataSource再通过AOP来实现动态数据源切换.下面给大家介绍Dru