根据EXIF时间对照片重命名

照片的命名乱七八糟的,对命名规则有洁癖的我自然要修改一下,于是乎。。。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.io.File;
import java.util.Iterator;

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

public class RenamePic {
public static void main(String[] args) {
try {
String dir = "D:/uri/";
File directory = new File(dir);
File[] files = directory.listFiles();
for (int i = 0; i < files.length; i++) {
File jpegFile = new File(dir + files[i].getName());
Metadata metadata = JpegMetadataReader.readMetadata(jpegFile);
Directory exif = metadata.getDirectory(ExifDirectory.class);
Iterator tags = exif.getTagIterator();
while (tags.hasNext()) {
Tag tag = (Tag) tags.next();
if (tag.getTagName().equals("Date/Time Original")) {
String time = tag.getDescription().replaceAll(":", "")
.replaceAll(" ", "_");
File reFile = new File(dir + "IMG_" + time + ".jpg");
jpegFile.renameTo(reFile);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

相关jar包:metadata-extractor.jar