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

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

Java进阶13讲__第12讲_1/2

多线程、线程池 1.  线程概念 1.1  什么是线程 1.2  线程的好处 2.   创建线程的三种方式 注意事项 2.1  继承Thread类 2.1.1 认识  2.1.2  编码实现  package cn.hdc.oop10.Thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象