Java 如何在 中从 JPEG 中读取 EXIF 和 XMP 人脸数据

1. 引入maven

<!--读取图片元数据-->
<dependency>
    <groupId>com.drewnoakes</groupId>
    <artifactId>metadata-extractor</artifactId>
    <version>2.6.2</version>
</dependency>

2. 正常exif数据读取

@Test
public void test15() throws Exception {
    File jpegFile = new File("C:\\Users\\Administrator\\Desktop\\DJI_20220827115707_0003_Z.JPG");
    Metadata metadata = JpegMetadataReader.readMetadata(jpegFile);

    Iterable<Directory> dis = metadata.getDirectories();

    for (Directory directory : metadata.getDirectories()) {
        for (Tag tag : directory.getTags()) {
            System.out.println(tag);
        }
    }
}
/**
 * 获取图片信息
 *
 * @param file {@link File}
 * @return 图片信息
 */
public static Map<String, String> readImageExif(File file) {
    Map<String, String> map = new HashMap<>();
    Metadata metadata = null;
    try {
        metadata = ImageMetadataReader.readMetadata(file);
    } catch (ImageProcessingException | IOException e) {
        e.printStackTrace();
    }
    for (Directory directory : metadata.getDirectories()) {
        for (Tag tag : directory.getTags()) {
            map.put(tag.getTagName(), tag.getDescription());
        }
    }
    return map;
}

3. 读取exif中XMP数据(代码中DjiExifInfo实体类可以自行定义)

/**
 * 获取大疆航拍图像的exif信息
 *
 * @param imagPath 图片路径
 * @return com.new3s.common.core.utils.file.ImgParamTransConvertUtils.DjiExifInfo
 */
public static DjiExifInfo readDjiExif(String imagPath) throws Exception {
    File jpegFile = new File(imagPath);
    return readDjiExif(jpegFile);
}

/**
 * 获取大疆航拍图像的exif信息
 *
 * @param imagFile 图片文件
 * @return com.new3s.common.core.utils.file.ImgParamTransConvertUtils.DjiExifInfo
 */
public static DjiExifInfo readDjiExif(File imagFile) throws Exception {

    DjiExifInfo djiExifInfo = new DjiExifInfo();

    Metadata metadata = JpegMetadataReader.readMetadata(imagFile);
    XmpDirectory xmpDirectory = metadata.getDirectory(XmpDirectory.class);
    XMPMeta xmpMeta = xmpDirectory.getXMPMeta();

    XMPIterator itr = xmpMeta.iterator();
    while (itr.hasNext()) {
        XMPPropertyInfo pi = (XMPPropertyInfo) itr.next();
        if (pi != null && pi.getPath() != null) {
            if ((pi.getPath().endsWith("drone-dji:FlightYawDegree"))) {
                djiExifInfo.setFlight_yaw(Arith.round(Double.valueOf(pi.getValue()), 8));
            }
            if ((pi.getPath().endsWith("drone-dji:GimbalPitchDegree"))) {
                djiExifInfo.setGimbal_pit(Arith.round(Double.valueOf(pi.getValue()), 8));
            }
            if ((pi.getPath().endsWith("drone-dji:GimbalYawDegree"))) {
                djiExifInfo.setGimbal_yaw(Arith.round(Double.valueOf(pi.getValue()), 8));
            }
            if ((pi.getPath().endsWith("drone-dji:LRFTargetAbsAlt"))) {
                djiExifInfo.setLRF_abs_alt(Arith.round(Double.valueOf(pi.getValue()), 8));
            }
            if ((pi.getPath().endsWith("drone-dji:LRFTargetDistance"))) {
                djiExifInfo.setLRF_distance(Arith.round(Double.valueOf(pi.getValue()), 8));
            }
            if ((pi.getPath().endsWith("drone-dji:LRFTargetLat"))) {
                djiExifInfo.setLRF_lat(Arith.round(Double.valueOf(pi.getValue()), 8));
            }
            if ((pi.getPath().endsWith("drone-dji:LRFTargetLon"))) {
                djiExifInfo.setLRF_lon(Arith.round(Double.valueOf(pi.getValue()), 8));
            }
        }
    }

    return djiExifInfo;
}

 

posted @ 2022-11-10 16:57  xiexie0812  阅读(497)  评论(0编辑  收藏  举报