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中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

Spring Boot 中 RestTemplate 的核心用法指南

《SpringBoot中RestTemplate的核心用法指南》本文详细介绍了RestTemplate的使用,包括基础用法、进阶配置技巧、实战案例以及最佳实践建议,通过一个腾讯地图路线规划的案... 目录一、环境准备二、基础用法全解析1. GET 请求的三种姿势2. POST 请求深度实践三、进阶配置技巧1

Python字符串处理方法超全攻略

《Python字符串处理方法超全攻略》字符串可以看作多个字符的按照先后顺序组合,相当于就是序列结构,意味着可以对它进行遍历、切片,:本文主要介绍Python字符串处理方法的相关资料,文中通过代码介... 目录一、基础知识:字符串的“不可变”特性与创建方式二、常用操作:80%场景的“万能工具箱”三、格式化方法

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

Spring Boot 处理带文件表单的方式汇总

《SpringBoot处理带文件表单的方式汇总》本文详细介绍了六种处理文件上传的方式,包括@RequestParam、@RequestPart、@ModelAttribute、@ModelAttr... 目录方式 1:@RequestParam接收文件后端代码前端代码特点方式 2:@RequestPart接

SpringBoot整合Zuul全过程

《SpringBoot整合Zuul全过程》Zuul网关是微服务架构中的重要组件,具备统一入口、鉴权校验、动态路由等功能,它通过配置文件进行灵活的路由和过滤器设置,支持Hystrix进行容错处理,还提供... 目录Zuul网关的作用Zuul网关的应用1、网关访问方式2、网关依赖注入3、网关启动器4、网关全局变

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

基于SpringBoot实现分布式锁的三种方法

《基于SpringBoot实现分布式锁的三种方法》这篇文章主要为大家详细介绍了基于SpringBoot实现分布式锁的三种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、基于Redis原生命令实现分布式锁1. 基础版Redis分布式锁2. 可重入锁实现二、使用Redisso

SpringBoot的全局异常拦截实践过程

《SpringBoot的全局异常拦截实践过程》SpringBoot中使用@ControllerAdvice和@ExceptionHandler实现全局异常拦截,@RestControllerAdvic... 目录@RestControllerAdvice@ResponseStatus(...)@Except

Springboot配置文件相关语法及读取方式详解

《Springboot配置文件相关语法及读取方式详解》本文主要介绍了SpringBoot中的两种配置文件形式,即.properties文件和.yml/.yaml文件,详细讲解了这两种文件的语法和读取方... 目录配置文件的形式语法1、key-value形式2、数组形式读取方式1、通过@value注解2、通过