package com.artozi.util.image;
import java.io.*; uaxLinux联盟
import java.sql.*; uaxLinux联盟
import java.util.*;
import com.drew.imaging.jpeg.*; uaxLinux联盟
import com.drew.metadata.*; uaxLinux联盟
import com.drew.metadata.exif.ExifDirectory;
public class ExifInfoWrapper { uaxLinux联盟
private Directory exifDirectory = null; uaxLinux联盟
private static ExifInfoWrapper eiw = null; uaxLinux联盟
public static ExifInfoWrapper getInstance(String filename){ uaxLinux联盟
if (eiw == null) { uaxLinux联盟
eiw = new ExifInfoWrapper(filename); uaxLinux联盟
} uaxLinux联盟
return eiw; uaxLinux联盟
}
public ExifInfoWrapper(String filename){ uaxLinux联盟
File jpegFile = new File(filename); uaxLinux联盟
try { uaxLinux联盟
Metadata metadata = JpegMetadataReader.readMetadata(jpegFile);
//读取jpeg源数据信息 uaxLinux联盟
exifDirectory = metadata.getDirectory(ExifDirectory.class);
//读取jpeg中exif目录 uaxLinux联盟
} catch (JpegProcessingException je) { uaxLinux联盟
System.out.println(je); uaxLinux联盟
} catch (Exception ex) { uaxLinux联盟
System.out.println(ex); uaxLinux联盟
} uaxLinux联盟
}
public String getPhotoCreatTime() { uaxLinux联盟
String time = null; uaxLinux联盟
try { uaxLinux联盟
if(exifDirectory.containsTag(ExifDirectory.TAG_DATETIME_ORIGINAL)){ uaxLinux联盟
java.util.Date d = exifDirectory.getDate(ExifDirectory. uaxLinux联盟
TAG_DATETIME_ORIGINAL); //将exif中的日期信息读出 uaxLinux联盟
Timestamp ts = new Timestamp(d.getTime()); uaxLinux联盟
time = ts.toString(); uaxLinux联盟
}else{ uaxLinux联盟
time = ""; uaxLinux联盟
} uaxLinux联盟
} catch (MetadataException ex) { uaxLinux联盟
System.out.println(ex); uaxLinux联盟
time = ""; uaxLinux联盟
} catch(Exception e){ uaxLinux联盟
System.out.println(e); uaxLinux联盟
time = ""; uaxLinux联盟
}finally{ uaxLinux联盟
return time; uaxLinux联盟
} uaxLinux联盟
}
public String showCrameBrand(){ uaxLinux联盟
String brand = null; uaxLinux联盟
try { uaxLinux联盟
if(exifDirectory.containsTag(ExifDirectory.TAG_MODEL)){ uaxLinux联盟
brand = exifDirectory.getString(ExifDirectory. uaxLinux联盟
TAG_MODEL); //将exif中的日期信息读出 uaxLinux联盟
uaxLinux联盟
}else{ uaxLinux联盟
brand = ""; uaxLinux联盟
} uaxLinux联盟
} catch(Exception e){ uaxLinux联盟
System.out.println(e); uaxLinux联盟
}finally{ uaxLinux联盟
return brand; uaxLinux联盟
}
} uaxLinux联盟
uaxLinux联盟
public static void main(String[] args) { uaxLinux联盟
ExifInfoWrapper e = ExifInfoWrapper.getInstance("path"); uaxLinux联盟
System.out.println(e.getPhotoCreatTime()); uaxLinux联盟
System.out.println(e.showCrameBrand()); uaxLinux联盟
}
}