| |
好久没有来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());
- }
- }
之后大家在main方法中填入自己照片的路径,然后大家看吧,我只显示了拍摄日期和相机型号,exifdirectory是个常量类里面有很多属性,大家感兴趣的话可以试试其他的,我已经把这个小技术用在我自己的网站中 artozi ,多提意见,今天就说这些,下次想想带点什么好玩的来。
|
|