項目結構

第一步:添加需要的架包metadate-extractor.jar
架包下載地址:https://code.google.com/p/metadata-extractor/downloads/list
或者去Maven倉庫下載 http://search.maven.org/#search%7Cga%7C1%7Cmetadata-extractor
第二步:編寫解析代碼
1 package com.drew.metadata; 2 import java.io.File; 3 4 import com.drew.imaging.ImageMetadataReader; 5 import com.drew.imaging.ImageProcessingException; 6 /** 7 * java讀取照片信息 8 */ 9 public class SampleUsage{ 10 public static void main(String[] args) throws Exception, Exception{ 11 File file = new File("E:\\帶有地理位置的圖片.jpg"); 12 printImageTags(file); 13 } 14 /** 15 * 讀取照片里面的信息 16 */ 17 private static void printImageTags(File file) throws ImageProcessingException, Exception{ 18 Metadata metadata = ImageMetadataReader.readMetadata(file); 19 for (Directory directory : metadata.getDirectories()) { 20 for (Tag tag : directory.getTags()) { 21 String tagName = tag.getTagName(); //標簽名 22 String desc = tag.getDescription(); //標簽信息 23 if (tagName.equals("Image Height")) { 24 System.out.println("圖片高度: "+desc); 25 } else if (tagName.equals("Image Width")) { 26 System.out.println("圖片寬度: "+desc); 27 } else if (tagName.equals("Date/Time Original")) { 28 System.out.println("拍攝時間: "+desc); 29 }else if (tagName.equals("GPS Latitude")) { 30 System.err.println("緯度 : "+desc); 31 // System.err.println("緯度(度分秒格式) : "+pointToLatlong(desc)); 32 } else if (tagName.equals("GPS Longitude")) { 33 System.err.println("經度: "+desc); 34 // System.err.println("經度(度分秒格式): "+pointToLatlong(desc)); 35 } 36 } 37 } 38 } 39 /** 40 * 經緯度格式 轉換為 度分秒格式 ,如果需要的話可以調用該方法進行轉換 41 * @param point 坐標點 42 * @return 43 */ 44 public static String pointToLatlong (String point ) { 45 Double du = Double.parseDouble(point.substring(0, point.indexOf("°")).trim()); 46 Double fen = Double.parseDouble(point.substring(point.indexOf("°")+1, point.indexOf("'")).trim()); 47 Double miao = Double.parseDouble(point.substring(point.indexOf("'")+1, point.indexOf("\"")).trim()); 48 Double duStr = du + fen / 60 + miao / 60 / 60 ; 49 return duStr.toString(); 50 } 51 } 52
