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;
|
}
|
|
public static void main(String[] args) throws ImageProcessingException, IOException {
|
File file1 = TbFjServiceImpl.downloadFile("http://dev.jxpskj.com:9000/cloud-bucket/05708dc5-4273-4b12-ad54-e05c89d6c3d4/DJI_202406031131_010_05708dc5-4273-4b12-ad54-e05c89d6c3d4/DJI_20240603113305_0001_W_航点1.jpeg");
|
System.out.println(getInfo(file1));
|
}
|
|
}
|