java读取照片EXIF信息并转实体类保存到表里的方法

2024-03-11 14:12

本文主要是介绍java读取照片EXIF信息并转实体类保存到表里的方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

读取照片EXIF信息并转实体类保存到表里

用到了就记录一下

主要依赖

<!--读取图片Exif信息-->
<dependency><groupId>com.drewnoakes</groupId><artifactId>metadata-extractor</artifactId><version>2.14.0</version>
</dependency>

Exif信息实体类

整理了一些常见的Exif信息

package com.faker.photoStation.model.domain;import cn.afterturn.easypoi.excel.annotation.Excel;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.faker.photoStation.model.ano.SqliteCreater;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;/*** <p>图片Exif信息</p>** <p>项目名称:photo-map</p>** <p>注释:无</p>** <p>Copyright: Copyright Faker(c) 2024/3/11</p>** <p>公司: Faker</p>** @author 淡梦如烟* @version 1.0* @date 2024/3/11 上午10:03*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("IMAGE_EXIF")
@ApiModel(value = "图片Exif信息对象", description = "Exif信息")
public class ImageExif {@ApiModelProperty("主键")@Excel(name = "主键")@TableId(value = "ID", type = IdType.ASSIGN_ID)private Long id;@SqliteCreater(unique = true)@ApiModelProperty(value = "文件绝对路径", example = "/home/godkiller/Pictures/照片/IMG_20180610_192907.jpg")@TableField("FILE_PATH")private String filePath;@JSONField(name = "File Name")@JsonProperty("File Name")@TableField("FILE_NAME")@ApiModelProperty(value = "文件名称", example = "IMG_20180610_192907.jpg")private String fileName;@JSONField(name = "Exif Image Width")@JsonProperty("Exif Image Width")@TableField("EXIF_IMAGE_WIDTH")@ApiModelProperty(value = "照片宽度", example = "4112 pixels")private String exifImageWidth;@JSONField(name = "Exif Image Height")@JsonProperty("Exif Image Height")@TableField("EXIF_IMAGE_HEIGHT")@ApiModelProperty(value = "照片高度", example = "5488 pixels")private String exifImageHeight;@JSONField(name = "GPS Latitude")@JsonProperty("GPS Latitude")@TableField("GPS_LATITUDE")@ApiModelProperty(value = "纬度", example = "29° 37' 30.61\"")private String gpsLatitude;@JSONField(name = "GPS Longitude")@JsonProperty("GPS Longitude")@TableField("GPS_LONGITUDE")@ApiModelProperty(value = "经度", example = "106° 27' 13.92\"")private String gpsLongitude;@JSONField(name = "Make")@JsonProperty("Make")@TableField("MAKE")@ApiModelProperty(value = "厂商", example = "nubia")private String make;@JSONField(name = "Model")@JsonProperty("Model")@TableField("MODEL")@ApiModelProperty(value = "机型", example = "n11")private String model;@JSONField(name = "Orientation")@JsonProperty("Orientation")@TableField("ORIENTATION")@ApiModelProperty("方向")private String orientation;@JSONField(name = "X Resolution")@JsonProperty("X Resolution")@TableField("X_RESOLUTION")@ApiModelProperty(value = "水平分辨率", example = "72 dots per inch")private String xResolution;@JSONField(name = "Y Resolution")@JsonProperty("Y Resolution")@TableField("Y_RESOLUTION")@ApiModelProperty(value = "垂直分辨率", example = "72 dots per inch")private String yResolution;@JSONField(name = "Resolution Unit")@JsonProperty("Resolution Unit")@TableField("RESOLUTION_UNIT")@ApiModelProperty(value = "分辨率单位")private String resolutionUnit;@JSONField(name = "Date/Time")@JsonProperty("Date/Time")@TableField("date_time")@ApiModelProperty(value = "分辨率单位")private String dateTime;@JSONField(name = "Exposure Time")@JsonProperty("Exposure Time")@TableField("EXPOSURE_TIME")@ApiModelProperty(value = "曝光时间", example = "0.02 sec")private String exposureTime;@JSONField(name = "F-Number")@JsonProperty("F-Number")@TableField("F_NUMBER")@ApiModelProperty(value = "光圈", example = "f/2.0")private String fNumber;@JSONField(name = "Exposure Program")@JsonProperty("Exposure Program")@TableField("EXPOSURE_PROGRAM")@ApiModelProperty(value = "曝光程序", example = "Unknown (0)")private String exposureProgram;@JSONField(name = "ISO Speed Ratings")@JsonProperty("ISO Speed Ratings")@TableField("ISO_SPEED_RATINGS")@ApiModelProperty(value = "ISO感光度", example = "100")private String iSOSpeedRatings;@JSONField(name = "Sensitivity Type")@JsonProperty("Sensitivity Type")@TableField("SENSITIVITY_TYPE")@ApiModelProperty(value = "感光类型")private String sensitivityType;@JSONField(name = "Recommended Exposure Index")@JsonProperty("Recommended Exposure Index")@TableField("RECOMMENDED_EXPOSURE_INDEX")@ApiModelProperty(value = "推荐曝光指数")private String recommendedExposureIndex;@JSONField(name = "Exif Version")@JsonProperty("Exif Version")@TableField("EXIF_VERSION")@ApiModelProperty(value = "Exif版本", example = "2.20")private String exifVersion;@JSONField(name = "Components Configuration")@JsonProperty("Components Configuration")@TableField("COMPONENTS_CONFIGURATION")@ApiModelProperty(value = "成分构成", example = "YCbCr")private String componentsConfiguration;@JSONField(name = "Shutter Speed Value")@JsonProperty("Shutter Speed Value")@TableField("SHUTTER_SPEED_VALUE")@ApiModelProperty(value = "快门速度", example = "1/14 sec")private String shutterSpeedValue;@JSONField(name = "Aperture Value")@JsonProperty("Aperture Value")@TableField("APERTURE_VALUE")@ApiModelProperty(value = "光圈值", example = "f/2.0")private String apertureValue;@JSONField(name = "Exposure Bias Value")@JsonProperty("Exposure Bias Value")@TableField("EXPOSURE_BIAS_VALUE")@ApiModelProperty(value = "曝光补偿", example = "0 EV")private String exposureBiasValue;@JSONField(name = "Metering Mode")@JsonProperty("Metering Mode")@TableField("METERING_MODE")@ApiModelProperty(value = "测光模式", example = "Center weighted average")private String meteringMode;@JSONField(name = "Flash")@JsonProperty("Flash")@TableField("FLASH")@ApiModelProperty(value = "闪光灯", example = "Flash did not fire")private String flash;@JSONField(name = "Focal Length")@JsonProperty("Focal Length")@TableField("FOCAL_LENGTH")@ApiModelProperty(value = "焦距", example = "3.9 mm")private String focalLength;@JSONField(name = "User Comment")@JsonProperty("User Comment")@TableField("USER_COMMENT")@ApiModelProperty(value = "用户注释", example = "{\"sha1\":\"f6f5ad61b3a6dc37c598b8aebef194e3310af797\",\"ext\":\"jpg\"}")private String userComment;@JSONField(name = "Color Space")@JsonProperty("Color Space")@TableField("COLOR_SPACE")@ApiModelProperty(value = "色域", example = "sRGB")private String colorSpace;@JSONField(name = "White Balance Mode")@JsonProperty("White Balance Mode")@TableField("WHITE_BALANCE_MODE")@ApiModelProperty(value = "白平衡模式", example = "Auto white balance")private String whiteBalanceMode;@JSONField(name = "Lens Model")@JsonProperty("Lens Model")@TableField("LENS_MODEL")@ApiModelProperty(value = "镜头型号", example = "Carl Zeiss AG")private String lensModel;@JSONField(name = "Quality")@JsonProperty("Quality")@TableField("QUALITY")@ApiModelProperty(value = "质量")private String quality;}

读取照片信息并转对象

主要代码如下,依赖fastjson等通用的jar包。

/*** 分析照片并保存exif信息** @param photo* @return*/public void analysisDir(File photo) {try {Map<String, Object> exif = this.getImgExif(photo);ImageExif imgExifVo = JSONObject.parseObject(JSONObject.toJSONString(exif), ImageExif.class);imgExifVo.setFilePath(photo.getAbsolutePath());//todo 批量读取后,saveImageExif() 保存到表里} catch (Exception e) {e.printStackTrace();}}/*** 获取exif信息** @param jpegFile* @return* @throws ImageProcessingException* @throws IOException*/public Map<String, Object> getImgExif(File jpegFile) throws ImageProcessingException, IOException {Map<String, Object> exifMap = new HashMap<>();Metadata metadata = ImageMetadataReader.readMetadata(jpegFile);//获取图片所有EXIF信息Iterable<Directory> directories = metadata.getDirectories();for (Directory directory : directories) {for (Tag tag : directory.getTags()) {exifMap.put(tag.getTagName(), tag.getDescription());}}return exifMap;}/*** 保存图片exif信息** @param imgExifVoList*/private void saveImageExif(List<ImageExif> imgExifVoList) {for (ImageExif imgExifVo : imgExifVoList) {synchronized (this) {QueryWrapper<ImageExif> imageExifQueryWrapper = new QueryWrapper<>();imageExifQueryWrapper.eq("FILE_PATH", imgExifVo.getFilePath());ImageExif imageExif = imageExifService.getOne(imageExifQueryWrapper);if (imageExif == null) {imageExifService.save(imgExifVo);} else {imgExifVo.setId(imageExif.getId());imageExifService.updateById(imgExifVo);}}}}

这篇关于java读取照片EXIF信息并转实体类保存到表里的方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/798022

相关文章

Java中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

MySQL数据库双机热备的配置方法详解

《MySQL数据库双机热备的配置方法详解》在企业级应用中,数据库的高可用性和数据的安全性是至关重要的,MySQL作为最流行的开源关系型数据库管理系统之一,提供了多种方式来实现高可用性,其中双机热备(M... 目录1. 环境准备1.1 安装mysql1.2 配置MySQL1.2.1 主服务器配置1.2.2 从

Java中Redisson 的原理深度解析

《Java中Redisson的原理深度解析》Redisson是一个高性能的Redis客户端,它通过将Redis数据结构映射为Java对象和分布式对象,实现了在Java应用中方便地使用Redis,本文... 目录前言一、核心设计理念二、核心架构与通信层1. 基于 Netty 的异步非阻塞通信2. 编解码器三、

SpringBoot基于注解实现数据库字段回填的完整方案

《SpringBoot基于注解实现数据库字段回填的完整方案》这篇文章主要为大家详细介绍了SpringBoot如何基于注解实现数据库字段回填的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解... 目录数据库表pom.XMLRelationFieldRelationFieldMapping基础的一些代

一篇文章彻底搞懂macOS如何决定java环境

《一篇文章彻底搞懂macOS如何决定java环境》MacOS作为一个功能强大的操作系统,为开发者提供了丰富的开发工具和框架,下面:本文主要介绍macOS如何决定java环境的相关资料,文中通过代码... 目录方法一:使用 which命令方法二:使用 Java_home工具(Apple 官方推荐)那问题来了,

Java HashMap的底层实现原理深度解析

《JavaHashMap的底层实现原理深度解析》HashMap基于数组+链表+红黑树结构,通过哈希算法和扩容机制优化性能,负载因子与树化阈值平衡效率,是Java开发必备的高效数据结构,本文给大家介绍... 目录一、概述:HashMap的宏观结构二、核心数据结构解析1. 数组(桶数组)2. 链表节点(Node

Java AOP面向切面编程的概念和实现方式

《JavaAOP面向切面编程的概念和实现方式》AOP是面向切面编程,通过动态代理将横切关注点(如日志、事务)与核心业务逻辑分离,提升代码复用性和可维护性,本文给大家介绍JavaAOP面向切面编程的概... 目录一、AOP 是什么?二、AOP 的核心概念与实现方式核心概念实现方式三、Spring AOP 的关

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三

Python版本信息获取方法详解与实战

《Python版本信息获取方法详解与实战》在Python开发中,获取Python版本号是调试、兼容性检查和版本控制的重要基础操作,本文详细介绍了如何使用sys和platform模块获取Python的主... 目录1. python版本号获取基础2. 使用sys模块获取版本信息2.1 sys模块概述2.1.1