java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java
3 * Copyright © 2014 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package cz.frantovo.copyImageResizer;
20 import cz.frantovo.copyImageResizer.SingleImageResizer.ImageFormat;
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.FileOutputStream;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
30 * @author Ing. František Kučera (frantovo.cz)
32 public class RecursiveImageResizer {
34 private static final Logger log = Logger.getLogger(RecursiveImageResizer.class.getName());
36 private final SingleImageResizer resizer = new SingleImageResizer();
38 public void resize(RecursiveOptions options) throws RecursiveException, ResizeException {
39 resizeDirectory(options.getInput(), options);
42 private void resizeFile(File file, RecursiveOptions options) throws ResizeException {
43 log.log(Level.FINE, "Resizing file: {0}", relativize(options.getInput(), file));
45 ImageFormat format = ImageFormat.getMatching(file.getName());
48 log.log(Level.FINE, "Skipping file: {0} (no image format matched this extension)", relativize(options.getInput(), file));
51 FileInputStream input = new FileInputStream(file);
52 File relative = relativize(options.getInput(), file);
53 File outputFile = new File(options.getOutput(), relative.getPath());
54 FileOutputStream output = new FileOutputStream(outputFile);
55 resizer.resize(input, output, format);
56 } catch (FileNotFoundException e) {
57 throw new ResizeException("Error while opening stream", e);
63 private void resizeDirectory(File directory, RecursiveOptions options) throws ResizeException {
65 log.log(Level.FINE, "Resizing directory: {0}", directory);
67 File relative = relativize(options.getInput(), directory);
68 File output = new File(options.getOutput(), relative.getPath());
71 for (File entry : directory.listFiles()) {
72 if (entry.isDirectory()) {
73 resizeDirectory(entry, options);
75 resizeFile(entry, options);
81 private static File relativize(File root, File child) {
82 return root.toPath().relativize(child.toPath()).toFile();