java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java
1.1 --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java Sun Nov 16 23:47:28 2014 +0100
1.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java Mon Nov 17 01:17:02 2014 +0100
1.3 @@ -17,13 +17,69 @@
1.4 */
1.5 package cz.frantovo.copyImageResizer;
1.6
1.7 +import cz.frantovo.copyImageResizer.SingleImageResizer.ImageFormat;
1.8 +import java.io.File;
1.9 +import java.io.FileInputStream;
1.10 +import java.io.FileNotFoundException;
1.11 +import java.io.FileOutputStream;
1.12 +import java.util.logging.Level;
1.13 +import java.util.logging.Logger;
1.14 +
1.15 /**
1.16 *
1.17 * @author Ing. František Kučera (frantovo.cz)
1.18 */
1.19 public class RecursiveImageResizer {
1.20
1.21 + private static final Logger log = Logger.getLogger(RecursiveImageResizer.class.getName());
1.22 +
1.23 + private final SingleImageResizer resizer = new SingleImageResizer();
1.24 +
1.25 public void resize(RecursiveOptions options) throws RecursiveException, ResizeException {
1.26 + resizeDirectory(options.getInput(), options);
1.27 + }
1.28 +
1.29 + private void resizeFile(File file, RecursiveOptions options) throws ResizeException {
1.30 + log.log(Level.FINE, "Resizing file: {0}", relativize(options.getInput(), file));
1.31 +
1.32 + ImageFormat format = ImageFormat.getMatching(file.getName());
1.33 +
1.34 + if (format == null) {
1.35 + log.log(Level.FINE, "Skipping file: {0} (no image format matched this extension)", relativize(options.getInput(), file));
1.36 + } else {
1.37 + try {
1.38 + FileInputStream input = new FileInputStream(file);
1.39 + File relative = relativize(options.getInput(), file);
1.40 + File outputFile = new File(options.getOutput(), relative.getPath());
1.41 + FileOutputStream output = new FileOutputStream(outputFile);
1.42 + resizer.resize(input, output, format);
1.43 + } catch (FileNotFoundException e) {
1.44 + throw new ResizeException("Error while opening stream", e);
1.45 + }
1.46 + }
1.47 +
1.48 + }
1.49 +
1.50 + private void resizeDirectory(File directory, RecursiveOptions options) throws ResizeException {
1.51 +
1.52 + log.log(Level.FINE, "Resizing directory: {0}", directory);
1.53 +
1.54 + File relative = relativize(options.getInput(), directory);
1.55 + File output = new File(options.getOutput(), relative.getPath());
1.56 + output.mkdirs();
1.57 +
1.58 + for (File entry : directory.listFiles()) {
1.59 + if (entry.isDirectory()) {
1.60 + resizeDirectory(entry, options);
1.61 + } else {
1.62 + resizeFile(entry, options);
1.63 + }
1.64 + }
1.65 +
1.66 + }
1.67 +
1.68 + private static File relativize(File root, File child) {
1.69 + return root.toPath().relativize(child.toPath()).toFile();
1.70 }
1.71
1.72 }