Through ExifInterface, some attribute information during taking pictures can be written into the picture file, including latitude and longitude information. This article introduces a method to write latitude and longitude coordinates into JPEG image files!
Core code
/** * Floating point latitude and longitude values are converted into degree and minute and second format * * @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. // . mod := -79.982195 % 1 == 0.982195 // // next get the integer part of the coord. On other words the whole // number part. // . intPart := -79 double mod = coord % 1; int intPart = (int) coord; // set degrees to the value of intPart // . degrees := "-79" degrees = (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. // . coord := 0.982195 * 60 == 58.9317 // mod := 58.9317 % 1 == 0.9317 // // next get the value of the integer part of the coord. // . 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. // . minutes = "58" minutes = (intPart); // do the same again for minutes // . coord := 0.9317 * 60 == 55.902 // . 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. // . seconds = "55" seconds = (intPart); // I used this format for android but you can change it // to return in whatever format you like // . 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; } /** * Write latitude and longitude information into JPEG picture file * * @param picPath * JPEG image file path * @param dLat * Latitude * @param dLon * Longitude */ public void writeLatLonIntoJpeg(String picPath, double dLat, double dLon) { File file = new File(picPath); if (()) { 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{ (ExifInterface.TAG_GPS_LATITUDE, decimalToDMS(dLat)); (ExifInterface.TAG_GPS_LATITUDE_REF, dLat > 0 ? "N" : "S"); // Distinguish between the north and south hemispheres (ExifInterface.TAG_GPS_LONGITUDE, decimalToDMS(dLon)); (ExifInterface.TAG_GPS_LONGITUDE_REF, dLon > 0 ? "E" : "W"); // Distinguish between East and West Meridians (); } } catch (Exception e) { } } }
Test code
String strImgPath = getImageCachePath() + + ""; ExifInterface eif = new ExifInterface(strImgPath); String lat = (ExifInterface.TAG_GPS_LATITUDE); String latRef = (ExifInterface.TAG_GPS_LATITUDE_REF); String lon = (ExifInterface.TAG_GPS_LONGITUDE); String lonRef = (ExifInterface.TAG_GPS_LONGITUDE_REF); ("Latitude Ref - " + latRef); ("Latitude - " + lat); ("Longitude Ref - " + lonRef); ("Longitude - " + lon); if (lat == null && lon == null) // Write it if there is no location information{ writeLatLonIntoJpeg(strImgPath, 39.23456, 116.123456); }
First run result
05-22 17:36:24.566: I/(17966): Latitude Ref - null 05-22 17:36:24.566: I/(17966): Latitude - null 05-22 17:36:24.566: I/(17966): Longitude Ref - null 05-22 17:36:24.566: I/(17966): Longitude - null
The original image has no location information, and write to a location is simulated by calling writeLatLonIntoJpeg(strImgPath, 39.23456, 116.123456).
Second run result
05-22 17:37:11.446: I/(17966): Latitude Ref - N 05-22 17:37:11.446: I/(17966): Latitude - 39/1,14/1,4/1 05-22 17:37:11.446: I/(17966): Longitude Ref - E 05-22 17:37:11.446: I/(17966): Longitude - 116/1,7/1,24/1
The above android implementation of writing location information into JPEG image files is all the content I share with you. I hope you can give you a reference and I hope you can support me more.