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

相关文章

Oracle查询优化之高效实现仅查询前10条记录的方法与实践

《Oracle查询优化之高效实现仅查询前10条记录的方法与实践》:本文主要介绍Oracle查询优化之高效实现仅查询前10条记录的相关资料,包括使用ROWNUM、ROW_NUMBER()函数、FET... 目录1. 使用 ROWNUM 查询2. 使用 ROW_NUMBER() 函数3. 使用 FETCH FI

Java实现Excel与HTML互转

《Java实现Excel与HTML互转》Excel是一种电子表格格式,而HTM则是一种用于创建网页的标记语言,虽然两者在用途上存在差异,但有时我们需要将数据从一种格式转换为另一种格式,下面我们就来看看... Excel是一种电子表格格式,广泛用于数据处理和分析,而HTM则是一种用于创建网页的标记语言。虽然两

java图像识别工具类(ImageRecognitionUtils)使用实例详解

《java图像识别工具类(ImageRecognitionUtils)使用实例详解》:本文主要介绍如何在Java中使用OpenCV进行图像识别,包括图像加载、预处理、分类、人脸检测和特征提取等步骤... 目录前言1. 图像识别的背景与作用2. 设计目标3. 项目依赖4. 设计与实现 ImageRecogni

Git中恢复已删除分支的几种方法

《Git中恢复已删除分支的几种方法》:本文主要介绍在Git中恢复已删除分支的几种方法,包括查找提交记录、恢复分支、推送恢复的分支等步骤,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录1. 恢复本地删除的分支场景方法2. 恢复远程删除的分支场景方法3. 恢复未推送的本地删除分支场景方法4. 恢复

Java中Springboot集成Kafka实现消息发送和接收功能

《Java中Springboot集成Kafka实现消息发送和接收功能》Kafka是一个高吞吐量的分布式发布-订阅消息系统,主要用于处理大规模数据流,它由生产者、消费者、主题、分区和代理等组件构成,Ka... 目录一、Kafka 简介二、Kafka 功能三、POM依赖四、配置文件五、生产者六、消费者一、Kaf

Java访问修饰符public、private、protected及默认访问权限详解

《Java访问修饰符public、private、protected及默认访问权限详解》:本文主要介绍Java访问修饰符public、private、protected及默认访问权限的相关资料,每... 目录前言1. public 访问修饰符特点:示例:适用场景:2. private 访问修饰符特点:示例:

Python将大量遥感数据的值缩放指定倍数的方法(推荐)

《Python将大量遥感数据的值缩放指定倍数的方法(推荐)》本文介绍基于Python中的gdal模块,批量读取大量多波段遥感影像文件,分别对各波段数据加以数值处理,并将所得处理后数据保存为新的遥感影像... 本文介绍基于python中的gdal模块,批量读取大量多波段遥感影像文件,分别对各波段数据加以数值处

详解Java如何向http/https接口发出请求

《详解Java如何向http/https接口发出请求》这篇文章主要为大家详细介绍了Java如何实现向http/https接口发出请求,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 用Java发送web请求所用到的包都在java.net下,在具体使用时可以用如下代码,你可以把它封装成一

在C#中获取端口号与系统信息的高效实践

《在C#中获取端口号与系统信息的高效实践》在现代软件开发中,尤其是系统管理、运维、监控和性能优化等场景中,了解计算机硬件和网络的状态至关重要,C#作为一种广泛应用的编程语言,提供了丰富的API来帮助开... 目录引言1. 获取端口号信息1.1 获取活动的 TCP 和 UDP 连接说明:应用场景:2. 获取硬

Window Server2016加入AD域的方法步骤

《WindowServer2016加入AD域的方法步骤》:本文主要介绍WindowServer2016加入AD域的方法步骤,包括配置DNS、检测ping通、更改计算机域、输入账号密码、重启服务... 目录一、 准备条件二、配置ServerB加入ServerA的AD域(test.ly)三、查看加入AD域后的变