2012年9月26日星期三

根据EXIF时间对照片重命名

根据EXIF时间对照片重命名

 1 import java.io.File; 2  import java.util.Iterator; 3   4  import com.drew.imaging.jpeg.JpegMetadataReader; 5  import com.drew.metadata.Directory; 6  import com.drew.metadata.Metadata; 7  import com.drew.metadata.Tag; 8  import com.drew.metadata.exif.ExifDirectory; 9  10  public class RenamePic {11      public static void main(String[] args) {12          try {13              String dir = "D:/uri/";14              File directory = new File(dir);15              File[] files = directory.listFiles();16              for (int i = 0; i < files.length; i++) {17                  File jpegFile = new File(dir + files[i].getName());18                  Metadata metadata = JpegMetadataReader.readMetadata(jpegFile);19                  Directory exif = metadata.getDirectory(ExifDirectory.class);20                  Iterator tags = exif.getTagIterator();21                  while (tags.hasNext()) {22                      Tag tag = (Tag) tags.next();23                      if (tag.getTagName().equals("Date/Time Original")) {24                          String time = tag.getDescription().replaceAll(":", "")25                                  .replaceAll(" ", "_");26                          File reFile = new File(dir + "IMG_" + time + ".jpg");27                          jpegFile.renameTo(reFile);28                      }29                  }30              }31          } catch (Exception e) {32              e.printStackTrace();33          }34      }35  }36  

JAR文件:metadata-extractor-2.3.1.jar


TAG: