How to write location information to a JPEG picture file "android"
Source: Internet
Author: User
<span id="Label3"></p><p><p>The exifinterface can be used to write some property information in the picture file, including latitude and longitude Information. This article describes a way to write latitude and longitude coordinates to a JPEG picture file!</p></p><p><p><br></p></p><p><p><strong>Core code</strong></p></p><p><p></p></p><pre name="code" class="java">/** * Floating-point latitude and longitude value to degrees minutes/minute format * * @param coord * @return */public string Decimaltodms (double coord) {string output, degrees, minut es, Seconds;//gets The modulus the coordinate divided by one (MOD1)./in other words gets all the numbers after the Deci Mal 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 degre Es to the value of INTPART//E.G. degrees: = " -79" degrees = string.valueof (intpart);//next times the MOD1 of degrees by 6 0 so we can find the integer part//for minutes.//get the MOD1 of the new coord to find the numbers after the decimal//p OINT.//E.G. coord: = 0.982195 * = 58.9317//mod: = 58.9317% 1 = = 0.9317////next Get the value of the integer part O f the coord.//e.g intpart: = 58coord = MoD * 60;mod = coord% 1;intpart = (int) coord;if (intpart < 0) {//Convert Nu Mber to positive if it ' s Negative.intpart *=-1;} //set minutes to the value of INTPART.//E.G. minutes = "all" minutes = string.valueof (intpart);//do the same again for min UTES//E.G. coord: = 0.9317 * = 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 = "on" seconds = string.valueof (intpart);//I used this format for a Ndroid 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;} /** * Latitude and longitude information is written in JPEG image file * * @param picpath * JPEG picture file path * @param dlat * Latitude * @param dlon * Longitude */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)//no latitude and longitude information {exif.setattribute (exifinterface.tag_gps_latitude,decimaltodms (dlat)); Exif.setattribute (ExifInterface.TAG_GPS_ Latitude_ref,dlat > 0? "N": "S"); Distinguish between southern and northern hemisphere exif.setattribute (exifinterface.tag_gps_longitude,decimaltodms (dlon)); Exif.setattribute ( Exifinterface.tag_gps_longitude_ref,dlon > 0? "E": "W"); Differentiate East longitude Exif.saveattributes ();}} Catch (Exception E) {}}}</pre><br><strong><strong>Test Code</strong></strong><p><p></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">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)//no location information is written to {writelatlonintojpeg (strimgpat h, 39.23456, 116.123456);}</pre></pre><br><strong><strong>run results for the first time</strong></strong><p><p></p></p><p><p></p></p><pre name="code" class="plain"><pre name="code" class="plain">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</pre></pre><br>The original picture has no location information, which is simulated by calling Writelatlonintojpeg (strimgpath, 39.23456, 116.123456) to write a Location.<p><p></p></p><p><p><br></p></p><p><p><strong>Second run result</strong></p></p><p><p></p></p><pre name="code" class="plain"><pre name="code" class="plain">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</pre></pre><br><br><p><p></p></p><p><p>Reference:</p></p><p><p>http://blog.csdn.net/henry121212/article/details/17553719<br></p></p><p><p>http://blog.csdn.net/caiwenfeng_for_23/article/details/37706617<br></p></p><p><p><br></p></p><p><p><br></p></p><p><p>How to write location information to a JPEG picture file "android"</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service