使用Java程序读取JPG Tif等格式图片的exif信息

Posted 码代码的小表哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Java程序读取JPG Tif等格式图片的exif信息相关的知识,希望对你有一定的参考价值。

package com.util;

import java.io.File;
import java.util.Iterator;

import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.MetadataException;
import com.drew.metadata.exif.ExifDirectory;
import com.drew.metadata.exif.ExifReader;
public class ExifUtil {

public static void main(String[] args) {
try {

ExifReader er =new ExifReader(new File("填照片路径"));//也就用inputstream 或者二进制流
Metadata exif = er.extract();
Iterator itr = exif.getDirectoryIterator();
while (itr.hasNext()) {
Directory directory = (Directory) itr.next();
System.out.println(directory.getString(ExifDirectory.TAG_EXIF_VERSION));
System.out.println(directory.getString(ExifDirectory.TAG_MAKE));
System.out.println(directory.getString(ExifDirectory.TAG_MODEL));
System.out.println(directory.getString(ExifDirectory.TAG_FNUMBER));
System.out.println(directory.getString(ExifDirectory.TAG_EXPOSURE_TIME));
System.out.println(directory.getString(ExifDirectory.TAG_ISO_EQUIVALENT));
System.out.println(directory.getString(ExifDirectory.TAG_SOFTWARE));
System.out.println(directory.getDate(ExifDirectory.TAG_DATETIME_DIGITIZED));
System.out.println(directory.getDate(ExifDirectory.TAG_DATETIME_ORIGINAL));
System.out.println(directory.getString(ExifDirectory.TAG_ORIENTATION));
System.out.println(directory.getString(ExifDirectory.TAG_EXIF_IMAGE_WIDTH));
System.out.println(directory.getString(ExifDirectory.TAG_EXIF_IMAGE_HEIGHT));
System.out.println(directory.getString(ExifDirectory.TAG_X_RESOLUTION));
System.out.println(directory.getString(ExifDirectory.TAG_Y_RESOLUTION));
System.out.println(directory.getString(ExifDirectory.TAG_EXPOSURE_BIAS));
}
} catch (ImageProcessingException e) {
e.printStackTrace();
} catch (MetadataException e) {
e.printStackTrace();
}
}
}

 

 

 

 

下面给一个实体类,可以方便存储exif信息

package com.util;

import java.util.Date;

/**
* 照片信息
*/
public class Photo {
// EXIF版本
private String exifversion = null;
// 相机品牌
private String make = null;
// 相机型号
private String model = null;
// 光 圈 值
private String fnumber = null;
// 快 門
private String exposuretime = null;
// 感 光 度
private String isoequivalent = null;
// 软 件
private String software = null;
// 原始拍摄时间
private Date originaldatetime = null;
// 数字化时间
private Date digitizeddatetime = null;
// 方 向
private String orientation = null;
// 图片宽度
private String exifimagewidth = null;
// 图片高度
private String exifimageheight = null;
// 水平分辨率
private String xresolution = null;
// 垂直分辨率
private String yresolution = null;
// 曝光补偿
private String exposurebias = null;

public String getExifversion() {
return exifversion;
}

public void setExifversion(String exifversion) {
this.exifversion = exifversion;
}

public String getMake() {
return make;
}

public void setMake(String make) {
this.make = make;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

public String getFnumber() {
return fnumber;
}

public void setFnumber(String fnumber) {
this.fnumber = fnumber;
}

public String getExposuretime() {
return exposuretime;
}

public void setExposuretime(String exposuretime) {
this.exposuretime = exposuretime;
}

public String getIsoequivalent() {
return isoequivalent;
}

public void setIsoequivalent(String isoequivalent) {
this.isoequivalent = isoequivalent;
}

public String getSoftware() {
return software;
}

public void setSoftware(String software) {
this.software = software;
}

public Date getOriginaldatetime() {
return originaldatetime;
}

public void setOriginaldatetime(Date originaldatetime) {
this.originaldatetime = originaldatetime;
}

public Date getDigitizeddatetime() {
return digitizeddatetime;
}

public void setDigitizeddatetime(Date digitizeddatetime) {
this.digitizeddatetime = digitizeddatetime;
}

public String getOrientation() {
return orientation;
}

public void setOrientation(String orientation) {
this.orientation = orientation;
}

public String getExifimagewidth() {
return exifimagewidth;
}

public void setExifimagewidth(String exifimagewidth) {
this.exifimagewidth = exifimagewidth;
}

public String getExifimageheight() {
return exifimageheight;
}

public void setExifimageheight(String exifimageheight) {
this.exifimageheight = exifimageheight;
}

public String getXresolution() {
return xresolution;
}

public void setXresolution(String xresolution) {
this.xresolution = xresolution;
}

public String getYresolution() {
return yresolution;
}

public void setYresolution(String yresolution) {
this.yresolution = yresolution;
}

public String getExposurebias() {
return exposurebias;
}

public void setExposurebias(String exposurebias) {
this.exposurebias = exposurebias;
}

@Override
public String toString() {
return "数字化时间:" + digitizeddatetime
+ ", 图片高度:" + exifimageheight + ", 图片宽度:"
+ exifimagewidth + ", EXIF版本:" + exifversion
+ ",曝光补偿:" + exposurebias + ", 快 門:"
+ exposuretime + ", 光 圈 值:" + fnumber + ", 感 光 度:"
+ isoequivalent + ", 相机品牌:" + make + ", 相机型号:" + model
+ ", 方 向:" + orientation + ", 原始拍摄时间:"
+ originaldatetime + ", 软 件:" + software
+ ", 水平分辨率:" + xresolution + ", 垂直分辨率:"
+ yresolution ;
}

}

 

 

 

 

metadata-extractor-2.4.0-beta-1.jar



















































































































































以上是关于使用Java程序读取JPG Tif等格式图片的exif信息的主要内容,如果未能解决你的问题,请参考以下文章

怎样把TIF转化为JPG图片格式

基于GDAL库图像读写——涉及(tif/tiff/bmp/jpg/png/gif等)多种格式图像的I/O

bmp,jpg,png,tif,wmf,emf与eps图片格式转换

Java如何把一个PDF转为tif

java 如何进行 bmp格式的图片转换成jpg格式

数字图像-概述