宅男必备技能
如何通过图片获取图片的拍摄位置

1、前提该图片有相应的GPS定位信息

package xxx.jjj;

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;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class ImgUtil {
    // 读取图片的全部信息
    public static void readImgAll(File file) throws ImageProcessingException, IOException {
        Metadata metadata = ImageMetadataReader.readMetadata(file);
        System.out.println("---打印全部详情---");
        for (Directory directory : metadata.getDirectories()) {
            for (Tag tag : directory.getTags()) {
                System.out.format("[%s] - %s = %s\n",
                        directory.getName(), tag.getTagName(), tag.getDescription());
            }
            if (directory.hasErrors()) {
                for (String error : directory.getErrors()) {
                    System.err.format("ERROR: %s", error);
                }
            }
        }
    }

    // 读取图片的常用信息,并将信息封装到集合里面
    public static Map readImgNormal(File file) throws ImageProcessingException, IOException {
        Metadata metadata = ImageMetadataReader.readMetadata(file);
        Map<String,String> map = new HashMap<>();
        for (Directory directory : metadata.getDirectories()) {
            for (Tag tag : directory.getTags()) {
                String tagName = tag.getTagName();  //标签名
                String desc = tag.getDescription(); //标签信息
                if (tagName.equals("Image Height")) {
                    map.put("imgHeigh" , desc);
                } else if (tagName.equals("Image Width")) {
                    map.put("imgWeith" , desc);
                } else if (tagName.equals("Date/Time Original")) {
                    map.put("time" , desc);
                } else if (tagName.equals("GPS Latitude")) {
                    map.put("lat" , GpsUtil.pointToLatlong(desc));
                } else if (tagName.equals("GPS Longitude")) {
                    map.put("log" , GpsUtil.pointToLatlong(desc));
                } else if (tagName.equals("Model")){
                    map.put("Model",desc);
                }

            }
        }
        return map;
    }

    /**
     * api_key:注册的百度api的key
     * coords:经纬度坐标
     * http://api.map.baidu.com/reverse_geocoding/v3/?ak="+api_key+"&output=json&coordtype=wgs84ll&location="+coords
     * <p>
     * 经纬度转地址信息
     *
     * @param lat  维度
     * @param log 精度
     * @return add 地址
     */

    public static Map parseAdd(String log, String lat,String time ,String Model) throws IOException {
        Map<String,String> adress = new HashMap<>();
        String apiKey = "YNxcSCAphFvuPD4LwcgWXwC3SEZZc7Ra";
        //lat 小  log  大
        //参数解释: 纬度,经度 type 001 (100代表道路,010代表POI,001代表门址,111可以同时显示前三项)
        String urlString = "http://api.map.baidu.com/reverse_geocoding/v3/?ak="+apiKey+"&output=json&coordtype=wgs84ll&location="+(lat + "," + log);
        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());
        }
        JSONObject object = JSONObject.parseObject(res);
        if (object.containsKey("result")) {
            JSONObject result = object.getJSONObject("result");

            if (result.containsKey("addressComponent")) {
                JSONObject addressObj = object.getJSONObject("result").getJSONObject("addressComponent");
                addressObj.toString();
                System.out.println(addressObj);
                System.out.println("-------------------------------------");
                adress.put("country", (String) addressObj.get("country"));
                adress.put("province", (String) addressObj.get("province"));
                adress.put("city", (String) addressObj.get("city"));
                adress.put("district", (String) addressObj.get("district"));
                adress.put("street", (String) addressObj.get("street"));
                adress.put("street_number",(String)addressObj.get("street_number"));
                adress.put("direction",(String)addressObj.get("direction"));
                System.out.println("-------------------------------");
                System.out.println(time);
                System.out.println(Model);

            }
        }else {
            adress = null;
        }
        return adress;
    }

    // 将图片里面的经纬度通过百度地图的api解析成详细的地址
    public static Map parseAddByImg(File file) throws ImageProcessingException, IOException {
        Map map = readImgNormal(file);
        String lat = (String) map.get("lat");
        String log = (String) map.get("log");
        String time = (String) map.get("time");
        String Model = (String) map.get("Model");
        return parseAdd(log,lat,time,Model);
    }

}

package xxx.jjj;

public class GpsUtil {

    /**
     * @author lipeng
     * @date 2021/11/4 10:56
     */

        /**
         * 经纬度格式  转换为  度分秒格式
         *
         * @param point 坐标点
         * @return duStr
         */

        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 gps
         * @return gps_dou
         */
        public static double latLng2Decimal(String gps) {
            String a = gps.split("°")[0].replace(" ", "");
            String b = gps.split("°")[1].split("'")[0].replace(" ", "");
            String c = gps.split("°")[1].split("'")[1].replace(" ", "").replace("\"", "");
            double gps_dou = Double.parseDouble(a) + Double.parseDouble(b) / 60 + Double.parseDouble(c) / 60 / 60;
            return gps_dou;
        }
    }


package xxx.jjj;

import com.drew.imaging.ImageProcessingException;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.util.Map;

public class ImgTest {
    private File file = new File("?????");
    @Test
    public void readImgAllTest() throws ImageProcessingException, IOException {
        ImgUtil.readImgAll(file);
    }
    @Test
    public void readImgNormalTest() throws ImageProcessingException, IOException {
        Map map = ImgUtil.readImgNormal(file);
        System.out.println(map);
    }
    @Test
    public void parseAdd() throws IOException {
        String log2= GpsUtil.pointToLatlong("");
        String lat2= GpsUtil.pointToLatlong("");
        Map add = ImgUtil.parseAdd(log2, lat2,"22","23");
        System.out.println(add);
    }
    @Test
    public void parseAddByImg() throws ImageProcessingException, IOException {
        Map add = ImgUtil.parseAddByImg(file);
        System.out.println(add);
    }

}

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐