根据图片的exif时间信息-重命名jpg文件

由于本人经常上poco网看一些摄影师的作品,遇到喜欢的就会保存到本地,于是乎久了积了一堆的图片,自己做一个工具代码来整理这些图片,很简单就是根据图片的exif信息来重命名图片。
  首先可以简单了解下关于如何得到exif信息。参考: http://yexin218.iteye.com/blog/340884http://www.drewnoakes.com/code/exif/
实现代码如下:
import java.io.File;

import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.MetadataException;
import com.drew.metadata.Tag;
import com.drew.metadata.exif.ExifDirectory;

public class ReNamePic {
   
	/**
	 * @param args
	 */
	private String srcFilePath;    //修改前图片所在文件夹
	private String desFilePath;   //修改后图片所在文件夹
	
	public String getDesFilePath() {
		return desFilePath;
	}
	public void setDesFilePath(String desFilePath) {
		this.desFilePath = desFilePath;
	}
	public String getSrcFilePath() {
		return srcFilePath;
	}
	public void setSrcFilePath(String srcFilePath) {
		this.srcFilePath = srcFilePath;
	}
	
	public ReNamePic(String srcFilePath,String desFilePath) { //带参数的构造方法
		super();
	    this.srcFilePath = srcFilePath;
	    this.desFilePath = desFilePath;
	}
	
	protected void run(String dir){
    	 try {
             File d = new File(this.srcFilePath);
             if (!d.isDirectory()) {
                 System.out.println("文件路径错误");
            	 return;
             }
             File[] files = d.listFiles();
             for (int i = 0; i < files.length; i++) {
                 if (files[i].isDirectory()) {
                     run(files[i].getAbsolutePath());
                 } else {
                     String filename = files[i].getAbsolutePath(); //得到图片绝对路径
                     if(filename.endsWith("jpg")){
                     doReName(filename);
                     }
                  }
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
    }
    public void doReName(String filePath){
    	File  jf=new File(filePath);
		try {
			Metadata metadata = JpegMetadataReader.readMetadata(jf);
			Directory exif = metadata.getDirectory(ExifDirectory.class); 
			 if(exif.containsTag(ExifDirectory.TAG_DATETIME)){  
	              try {
	            	  String reName=exif.getDescription(ExifDirectory.TAG_DATETIME).replace(":","_").replace(" ","_"); //去除时间中的:和空格
				      System.out.println(reName);
	            	  jf.renameTo(new File(desFilePath+reName+".jpg"));
	              } catch (MetadataException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}   
	         }else{
	        	 System.out.println("没有exif信息:    "+filePath);
	        	 
	         }
		} catch (JpegProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
    	
    }
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ReNamePic rep=new ReNamePic("C:/Documents and Settings/Administrator/桌面/文件/","C:/Documents and Settings/Administrator/桌面/文件2/");
		rep.run("");
	    
	}

}

你可能感兴趣的:(C++,c,C#,Blog)