好久没有来JR了,大概有1年多了,之前一直“野”在外面,这次回来给大家带来点好玩的技术,这次给大家说一下怎样读取数码照片里的Exif信息。
其实数码照片文件中除了Exif,还有GPS、相机厂商信息等,这些都会在相机拍摄时写入照片中,大家可以下载一个Opanda IExif 2.25软件玩玩。
接下来说的是怎样使用我们喜欢的Java语言来读取,首页需要一个第三方的类包附件,这是个老外写的好不轻易找到的。
然后就是写个测试类了。
package com.artozi.util.image;
import java.io.*;
import java.sql.*;
import java.util.*;
import com.drew.imaging.jpeg.*;
import com.drew.metadata.*;
import com.drew.metadata.exif.ExifDirectory;
public class ExifInfoWrapper {
private Directory exifDirectory = null;
private static ExifInfoWrapper eiw = null;
public static ExifInfoWrapper getInstance(String filename){
if (eiw == null) {
eiw = new ExifInfoWrapper(filename);
}
return eiw;
}
public ExifInfoWrapper(String filename){
File jpegFile = new File(filename);
try {
Metadata metadata = JpegMetadataReader.readMetadata(jpegFile); //读取jpeg源数据信息
exifDirectory = metadata.getDirectory(ExifDirectory.class); //读取jpeg中exif目录
} catch (JpegProcessingException je) {
System.out.println(je);
} catch (Exception ex) {
System.out.println(ex);
}
}
public String getPhotoCreatTime() {
String time = null;
try {
if(exifDirectory.containsTag(ExifDirectory.TAG_DATETIME_ORIGINAL)){
java.util.Date d = exifDirectory.getDate(ExifDirectory.
TAG_DATETIME_ORIGINAL); //将exif中的日期信息读出
Timestamp ts = new Timestamp(d.getTime());
time = ts.toString();
}else{
time = "";
}
} catch (MetadataException ex) {
System.out.println(ex);
time = "";
} catch(Exception e){
System.out.println(e);
time = "";
}finally{
return time;
}
}
public String showCrameBrand(){
String brand = null;
try {
if(exifDirectory.containsTag(ExifDirectory.TAG_MODEL)){
brand = exifDirectory.getString(ExifDirectory.
TAG_MODEL); //将exif中的日期信息读出
}else{
brand = "";
}
} catch(Exception e){
System.out.println(e);
}finally{
return brand;
}
}
public static void main(String[] args) {
ExifInfoWrapper e = ExifInfoWrapper.getInstance("path");
System.out.println(e.getPhotoCreatTime());
System.out.println(e.showCrameBrand());
}
}