android实现将位置信息写入JPEG图片文件
作者:jingxian 时间:2023-04-16 21:26:00
通过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
0
投稿
猜你喜欢
C#探秘系列(一)——ToDictionary,ToLookup
2023-04-19 09:16:15
c#实现数据同步的方法(使用文件监控对象filesystemwatcher)
2021-11-27 03:03:46
C#中4种深拷贝方法介绍
2023-01-02 21:16:11
Java源码解析HashMap成员变量
2023-04-24 07:20:22
DataGridView带图标的单元格实现代码
2021-08-07 16:50:04
Android开发实战闹钟项目
2022-02-28 19:48:38
简单理解java泛型的本质(非类型擦除)
2023-10-13 03:54:34
Android自定义view之围棋动画效果的实现
2022-05-07 17:36:38
SpringBoot参数校验Validator框架详解
2023-09-22 07:08:40
Android实现悬浮对话框代码
2022-11-23 16:12:45
Java中遍历Map集合的5种方式总结
2023-04-29 13:14:25
android 分辨率适配的方法
2023-03-09 09:21:47
Android init.rc文件详解及简单实例
2023-08-02 08:55:09
java处理数据库不支持的emoji表情符问题解决
2021-08-21 00:16:58
C# WPF实现的语音播放自定义控件
2022-11-23 19:45:49
Android编程中的消息机制实例详解
2022-11-14 06:50:05
C#实现文本文件读写方法汇总
2023-10-27 16:57:40
c# 遍历获取所有文件的示例代码
2022-11-21 20:01:43
Spring Data Jpa实现分页和排序代码实例
2021-11-08 01:19:48
解决fastjson从1.1.41升级到1.2.28后报错问题详解
2021-12-30 21:55:35