根据EXIF时间对照片重命名

1033 查看

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

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-2.3.1