View Javadoc
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.logging.log4j.LogManager;
21  import org.apache.logging.log4j.Logger;
22  
23  import java.io.File;
24  import java.io.IOException;
25  
26  /**
27   * This class transforms picture file names. All relevant files
28   * in the target directory get a new file name - if correct
29   * metadata can be extracted from the files' EXIF file headers.
30   * <br>
31   * A picture <code>foo.jpg</code> is renamed to
32   * <code>201108111100_foo.jpg</code>  if the picture's creation date
33   * was 2011-08-11 11:00.
34   * <br>
35   * Files without EXIF metadata are not touched at all.
36   *
37   * @author hirsch
38   * @version 2011-06-02, 13:22
39   */
40  public final class CreationDateFromExifImageRenamer extends AbstractImageRenamer {
41  
42      /** Logger for this class. */
43      private static final Logger LOG =
44              LogManager.getLogger(CreationDateFromExifImageRenamer.class);
45  
46      /**
47       * The given directory is scanned for image files that
48       * are processed.
49       *
50       * @param targetDirectory Name of the directory to work on.
51       * @throws InvalidDirectoryException If there's a problem with the directory
52       *                                   selected.
53       * @throws NoFilesFoundException     if the selected directory is empty.
54       */
55      public CreationDateFromExifImageRenamer(final String targetDirectory) throws
56              InvalidDirectoryException, NoFilesFoundException {
57          super(targetDirectory);
58      }
59  
60      /**
61       * Extracts creation date from EXIF information and returns new filename. If
62       * an error occurs during EXIF data extraction the original filename is
63       * returned.
64       *
65       * @param imageFile Filename to renameFiles according to the subclass
66       *                  implementation.
67       * @return New filename that includes the image's creation date.
68       * @see MetaDataExtractor for more information about the file format.
69       */
70      @Override
71      public final String renameImage(final File imageFile) {
72          String newImageName = imageFile.getName();
73          LOG.info("Start renaming in CreationDateFromExifImageRenamer");
74  
75          try {
76              newImageName = MetaDataExtractor.generateCreationDateInCorrectFormat(imageFile);
77          } catch (IOException e) {
78              LOG.error("Error during exif date extraction: ", e);
79              return newImageName;
80          }
81  
82          return newImageName;
83      }
84  }