国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android實現將位置信息寫入JPEG圖片文件

2019-12-12 03:25:18
字體:
來源:轉載
供稿:網友

通過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 - null05-22 17:36:24.566: I/System.out(17966): Latitude - null05-22 17:36:24.566: I/System.out(17966): Longitude Ref - null05-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 - N05-22 17:37:11.446: I/System.out(17966): Latitude - 39/1,14/1,4/105-22 17:37:11.446: I/System.out(17966): Longitude Ref - E05-22 17:37:11.446: I/System.out(17966): Longitude - 116/1,7/1,24/1

以上這篇android實現將位置信息寫入JPEG圖片文件就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 逊克县| 连州市| 大悟县| 张家港市| 团风县| 南皮县| 巴塘县| 砀山县| 新蔡县| 中方县| 德江县| 甘肃省| 威信县| 富顺县| 中宁县| 双辽市| 溧阳市| 清苑县| 阳山县| 碌曲县| 滨海县| 盱眙县| 聂荣县| 涞水县| 栾城县| 内丘县| 扎赉特旗| 增城市| 浦江县| 崇义县| 太仓市| 黑山县| 绩溪县| 迭部县| 瑞丽市| 石城县| 伽师县| 江津市| 深圳市| 英吉沙县| 板桥市|