1 /** 2 * Copyright 2011, Aiki IT, FotoRenamer 3 * <p/> 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * <p/> 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * <p/> 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package de.aikiit.fotorenamer.image; 17 18 import de.aikiit.fotorenamer.exception.InvalidDirectoryException; 19 import de.aikiit.fotorenamer.exception.NoFilesFoundException; 20 import org.apache.commons.imaging.ImageReadException; 21 import org.apache.logging.log4j.LogManager; 22 import org.apache.logging.log4j.Logger; 23 24 import java.io.File; 25 import java.io.IOException; 26 27 /** 28 * This class transforms picture file names. All relevant files 29 * in the target directory get a new file name - if correct 30 * metadata can be extracted from the files' EXIF file headers. 31 * <br> 32 * A picture <code>foo.jpg</code> is renamed to 33 * <code>201108111100_foo.jpg</code> if the picture's creation date 34 * was 2011-08-11 11:00. 35 * <br> 36 * Files without EXIF metadata are not touched at all. 37 * 38 * @author hirsch 39 * @version 2011-06-02, 13:22 40 */ 41 public class CreationDateFromExifImageRenamer extends AbstractImageRenamer { 42 43 /** Logger for this class. */ 44 private static final Logger LOG = 45 LogManager.getLogger(CreationDateFromExifImageRenamer.class); 46 47 /** 48 * The given directory is scanned for image files that 49 * are processed. 50 * 51 * @param targetDirectory Name of the directory to work on. 52 * @throws InvalidDirectoryException If there's a problem with the directory 53 * selected. 54 * @throws NoFilesFoundException if the selected directory is empty. 55 */ 56 public CreationDateFromExifImageRenamer(final String targetDirectory) throws 57 InvalidDirectoryException, NoFilesFoundException { 58 super(targetDirectory); 59 } 60 61 /** 62 * Extracts creation date from EXIF information and returns new filename. If 63 * an error occurs during EXIF data extraction the original filename is 64 * returned. 65 * 66 * @param imageFile Filename to renameFiles according to the subclass 67 * implementation. 68 * @return New filename that includes the image's creation date. 69 * @see MetaDataExtractor for more information about the file format. 70 */ 71 @Override 72 public final String renameImage(final File imageFile) { 73 String newImageName = imageFile.getName(); 74 LOG.info("Start renaming in CreationDateFromExifImageRenamer"); 75 76 try { 77 newImageName = MetaDataExtractor.generateCreationDateInCorrectFormat(imageFile); 78 } catch (ImageReadException | IOException e) { 79 LOG.error("Error during exif date extraction: ", e); 80 return newImageName; 81 } 82 83 return newImageName; 84 } 85 }