通过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 图片 写入文件格式,android实现将位置信息写入JPEG图片文件相关推荐

  1. android全平台编译libjpeg-turbo并基于ANativeWindow加载JPEG图片

    图形图像实践 android全平台编译libjpeg-turbo并基于ANativeWindow加载JPEG图片 android全平台编译libpng并基于ANativeWindow加载PNG图片 概 ...

  2. android图片素材文件格式,Android Studio神器之Image Asset

    我把Image Asset翻译成"图片资源素材",本章主要介绍如何使用Android Studio制作图片素材. (1)阿里图库的使用 起初,我常常在阿里图库中寻找我所需要的素材, ...

  3. android 播放视频文件格式,Android视频文件格式解析相关分析

    目录结构 OpenCore的代码在如下目录中:external/opencore/.这个目录是OpenCore的根目录,其中包含的子目录以下所示: * android:这里面是一个上层的库,它基于PV ...

  4. android 输出xlsx文件格式,Android导出Excel表格文件

    1.首先再Android添加jar包:jxl-2.6.12.jar 2.创建javaBean类,用于存储需要写入表格中的数据 public class DemoBean { private Strin ...

  5. 【Android App】获取照片里的位置信息及使用全球卫星导航系统(GNSS)获取位置实战(附源码和演示 超详细)

    需要全部代码请点赞关注收藏后评论区留言私信~~~ 一.获取照片里的位置信息 手机拍摄的相片还保存着时间.地点.镜头参数等信息,这些信息由相片接口工具ExifInterface管理,它的常用方法说明如下 ...

  6. Android开发获取当前经纬度和详细位置信息(原生代码实现)简单案例

    文章目录 Android定位(经纬度+当前位置信息) 申请权限 LocationManage位置管理器 完整代码 Android定位(经纬度+当前位置信息) 我相信大家在Android开发中应该都有遇 ...

  7. Android 集成GoogleMap,实现定位和获取位置信息

    1.准备 我使用的是AS2.2.2,首先FQ注册google开发者帐号,准备获取API Key,网上有许多相关资料我就不再赘述,这里讲一个比较小白级的获取方法,可以减少许多输入 1.1. AS创建项目 ...

  8. Android开发:基于原生API获取位置信息、卫星信号个数及参与定位的卫星个数

    目录 概述 权限申请及开启GPS 调用原生API进行定位 获取设备收到的卫星信号个数 获取用于定位的卫星信号个数 成果图 概述 最近在做室内外无缝定位的相关demo,室外定位中,GNSS定位方法具有精 ...

  9. Android RTT : 通过 RTT 确定 WLAN 位置信息

    https://source.android.google.cn/devices/tech/connect/wifi-rtt Android 9 中的 WLAN 往返时间 (RTT) 功能允许设备测量 ...

  10. linux 微信不能发图片,微信回应发原图泄露位置信息​;元旦起 AI 造假音视频不得随意发布...

    原标题:微信回应发原图泄露位置信息​:元旦起 AI 造假音视频不得随意发布 参考:开源中国.solidot.cnBeta.腾讯科技.快科技等 0.网信办等三部门联合发布新规:明年起,AI 造假音视频不 ...

最新文章

  1. mysql dml ddldcl权限_MySQL中的DML、DDL、DCL到底是什么呢?
  2. excel中日期转成java_用Java程序将日期转换为序列号,就像在Excel中一样
  3. 一键进入dfu模式软件_dfu模式是什么 dfu模式介绍及进入方法【详解】
  4. php bootstraptable分页,php+bootstrap+dataTable+jquery分页列表
  5. Lua中的字符串函数库
  6. 华谊兄弟出现什么问题_什么是语言训练?这就要从语言问题的出现说起了
  7. 前端学习(3055):vue+element今日头条管理-反馈
  8. 【HDU - 4509】湫湫系列故事——减肥记II(合并区间模板 or 离散化标记 or 线段树)
  9. 宁波大学考研复试C语言设计,2021年宁波大学考研复试名单及复试方案汇总
  10. 【转】TestNG常用注解
  11. 3D程序运行在服务器上 VirtualGL
  12. kaggle房价预测特征意思_未来销量预测——Kaggle基础方案(三):特征工程及线下验证划分...
  13. C#序列化出现“因其保护级别而不可访问。只能处理公共类型。”
  14. Typora_Markdown_图片标题(题注)
  15. 我是如何搭建一台家庭NAS的
  16. centos Iptables学习笔记
  17. 《优势教养》:开放式沟通vs负面偏见
  18. DNS服务在企业网络中的应用(四)
  19. ps 究竟是 aux 还是 ef
  20. 计算商品过期具体时间

热门文章

  1. 几组数据的相关性python_Python数据相关系数矩阵和热力图轻松实现(参数解释)...
  2. fetch与axios
  3. 22. Django进阶:文件上传
  4. Vue:使用vue-json-excel导出数据到excel
  5. 论文笔记_S2D.21_2014-CVPR_单张图像的离散-连续深度估计
  6. 实操教程:Android部署Nanodet模型完成实时高效的物体检测
  7. mysql 查询用户权限
  8. [Alpha阶段]发布说明
  9. FIND、FINDINDEX、INDEXOF、LASTINDEX、INCLUDES 数组五种查询条件方法介绍
  10. [Swift通天遁地]一、超级工具-(11)使用EZLoadingActivity制作Loading加载等待动画