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

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


冰剑
898 声望26 粉丝

轻击键盘,静侯回音。