前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >读取图片文件中的位置等meta信息

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

作者头像
yawn
发布2019-03-12 17:23:18
1.1K0
发布2019-03-12 17:23:18
举报

读取图片文件中的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;
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018/12/03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档