背景不过多介绍,就是想获取并修改照片中的拍摄时间、经纬度等信息,并且当在windows环境下,图片会有标题、主题、描述等属性,如果可以,希望能够一并获取。
可能是需求比较奇怪,这方面的内容在国内很少,最终发现org.apache.commons-imaging
包可以解决这个问题。
gradle:
implementation 'org.apache.commons:commons-imaging:1.0-alpha1'
maven:
public void buildPhoto(File file) throws IOException, ImageReadException, ImageWriteException {
ImageMetadata imageMetadata = Imaging.getMetadata(file);
JpegImageMetadata metadata = (JpegImageMetadata) imageMetadata;
//遍历所有图片属性
for (ImageMetadata.ImageMetadataItem item : metadata.getItems()) {
System.out.println(item);
}
//获取标题属性的值
if (metadata.findEXIFValue(MicrosoftTagConstants.EXIF_TAG_XPTITLE) != null) {
Object titleValue = metadata.findEXIFValue(MicrosoftTagConstants.EXIF_TAG_XPTITLE).getValue();
System.out.println(titleValue);
}
//获取经纬度信息
TiffImageMetadata exif = metadata.getExif();
if (exif != null && exif.getGPS() != null) {
TiffImageMetadata.GPSInfo gpsInfo = exif.getGPS();
double lOngitudeAsDegreesEast= gpsInfo.getLongitudeAsDegreesEast();
double latitudeAsDegreesNorth = gpsInfo.getLatitudeAsDegreesNorth();
}
//修改某个属性值
TiffOutputSet out = exif.getOutputSet();
//获取TiffOutputDirectory
TiffOutputDirectory exifDirectory = out.getOrCreateExifDirectory();
//拍摄日期修改 注意日期格式为 yyyy:MM:dd HH:mm:ss
exifDirectory.removeField(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL);
exifDirectory.removeField(ExifTagConstants.EXIF_TAG_DATE_TIME_DIGITIZED);
exifDirectory.add(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL, "2022:01:01 00:00:00");
exifDirectory.add(ExifTagConstants.EXIF_TAG_DATE_TIME_DIGITIZED, "2022:01:01 00:00:00");
//gps信息修改
out.setGPSInDegrees(116.23128, 40.22077);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("path"));
//写图片
new ExifRewriter().updateExifMetadataLossless(file, bos, out);
}
照片属性遍历结果
ImageDescription: 'title'
Make: 'SONY'
Model: 'ILCE-7M3'
Orientation: 1
XResolution: 350
YResolution: 350
ResolutionUnit: 2
Software: 'ILCE-7M3 v3.10t'
DateTime: '2022:04:16 11:47:59'
YCbCrPositioning: 2
Rating: 4
RatingPercent: 75
ExifOffset: 4202
GPSInfo: 4958
XPTitle: 'title'
XPComment: 'this is a comments'
XPKeywords: '标记'
XPSubject: 'ubject'
PrintIM: 80, 114, 105, 110, 116, 73, 77, 0, 48, 51, 48, 48, 0, 0, 3, 0, 2, 0, 1, 0, 0, 0, 3, 0, 34, 0, 0, 0, 1, 1, 0, 0, 0, 0, 9, 17, 0, 0, 16, 39, 0, 0, 11, 15, 0, 0, 16, 39, 0, 0, -105... (106)
Padding: 28, -22, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0... (1928)
ExposureTime: 1/640 (0.002)
FNumber: 5
ExposureProgram: 2
PhotographicSensitivity: 100
Unknown Tag (0x8830): 2
Unknown Tag (0x8832): 100
ExifVersion: 48, 50, 51, 49
DateTimeOriginal: '2022:04:16 11:47:59'
DateTimeDigitized: '2022:04:16 11:47:59'
Unknown Tag (0x9010): '+08:00'
Unknown Tag (0x9011): '+08:00'
Unknown Tag (0x9012): '+08:00'
ComponentsConfiguration: 1, 2, 3, 0
CompressedBitsPerPixel: 3
BrightnessValue: 25044/2560 (9.783)
ExposureCompensation: 0
MaxApertureValue: 760/256 (2.969)
MeteringMode: 5
LightSource: 0
Flash: 16
FocalLength: 34
MakerNote: 83, 79, 78, 89, 32, 68, 83, 67, 32, 0, 0, 0, 102, 0, 3, 16, 4, 0, 16, 0, 0, 0, -84, 8, 0, 0, 0, 32, 7, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 32, 4, 0, 1, 0, 0, 0, 0, 0, 0, 0, 3... (37672)
FlashpixVersion: 48, 49, 48, 48
ColorSpace: 1
ExifImageWidth: 6000
ExifImageLength: 4000
InteropOffset: 2230
FileSource: 3
SceneType: 1
CustomRendered: 0
ExposureMode: 0
WhiteBalance: 0
DigitalZoomRatio: 1
FocalLengthIn35mmFormat: 34
SceneCaptureType: 0
Contrast: 0
Saturation: 0
Sharpness: 0
LensSpecification: 28, 75, 28/10 (2.8), 28/10 (2.8)
LensModel: 'E 28-75mm F2.8-2.8'
Padding: 28, -22, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0... (2060)
OffsetSchema: 4050
InteroperabilityVersion: 48, 49, 48, 48
GPSLatitudeRef: 'N'
GPSLatitude: 40, 13, 3693/250 (14.772)
GPSLongitudeRef: 'E'
GPSLongitude: 116, 13, 6576/125 (52.608)
Compression: 6
ImageDescription: ''
Make: 'SONY'
Model: 'ILCE-7M3'
Orientation: 1
XResolution: 72
YResolution: 72
ResolutionUnit: 2
Software: 'ILCE-7M3 v3.10'
DateTime: '2022:04:16 11:47:59'
JpgFromRawStart: 42798
JpgFromRawLength: 9229
YCbCrPositioning: 2