rain
2024-08-03 b7c05407dcf01cd7c01fde0efdf2a6a0283d4016
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.dji.sample.media.util;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dji.sample.territory.service.impl.TbFjServiceImpl;
import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import com.drew.metadata.exif.ExifIFD0Directory;
import com.drew.metadata.xmp.XmpDirectory;
import lombok.extern.slf4j.Slf4j;
 
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
 
@Slf4j
public class ImgUtil {
    public static Map<String, String> readPicExifInfo(File file)
            throws ImageProcessingException, IOException {
        Map<String, String> map = new HashMap<>();
        Metadata metadata = ImageMetadataReader.readMetadata(file);
        for (Directory directory : metadata.getDirectories()) {
            for (Tag tag : directory.getTags()) {
                // 将Tag名称和描述添加到Map中
                map.put(tag.getTagName(), tag.getDescription());
            }
            if (directory.hasErrors()) {
                for (String error : directory.getErrors()) {
                    log.error(error);
                }
            }
        }
        return map;
    }
 
    public static String getXmp(File file) {
        try {
            Metadata metadata = JpegMetadataReader.readMetadata(file);
            for (Directory directory : metadata.getDirectories()) {
                if (directory instanceof XmpDirectory) {
                    XmpDirectory xmpDirectory = (XmpDirectory) directory;
                    Map<String, String> xmpProperties = xmpDirectory.getXmpProperties();
                    return JSON.toJSONString(xmpProperties);
                }
            }
        } catch (Exception e) {
            log.error(String.valueOf(e));
        }
        return null;
    }
 
    public static String[] getImageParam(File file) {
        try {
            String[] param = new String[5];
 
            Metadata metadata = JpegMetadataReader.readMetadata(file);
            for (Directory directory : metadata.getDirectories()) {
 
                //获取图片的Exif拍摄时间
                if (directory instanceof ExifIFD0Directory) {
                    ExifIFD0Directory exifIFD0Directory = (ExifIFD0Directory) directory;
                    exifIFD0Directory.getName();
                    param[4] = JSON.toJSONString(exifIFD0Directory.getDate(306));
                }
 
                if (directory instanceof XmpDirectory) {
                    XmpDirectory xmpDirectory = (XmpDirectory) directory;
                    Map<String, String> xmpProperties = xmpDirectory.getXmpProperties();
                    System.out.println(xmpProperties);
                    String degree = xmpProperties.get("drone-dji:GimbalYawDegree");
                    String lat = xmpProperties.get("drone-dji:GpsLatitude");
                    String lon = xmpProperties.get("drone-dji:GpsLongtitude");
                    String relativeAltitude = xmpProperties.get("drone-dji:RelativeAltitude");
 
                    param[0] = degree;
                    param[1] = lat;
                    param[2] = lon;
                    param[3] = relativeAltitude;
                    System.out.println(Arrays.toString(param));
                }
            }
            return param;
        } catch (Exception e) {
            log.error(String.valueOf(e));
            return null;
        }
    }
    public static Object getInfo(File file) throws ImageProcessingException, IOException {
        String str =getXmp(file);
        // 解析 JSON 字符串为 JSON 对象
        JSONObject jsonObject = JSON.parseObject(str);
 
        // 获取指定的键值对
        String gimbalYawDegree = jsonObject.getString("drone-dji:GimbalYawDegree");
        String flightYawDegree = jsonObject.getString("drone-dji:FlightYawDegree");
        String flightPitchDegree = jsonObject.getString("drone-dji:FlightPitchDegree");
        String gimbalPitchDegree = jsonObject.getString("drone-dji:GimbalPitchDegree");
        String mm=readPicExifInfo(file).get("Focal Length 35");
        // 构造新的 JSON 对象
        JSONObject newJsonObject = new JSONObject();
        newJsonObject.put("GimbalYawDegree", gimbalYawDegree);
        newJsonObject.put("FlightYawDegree", flightYawDegree);
        newJsonObject.put("FlightPitchDegree", flightPitchDegree);
        newJsonObject.put("GimbalPitchDegree", gimbalPitchDegree);
        newJsonObject.put("FocalLength", mm);
        return newJsonObject;
    }
 
}