可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
This is a basically gps application where i am getting the latitude information from the meta data of a picture in this format 28"41'44.13597 .
My need is to convert the same information into decimal and the out will show data in decimal format like 28.705450.
Please help through code or any references
Thanks in advance
回答1:
/** answer=hour+minutes/60+seconds/3600 */ public double convertHourToDecimal(String degree) { if(!degree.matches("(-)?[0-6][0-9]\"[0-6][0-9]\'[0-6][0-9](.[0-9]{1,5})?") throw new IllegalArgumentException(); String[] strArray=degree.split("[\"']"); return Double.parseDouble(strArray[0])+Double.parseDouble(strArray[1])/60+Double.parseDouble(strArray[2])/3600; }
回答2:
Divide the minutes by 60. and the seconds by 3600., then add the three together.
回答3:
I dont know of any java library that will do this for you but the formula to convert from degrees to decimal degrees is: degree + (minutes / 60) + (seconds / (60 * 60))
回答4:
package newstract; import java.io.File; import java.util.Date; import com.drew.imaging.jpeg.JpegMetadataReader; import com.drew.metadata.Directory; import com.drew.metadata.Metadata; import com.drew.metadata.exif.ExifDirectory; import java.text.SimpleDateFormat; import com.drew.metadata.exif.GpsDirectory; public class GetTagInfo { public static void main(String[] args) { System.out.println("Picture Tagged Details"); try{ File jpegFile = new File("DSC_0060.JPG"); Metadata metadata = JpegMetadataReader.readMetadata(jpegFile); Directory exifDirectory = metadata.getDirectory(ExifDirectory.class); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss"); Date myDate = exifDirectory.getDate(ExifDirectory.TAG_DATETIME); System.out.println(sdf.format(myDate)); SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yyyy"); Date myDate1 = exifDirectory.getDate(ExifDirectory.TAG_DATETIME); System.out.println(sdf1.format(myDate1)); SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm:ss"); Date myDate3 = exifDirectory.getDate(ExifDirectory.TAG_DATETIME); System.out.println(sdf2.format(myDate3)); Directory gpsDirectory = metadata.getDirectory(GpsDirectory.class); // Boolean b = (gpsDirectory.containsTag(GpsDirectory.TAG_GPS_LATITUDE)); // System.out.println(GpsDirectory.TAG_GPS_LATITUDE);+ String s = gpsDirectory.getDescription(2); System.out.println(s); SplitString1 w = new SplitString1(); w.doit(s); Iterator directories = metadata.getDirectoryIterator(); while (directories.hasNext()) { GpsDescriptor directory = (GpsDescriptor) directories.next(); System.out.print(directory.getGpsLatitudeDescription()); } } // close of catch catch (Exception e) { System.err.println(e.getMessage()); //System.err.println(tag.getDirectoryName() + " " + tag.getTagName() + " (error)"); } } } class SplitString1 { public void doit(String lat) { String str = lat; String [] temp = null; String dtemp = null; //temp = str.split("[\"]|\"[\']"); temp = str.split("[\"]|[\']" ); dtemp = str.replace("\"", "°"); System.out.println("Formated DCM : "+dtemp); dump(temp); } public void dump(String []s) { for (int i = 0 ; i < s.length ; i++) { System.out.println("\ndegree : "+s[0]); System.out.println("\nminutes : "+s[1]); System.out.println("\nsecond : "+s[2]); String deg = s[0] ; int ndeg = Integer.parseInt(deg); String min = s[1] ; double nmin = Double.parseDouble(min); String sec = s[2] ; double nsec = Double.parseDouble(sec); double decimaldms = (ndeg+(nmin/60)+(nsec/3600)); System.out.println("\nfinaldecimal : "+decimaldms); } } // Decimal degrees = whole number of degrees, plus minutes divided by 60, //plus seconds divided by 3600 }