Skip to content

Instantly share code, notes, and snippets.

@wei-spring
Last active November 28, 2020 08:16
Show Gist options
  • Save wei-spring/f6680bbe6f26bcc43cc9c574eaddc0fd to your computer and use it in GitHub Desktop.
Save wei-spring/f6680bbe6f26bcc43cc9c574eaddc0fd to your computer and use it in GitHub Desktop.
查看图片Exif信息
package com.kpl.share;
import com.drew.imaging.ImageMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import com.drew.metadata.exif.ExifIFD0Directory;
import com.drew.metadata.exif.ExifSubIFDDirectory;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Copyright © 2018 100wlc. All Rights Reserved.
* Author: wei_spring
* Date:2018/11/22.
* Function: 查看图片信息工具类,需要的依赖库:com.drewnoakes:metadata-extractor:2.11.0
*/
public class ImageUtil {
/**
* 注:
* Image Description 图像描述、来源. 指生成图像的工具
* Artist 作者 有些相机可以输入使用者的名字
* Make 生产者 指产品生产厂家
* Model 型号 指设备型号
* Orientation方向 有的相机支持,有的不支持
* XResolution/YResolution X/Y方向分辨率 本栏目已有专门条目解释此问题。
* ResolutionUnit分辨率单位 一般为PPI
* Software软件 显示固件Firmware版本
* DateTime日期和时间
* YCbCrPositioning 色相定位
* ExifOffsetExif信息位置,定义Exif在信息在文件中的写入,有些软件不显示。
* ExposureTime 曝光时间 即快门速度
* FNumber光圈系数
* ExposureProgram曝光程序 指程序式自动曝光的设置,各相机不同,可能是Sutter Priority(快门优先)、Aperture Priority(快门优先)等等。
* ISO speed ratings感光度
* ExifVersionExif版本
* DateTimeOriginal创建时间
* DateTimeDigitized数字化时间
* ComponentsConfiguration图像构造(多指色彩组合方案)
* CompressedBitsPerPixel(BPP)压缩时每像素色彩位 指压缩程度
* ExposureBiasValue曝光补偿。
* MaxApertureValue最大光圈
* MeteringMode测光方式, 平均式测光、中央重点测光、点测光等。
* Lightsource光源 指白平衡设置
* Flash是否使用闪光灯。
* FocalLength焦距,一般显示镜头物理焦距,有些软件可以定义一个系数,从而显示相当于35mm相机的焦距 MakerNote(User Comment)作者标记、说明、记录
* FlashPixVersionFlashPix版本 (个别机型支持)
* ColorSpace色域、色彩空间
* ExifImageWidth(Pixel X Dimension)图像宽度 指横向像素数
* ExifImageLength(Pixel Y Dimension)图像高度 指纵向像素数
* Interoperability IFD通用性扩展项定义指针 和TIFF文件相关,具体含义不详
* FileSource源文件 Compression压缩比。
*/
public static void main(String[] args) throws Exception {
//Nokia
//String path = "/Users/wei_spring/Desktop/IMG_20181122_151528.jpg";
//HuaWei
String path = "/Users/wei_spring/Desktop/WechatIMG88.jpeg";
//HuaWei PS
//String path = "/Users/wei_spring/Desktop/IMG_20181122_153332.jpg";
System.err.println("---------------------------------------");
getInfo(path);
System.err.println("---------------------------------------");
}
private static void getInfo(String path) {
File imageFile = new File(path);
try {
Metadata metadata = ImageMetadataReader.readMetadata(imageFile);
for (Directory directory : metadata.getDirectories()) {
if (directory == null) {
continue;
}
for (Tag tag : directory.getTags()) {
String tagName = tag.getTagName(); // 标签名
String desc = tag.getDescription(); // 标签信息
String directName = tag.getDirectoryName();//文档名字
if (tagName.equals("GPS Latitude")) {
System.err.println("纬度 : " + desc);
} else if (tagName.equals("GPS Longitude")) {
System.err.println("经度: " + desc);
} else if (tagName.equals("GPS Altitude")) {
System.err.println("海拔: " + desc);
} else if (tagName.equals("Date/Time Original")) {
SimpleDateFormat yyyymmddhhmmss = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
Date date = yyyymmddhhmmss.parse(desc);
SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("拍照时间: " + newFormat.format(date));
} else if (tagName.equals("Date/Time")) {
SimpleDateFormat yyyymmddhhmmss = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
Date date = yyyymmddhhmmss.parse(desc);
SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("创建时间: " + newFormat.format(date));
} else if (tagName.equals("Image Width")) {
System.err.println("图片宽: " + Integer.parseInt(desc.replaceAll("[^0-9]", "").trim()));
} else if (tagName.equals("Image Height")) {
System.err.println("图片高: " + Integer.parseInt(desc.replaceAll("[^0-9]", "").trim()));
} else if (tagName.equals("X Resolution")) {
System.out.println("水平分辨率: " + Integer.parseInt(desc.replaceAll("[^0-9]", "").trim()));
} else if (tagName.equals("Y Resolution")) {
System.out.println("垂直分辨率: " + Integer.parseInt(desc.replaceAll("[^0-9]", "").trim()));
}
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_FNUMBER)) {
//光圈F值=镜头的焦距/镜头光圈的直径
System.out.println("光圈值:" + directory.getDescription(ExifSubIFDDirectory.TAG_FNUMBER));
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_EXPOSURE_TIME)) {
System.out.println("曝光时间: " + directory.getString(ExifSubIFDDirectory.TAG_EXPOSURE_TIME) + "秒");
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_ISO_EQUIVALENT)) {
System.out.println("ISO速度: " + directory.getString(ExifSubIFDDirectory.TAG_ISO_EQUIVALENT));
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_FOCAL_LENGTH)) {
System.out.println("焦距: " + directory.getString(ExifSubIFDDirectory.TAG_FOCAL_LENGTH) + "毫米");
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_MAX_APERTURE)) {
System.out.println("最大光圈:" + directory.getDouble(ExifSubIFDDirectory.TAG_MAX_APERTURE));
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_EXIF_IMAGE_WIDTH)) {
System.out.println("宽: " + directory.getString(ExifIFD0Directory.TAG_EXIF_IMAGE_WIDTH));//ExifIFD0Directory
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_EXIF_IMAGE_HEIGHT)) {
System.out.println("高: " + directory.getString(ExifSubIFDDirectory.TAG_EXIF_IMAGE_HEIGHT));
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_MAKE)) {
System.out.println("照相机制造商: " + directory.getString(ExifSubIFDDirectory.TAG_MAKE));
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_MODEL)) {
System.out.println("照相机型号: " + directory.getString(ExifSubIFDDirectory.TAG_MODEL));
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_X_RESOLUTION)) {
System.out.println("水平分辨率(X方向分辨率): " + directory.getString(ExifSubIFDDirectory.TAG_X_RESOLUTION));
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_Y_RESOLUTION)) {
System.out.println("垂直分辨率(Y方向分辨率): " + directory.getString(ExifSubIFDDirectory.TAG_Y_RESOLUTION));
}
//其他参数测试开始
if (directory.containsTag(ExifSubIFDDirectory.TAG_SOFTWARE)) {
//Software软件 显示固件Firmware版本
System.out.println("显示固件Firmware版本(图片详细信息的程序名称):" + directory.getString(ExifSubIFDDirectory.TAG_SOFTWARE));
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_35MM_FILM_EQUIV_FOCAL_LENGTH)) {
System.out.println("35mm焦距:" + directory.getString(ExifSubIFDDirectory.TAG_35MM_FILM_EQUIV_FOCAL_LENGTH));
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_APERTURE)) {
System.out.println("孔径(图片分辨率单位):" + directory.getString(ExifSubIFDDirectory.TAG_APERTURE));
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_APPLICATION_NOTES)) {
//一般无
System.out.println("应用程序记录:" + directory.getString(ExifSubIFDDirectory.TAG_APPLICATION_NOTES));
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_ARTIST)) {
//作者
System.out.println("作者:" + directory.getString(ExifSubIFDDirectory.TAG_ARTIST));
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_BODY_SERIAL_NUMBER)) {
System.out.println("TAG_BODY_SERIAL_NUMBER:" + directory.getString(ExifSubIFDDirectory.TAG_BODY_SERIAL_NUMBER));
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_METERING_MODE)) {
//MeteringMode测光方式, 平均式测光、中央重点测光、点测光等
System.out.println("点测光值:" + directory.getString(ExifSubIFDDirectory.TAG_METERING_MODE));
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_RESOLUTION_UNIT)) {
//XResolution/YResolution X/Y方向分辨率
System.out.println("分辨率单位:" + directory.getString(ExifSubIFDDirectory.TAG_RESOLUTION_UNIT));
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_EXPOSURE_BIAS)) {
System.out.println("曝光补偿:" + directory.getDouble(ExifSubIFDDirectory.TAG_EXPOSURE_BIAS));
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_COLOR_SPACE)) {
System.out.println("色域、色彩空间:" + directory.getString(ExifSubIFDDirectory.TAG_COLOR_SPACE));
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_YCBCR_COEFFICIENTS)) {
System.out.println("色相系数:" + directory.getString(ExifSubIFDDirectory.TAG_YCBCR_COEFFICIENTS));
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_YCBCR_POSITIONING)) {
System.out.println("色相定位:" + directory.getString(ExifSubIFDDirectory.TAG_YCBCR_POSITIONING));
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_YCBCR_SUBSAMPLING)) {
System.out.println("色相抽样:" + directory.getString(ExifSubIFDDirectory.TAG_YCBCR_SUBSAMPLING));
}
if (directory.containsTag(ExifSubIFDDirectory.TAG_EXIF_VERSION)) {
System.out.println("exif版本号:" + directory.getString(ExifSubIFDDirectory.TAG_EXIF_VERSION));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment