读取照片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);
}
}
}
}