android应用如何将位置信息写入JPEG文件中
短信预约 -IT技能 免费直播动态提醒
这篇文章给大家介绍android应用如何将位置信息写入JPEG文件中,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
核心代码
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 := -79double 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 := 58coord = 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 := 55coord = 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;}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文件中就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341