java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 17 Nov 2014 01:17:02 +0100
changeset 6 b329573c76d7
parent 5 5019e3e93a4e
child 7 8e9983260624
permissions -rw-r--r--
resize, fixed size, TODO: configured size + multiple sizes
     1 /**
     2  * copy-image-resizer
     3  * Copyright © 2014 František Kučera (frantovo.cz)
     4  *
     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.
     9  *
    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.
    14  *
    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/>.
    17  */
    18 package cz.frantovo.copyImageResizer;
    19 
    20 import cz.frantovo.copyImageResizer.SingleImageResizer.ImageFormat;
    21 import java.io.File;
    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;
    27 
    28 /**
    29  *
    30  * @author Ing. František Kučera (frantovo.cz)
    31  */
    32 public class RecursiveImageResizer {
    33 
    34 	private static final Logger log = Logger.getLogger(RecursiveImageResizer.class.getName());
    35 
    36 	private final SingleImageResizer resizer = new SingleImageResizer();
    37 
    38 	public void resize(RecursiveOptions options) throws RecursiveException, ResizeException {
    39 		resizeDirectory(options.getInput(), options);
    40 	}
    41 
    42 	private void resizeFile(File file, RecursiveOptions options) throws ResizeException {
    43 		log.log(Level.FINE, "Resizing file: {0}", relativize(options.getInput(), file));
    44 
    45 		ImageFormat format = ImageFormat.getMatching(file.getName());
    46 
    47 		if (format == null) {
    48 			log.log(Level.FINE, "Skipping file: {0} (no image format matched this extension)", relativize(options.getInput(), file));
    49 		} else {
    50 			try {
    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);
    58 			}
    59 		}
    60 
    61 	}
    62 
    63 	private void resizeDirectory(File directory, RecursiveOptions options) throws ResizeException {
    64 
    65 		log.log(Level.FINE, "Resizing directory: {0}", directory);
    66 
    67 		File relative = relativize(options.getInput(), directory);
    68 		File output = new File(options.getOutput(), relative.getPath());
    69 		output.mkdirs();
    70 
    71 		for (File entry : directory.listFiles()) {
    72 			if (entry.isDirectory()) {
    73 				resizeDirectory(entry, options);
    74 			} else {
    75 				resizeFile(entry, options);
    76 			}
    77 		}
    78 
    79 	}
    80 
    81 	private static File relativize(File root, File child) {
    82 		return root.toPath().relativize(child.toPath()).toFile();
    83 	}
    84 
    85 }