android实现将位置信息写入JPEG图片文件
通过ExifInterface可以将拍照时的一些属性信息写入图片文件里,其中包括经纬度信息。本文介绍一种将经纬度坐标写入JPEG图片文件的方法!
核心代码
/** *浮点型经纬度值转成度分秒格式 * *@paramcoord *@return */ publicStringdecimalToDMS(doublecoord){ Stringoutput,degrees,minutes,seconds; //getsthemodulusthecoordinatedividedbyone(MOD1). //inotherwordsgetsallthenumbersafterthedecimalpoint. //e.g.mod:=-79.982195%1==0.982195 // //nextgettheintegerpartofthecoord.Onotherwordsthewhole //numberpart. //e.g.intPart:=-79 doublemod=coord%1; intintPart=(int)coord; //setdegreestothevalueofintPart //e.g.degrees:="-79" degrees=String.valueOf(intPart); //nexttimestheMOD1ofdegreesby60sowecanfindtheintegerpart //forminutes. //gettheMOD1ofthenewcoordtofindthenumbersafterthedecimal //point. //e.g.coord:=0.982195*60==58.9317 //mod:=58.9317%1==0.9317 // //nextgetthevalueoftheintegerpartofthecoord. //e.g.intPart:=58 coord=mod*60; mod=coord%1; intPart=(int)coord; if(intPart<0){ //Convertnumbertopositiveifit'snegative. intPart*=-1; } //setminutestothevalueofintPart. //e.g.minutes="58" minutes=String.valueOf(intPart); //dothesameagainforminutes //e.g.coord:=0.9317*60==55.902 //e.g.intPart:=55 coord=mod*60; intPart=(int)coord; if(intPart<0){ //Convertnumbertopositiveifit'snegative. intPart*=-1; } //setsecondstothevalueofintPart. //e.g.seconds="55" seconds=String.valueOf(intPart); //Iusedthisformatforandroidbutyoucanchangeit //toreturninwhateverformatyoulike //e.g.output="-79/1,58/1,56/1" output=degrees+"/1,"+minutes+"/1,"+seconds+"/1"; //StandardoutputofD°M′S″ //output=degrees+"°"+minutes+"'"+seconds+"\""; returnoutput; } /** *将经纬度信息写入JPEG图片文件里 * *@parampicPath *JPEG图片文件路径 *@paramdLat *纬度 *@paramdLon *经度 */ publicvoidwriteLatLonIntoJpeg(StringpicPath,doubledLat,doubledLon){ Filefile=newFile(picPath); if(file.exists()){ try{ ExifInterfaceexif=newExifInterface(picPath); StringtagLat=exif .getAttribute(ExifInterface.TAG_GPS_LATITUDE); StringtagLon=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(Exceptione){ } } }
测试代码
StringstrImgPath=getImageCachePath()+File.separator+"1.jpg"; ExifInterfaceeif=newExifInterface(strImgPath); Stringlat=eif.getAttribute(ExifInterface.TAG_GPS_LATITUDE); StringlatRef=eif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF); Stringlon=eif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE); StringlonRef=eif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF); System.out.println("LatitudeRef-"+latRef); System.out.println("Latitude-"+lat); System.out.println("LongitudeRef-"+lonRef); System.out.println("Longitude-"+lon); if(lat==null&&lon==null)//没有位置信息才写入 { writeLatLonIntoJpeg(strImgPath,39.23456,116.123456); }
第一次运行结果
05-2217:36:24.566:I/System.out(17966):LatitudeRef-null 05-2217:36:24.566:I/System.out(17966):Latitude-null 05-2217:36:24.566:I/System.out(17966):LongitudeRef-null 05-2217:36:24.566:I/System.out(17966):Longitude-null
原始图片没有位置信息,通过调用writeLatLonIntoJpeg(strImgPath,39.23456,116.123456)来模拟写入一个位置。
第二次运行结果
05-2217:37:11.446:I/System.out(17966):LatitudeRef-N 05-2217:37:11.446:I/System.out(17966):Latitude-39/1,14/1,4/1 05-2217:37:11.446:I/System.out(17966):LongitudeRef-E 05-2217:37:11.446:I/System.out(17966):Longitude-116/1,7/1,24/1
以上这篇android实现将位置信息写入JPEG图片文件就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。