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 readPicExifInfo(File file) throws ImageProcessingException, IOException { Map 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 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 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; } } /** * 获取图片的XMP信息 * * @param file 图片文件 * @return 包含图片XMP信息的JSONObject对象 * @throws ImageProcessingException 图像处理异常 * @throws IOException 输入输出异常 */ public static Object getInfo(File file) throws ImageProcessingException, IOException { // 从文件中提取XMP信息 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"); // 读取图片的XMP信息中的焦距 String mm=readPicExifInfo(file).get("Focal Length 35"); // 构造新的 JSON 对象 JSONObject newJsonObject = new JSONObject(); // 将获取到的信息添加到新的 JSON 对象中 newJsonObject.put("GimbalYawDegree", gimbalYawDegree); newJsonObject.put("FlightYawDegree", flightYawDegree); newJsonObject.put("FlightPitchDegree", flightPitchDegree); newJsonObject.put("GimbalPitchDegree", gimbalPitchDegree); newJsonObject.put("FocalLength", mm); // 返回包含图片XMP信息的JSON对象 return newJsonObject; } }