读取图片文件中的位置等meta信息

2019-03-12 17:23:18 浏览数 (1)

读取图片文件中的meta信息:

代码语言:javascript复制
		<dependency>
			<groupId>com.drewnoakes</groupId>
			<artifactId>metadata-extractor</artifactId>
			<version>2.11.0</version>
		</dependency>
代码语言:javascript复制
        Metadata metadata;
        try {
            metadata = ImageMetadataReader.readMetadata(file.getInputStream());
        } catch (ImageProcessingException | IOException e) {
            e.printStackTrace();
            return;
        }
        String longitude = null;
        String latitude = null;
        String location;
        // 解析经纬度
        if (metadata != null) {
            Iterable<Directory> directories = metadata.getDirectories();
            for (Directory directory : directories) {
                Collection<Tag> tags = directory.getTags();
                for (Tag tag : tags) {
                    String tagName = tag.getTagName();
                    String value = tag.getDescription();
                    if (value != null) {
                        System.out.println(tagName   " : "   value);
                        if ("GPS Longitude".equals(tagName)) {
                            longitude = LocationUtil.convert(value);
                            xxx.setLongitude(longitude);
                        } else if ("GPS Latitude".equals(tagName)) {
                            latitude = LocationUtil.convert(value);
                            xxx.setLatitude(latitude);
                        }
                    }
                }
            }
        }

 根据经纬度获取附近位置信息:

代码语言:javascript复制
    private String getLocation(String longitude, String latitude) {
        String location = null;
        String coordStr = LocationUtil.convertCoordinates(longitude, latitude);
        System.out.println(coordStr);
        String url = "http://gc.ditu.aliyun.com/regeocoding?l="
                  coordStr   "&type=111";
//            String url1 = "http://gc.ditu.aliyun.com/regeocoding?l="
//                      latitude   ","   longitude   "&type=001";
//            String url2 = "http://gc.ditu.aliyun.com/regeocoding?l="
//                      latitude   ","   longitude   "&type=010";
//            String url3 = "http://gc.ditu.aliyun.com/regeocoding?l="
//                      latitude   ","   longitude   "&type=100";
        ResponseEntity<String> res = restTemplate.getForEntity(url, String.class);
        String resBody = res.getBody();
        System.out.println(resBody);
        JSONObject jsonObject = JSON.parseObject(resBody);
        JSONArray jsonArray = JSON.parseArray(jsonObject.getString("addrList"));
        StringBuilder sb = new StringBuilder();
        for (Object o : jsonArray) {
            JSONObject address = (JSONObject) o;
            int status = (int) address.get("status");
            if (status >= 1) {
                String type = (String) address.get("type");
                if ("poi".equals(type)) {
                    String admName = (String) address.get("admName");
                    String addr = (String) address.get("addr");
                    String name = (String) address.get("name");
                    if (StringUtils.isNotBlank(admName) && StringUtils.isNotBlank(name)) {
                        sb.append(admName.replaceAll(",", ""))
                                .append(addr).append(" ").append(name);
                        break;
                    }
                }

            }
        }
        if (sb.length() > 0) {
            sb.append("附近");
            location = new String(sb);
        }
        return location;
    }

经纬度转换以及经纬度坐标转换方法:

代码语言:javascript复制
public class LocationUtil {

    private LocationUtil() {
    }

    public static String convert(String value) {
        int index1 = value.indexOf("°");
        int index2 = value.indexOf("'");
        int index3 = value.indexOf(""");
        Double du = Double.valueOf(value.substring(0, index1));
        Double fen = Double.valueOf(value.substring(index1   2, index2));
        Double miao = Double.valueOf(value.substring(index2   2, index3));
        System.out.println(du   " "   fen   " "   miao);
        return Double.toString((fen * 60   miao) / (60 * 60)   du);
    }

    /**
     * 将 GCJ-02 坐标转换成 BD-09 坐标
     */
    public static String convertCoordinates(String longitude, String latitude) {
        double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
        double x = Double.valueOf(longitude), y = Double.valueOf(latitude);
        double z = sqrt(x * x   y * y)   0.00002 * sin(y * x_pi);
        double theta = atan2(y, x)   0.000003 * cos(x * x_pi);
        double lon = z * cos(theta)   0.0065 - 0.0016;
        double lat = z * sin(theta)   0.006 - 0.00898;
        return lat   ","   lon;
    }
}

0 人点赞