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
0
投稿
猜你喜欢
- 本文实例讲述了Spring实战之清除缓存操作。分享给大家供大家参考,具体如下:一 配置文件<?xml version="1.
- 本文实例为大家分享了Java实现石头剪刀布的具体代码,供大家参考,具体内容如下代码:package com.neusoft.test;imp
- 本文实例为大家分享了Java实现人机猜拳游戏的具体代码,供大家参考,具体内容如下实现:User类public class User { pr
- 新建一个txt的文本(代码中读取这个文本文档路径就行,命名随意)里面的内容一行代表一个,因为我是按行来遍历循环读取要屏蔽的关键字.然后用一个
- 说明:曾经在网上看过花样繁多的分页,很多都号称如何通用,但很多时候往往不尽如人意:有在分页类中还加入URL地址信息的,有在分页类中还进行分页
- 说明Spring Boot Admin 是一个管理和监控你的 Spring Boot 应用程序的应用程序。 这些应用程序通过 Spring
- float是单精度类型,精度是8位有效数字,取值范围是10的-38次方到10的38次方,float占用4个字节的存储空间double是双精度
- 本文实例讲述了C#判断多个文本框是否为空的方法。分享给大家供大家参考。具体实现方法如下:/// <summary>/// 自定义
- 前言在我们日常的开发过程中通过打印详细的日志信息能够帮助我们很好地去发现开发过程中可能出现的Bug,特别是在开发Controller层的接口
- Thread dumps(线程转储)能帮助我们判断 CPU 峰值、死锁、内存异常、应用反应迟钝、响应时间变长和其他系统问题。一些在线的分析工
- 本文实例讲述了Java基于Runtime调用外部程序出现阻塞的解决方法, 是一个很实用的技巧。分享给大家供大家参考。具体分析如下:有时候在j
- 前言无论Spring的XML配置或者Java配置,都可以配置自动扫描,也就是在指定包及其子包下的component,都会被自动扫描并被Spr
- 本文实例为大家分享了C#语言使用Unity实现剪刀石头布游戏的具体代码,供大家参考,具体内容如下游戏:剪刀石头布实现功能:1、电脑随机出牌(
- 简介本文用示例介绍使用MyBatis-Plus进行多表查询的方法,包括静态查询和动态查询。代码controllerpackage com.e
- 需求:接口是否可继承接口?抽象类是否可实现(implements)接口?抽象类是否可继承实体类(concrete class)?抽象类中是否
- 一、反射的基本概念1、反射的概念将类的各个组成部分封装成其他对象,这就是反射的机制.通过上图,我们可以通过反射获得Person类的Class
- 目录前言系统调用的分类同步回调实例异步回调实例基于Future的半异步小结前言先让我们通过一个生活中的场景来还原一下回调的场景:你遇到了一个
- C#版本public static Component AddComponent(GameObject go, string assembl
- 一、分布式锁介绍分布式锁主要用于在分布式环境中保护跨进程、跨主机、跨网络的共享资源实现互斥访问,以达到保证数据的一致性。二、架构介绍&nbs
- 什么是Handler?Handler可以发送和处理消息对象或Runnable对象,这些消息对象和Runnable对象与一个线程相关联。每个H