通过获取图片中的EXIF信息来定位拍摄地点

最近看见一篇报道“你与陌生人之间,可能只是一张照片的距离。有时候一张 “原图”照片,分分钟就暴露了你的信息……”。如今,任何智能手机拍摄的照片,都含有Exif参数,可以调用GPS全球定位系统数据,在照片中记录下位置、时间等信息。当用户把原始图片发送给其他人时,所附带的信息也一并发出去了。无论用微信、短信、邮件,抑或是其他传输工具,都是如此。
下面我们就来看看如何来获取通过一张图片来获取拍摄地点:
  1. 使用metadata-extractor包实现图片详细信息提取。(主要是为了获取经纬度)
  2. 利用经纬度调用阿里云地图api来获取地址信息。
  3. 利用alibaba封装的fastjson来解析数据。
package com.kevin.method.file;

import java.io.File;
import java.net.URL;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;

public class GetPicProperties {
    public static void main(String[] args) throws Exception, Exception{  
         File file = new File("F:\\Pictures\\黄山行\\IMG_20170402_143927.jpg");  
         printImageTags(file);
     } 
     /** 
      * 读取照片里面的信息 
      */ 
     private static void printImageTags(File file) throws ImageProcessingException, Exception{  
         Metadata metadata = ImageMetadataReader.readMetadata(file);  
         String lat="";
         String log="";
         for (Directory directory : metadata.getDirectories()) {  
             for (Tag tag : directory.getTags()) { 
                 String tagName = tag.getTagName();  //标签名
                 String desc = tag.getDescription(); //标签信息
                 System.out.println(tagName+"   "+desc);//照片信息
                 if (tagName.equals("GPS Latitude")) {  
                      lat = pointToLatlong(desc);
                 } else if (tagName.equals("GPS Longitude")) {  
                      log = pointToLatlong(desc);
                 }

             }  
         }  
         String add = getAdd(log, lat);  
         JSONObject jsonObject = JSON.parseObject(add);
         JSONArray jsonArray = jsonObject.getJSONArray("addrList");
         JSONObject jso = jsonArray.getJSONObject(0);
         String allAdd = jso.getString("admName");  
         String arr[] = allAdd.split(",");  
         String adds = jso.getString("name");  
         System.out.println("省:"+arr[0]+"\n市:"+arr[1]+"\n区:"+arr[2]+"\n"+adds);
     }  
     /** 
      * 经纬度格式  转换
      * @param point 坐标点 
      * @return 
      */ 
     public static String pointToLatlong (String point ) {  
         Double du = Double.parseDouble(point.substring(0, point.indexOf("°")).trim());  
         Double fen = Double.parseDouble(point.substring(point.indexOf("°")+1, point.indexOf("'")).trim());  
         Double miao = Double.parseDouble(point.substring(point.indexOf("'")+1, point.indexOf("\"")).trim());  
         Double duStr = du + fen / 60 + miao / 60 / 60 ;  
         return duStr.toString();  
     }  
     /**
      * 
      * @param log 经度
      * @param lat 纬度
      * @return
      */
     public static String getAdd(String log, String lat ){  
         //参数解释: 纬度,经度 type 001 (100代表道路,010代表POI,001代表门址,111可以同时显示前三项)  
         String urlString = "http://gc.ditu.aliyun.com/regeocoding?l="+lat+","+log+"&type=010";  
         String res = "";     
         try {     
             URL url = new URL(urlString);    
             java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();    
             conn.setDoOutput(true);    
             conn.setRequestMethod("POST");    
             java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8"));    
             String line;    
            while ((line = in.readLine()) != null) {    
                res += line+"\n";    
          }    
             in.close();    
         } catch (Exception e) {    
             System.out.println("error in wapaction,and e is " + e.getMessage());    
         }   
    //               System.out.println(res);  
         return res;    
     }  
}
//图片里的EXIF参数信息
Compression Type   Baseline
Data Precision   8 bits
Image Height   2340 pixels
Image Width   4160 pixels
Number of Components   3
Component 1   Y component: Quantization table 0, Sampling factors 2 horiz/2 vert
Component 2   Cb component: Quantization table 1, Sampling factors 1 horiz/1 vert
Component 3   Cr component: Quantization table 1, Sampling factors 1 horiz/1 vert
Exposure Time   1/1236 sec
F-Number   F2
Exposure Program   Unknown program (0)
ISO Speed Ratings   100
Exif Version   2.20
Date/Time Original   2017:04:02 14:39:27
Date/Time Digitized   2017:04:02 14:39:27
Components Configuration   YCbCr
Shutter Speed Value   1/1235 sec
Aperture Value   F2
Brightness Value   7.43
Metering Mode   Center weighted average
Flash   Flash did not fire, auto
Focal Length   4.12 mm
Sub-Sec Time   477109
Sub-Sec Time Original   477109
Sub-Sec Time Digitized   477109
FlashPix Version   1.00
Color Space   sRGB
Exif Image Width   4160 pixels
Exif Image Height   2340 pixels
Sensing Method   One-chip color area sensor
Scene Type   Directly photographed image
Exposure Mode   Auto exposure
White Balance Mode   Auto white balance
Focal Length 35   30mm
Scene Capture Type   Standard
Make   Xiaomi
Model   Mi-4c
X Resolution   72 dots per inch
Y Resolution   72 dots per inch
Resolution Unit   Inch
Software   libra-user 5.1.1 LMY47V V8.2.1.0.LXKCNDL release-keys
Date/Time   2017:04:02 14:39:27
YCbCr Positioning   Center of pixel array
Interoperability Index   Recommended Exif Interoperability Rules (ExifR98)
Interoperability Version   1.00
GPS Altitude Ref   Sea level
GPS Latitude Ref   N
GPS Latitude   29.0° 52.0' 3.24970000000377"
GPS Longitude Ref   E
GPS Longitude   118.0° 25.0' 49.81550000001107"
GPS Altitude   141 metres
GPS Time-Stamp   6:39:27 UTC
GPS Processing Method   ASCII
GPS Date Stamp   2017:04:02
Thumbnail Compression   JPEG (old-style)
X Resolution   72 dots per inch
Y Resolution   72 dots per inch
Resolution Unit   Inch
Thumbnail Offset   1052 bytes
Thumbnail Length   32357 bytes

//地址信息
省:安徽省
市:黄山市
区:歙县
地址:朋悦旅馆

你可能感兴趣的:(java-api,javaio流)