Use of the Exifinterface class to write GPS information to a picture in JPEG format
JPGE format is one of the image compression format, JPG belongs to one of these; if we need to write the GPS information to the picture itself is relatively simple, get the picture handle exifinterface on it, but I have encountered a few problems in the process, After the settlement, we will consider writing this piece for your reference.
Write GPS information to JPEG format picture altogether three steps, get handle , write information, verify write information
First step: Get handle
Gets the picture prefix exifinterfaceexif = newexifinterface (FilePath);
The key point here is filepath this parameter, which is the absolute path of the file plus the filename (definitely contains the file's suffix name!). )
Step Two: Write the information
Write Longitude Information exif.setattribute (Exifinterface.tag_gps_longitude, This.gpsinfoconvert (Jingdu)); Exif.setattribute (exifinterface.tag_gps_longitude_ref, jingdu> 0?) "E": "W");// write latitude information exif.setattribute (Exifinterface.tag_gps_latitude, Gpsinfoconvert (Weidu)); Exif.setattribute (exifinterface.tag_gps_latitude_ref, weidu> 0?) "N": "S");// Execute save exif.saveattributes ();
In the above code, JINGDU and Weidu are all variables that have been declared and copied, representing the latitude and longitude; the last execution save must be manipulated, otherwise the information may not have been written successfully.
Step Three: Verify write information
Exifinterface exiftest = null; exiftest= Newexifinterface (filePath); This.printmessage ("After the completion of the verification:", exiftest);
This printmessage function is a custom information output function designed to read the GPS information in the picture to verify.
private void Printmessage (String tag, exifinterface e) {System.out.println (tag + "\ n" + e.getattribute (Exifinterface.tag _gps_latitude) + "\ n" + e.getattribute (exifinterface.tag_gps_longitude) + "\ n" + e.getattribute (exifinterface.tag_gps_ Processing_method) + "\ n" + e.getattribute (exifinterface.tag_image_length) + "\ n" + e.getattribute (ExifInterface.TAG_ image_width));
Note
1, the above more important place there are two, the first is the new Exifinterface (FilePath) Here the parameter refers to the absolute path plus the file name, Exifinterface This class is not an excuse even though it is so written, and secondly, even if you write the path is wrong, It will not error or even hint, which leads to the path is not easy to find, I made this mistake, did not find time to waste
2, must remember to carry out the preservation; the latitude and longitude information written by Baidu Map will be written in the latitude and longitude information will be problematic, the format is not correct, must be converted through the Gpsinforconvert (double aa) function, this function is also my custom, The found format is not written in the standard format.
Format Conversion Functions:
Private String Gpsinfoconvert (double gpsinfo) { gpsinfo= math.abs (gpsinfo); Stringdms = Location.convert (Gpsinfo, location.format_seconds); String[]splits = Dms.split (":"); String[]secnds = (splits[2]). Split ("\ \"); Stringseconds; if (Secnds.length = = 0) { seconds= splits[2]; } else{ seconds= secnds[0]; } return splits[0] + "/1," + splits[1 "+"/1, "+ Seconds +"/1 ";}
SOURCE download
(because the instance needs to obtain GPS information, so the source code has been used in the Baidu Map SDK, enter their own key can be directly used to obtain GPS information to write to the JPEG picture)
Use of the Exifinterface class to write GPS information to a picture in JPEG format