java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 17 Nov 2014 18:13:59 +0100
changeset 10 a5a723467a39
parent 7 8e9983260624
child 11 f517bafcf812
permissions -rw-r--r--
logging, imports, constants
     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.awt.image.BufferedImage;
    22 import java.io.File;
    23 import java.io.FileInputStream;
    24 import java.io.FileNotFoundException;
    25 import java.io.FileOutputStream;
    26 import java.io.IOException;
    27 import java.io.InputStream;
    28 import java.nio.channels.FileChannel;
    29 import java.util.logging.Level;
    30 import java.util.logging.Logger;
    31 import javax.imageio.ImageIO;
    32 
    33 /**
    34  *
    35  * @author Ing. František Kučera (frantovo.cz)
    36  */
    37 public class RecursiveImageResizer {
    38 
    39 	private static final Logger log = Logger.getLogger(RecursiveImageResizer.class.getName());
    40 
    41 	private final SingleImageResizer resizer = new SingleImageResizer();
    42 
    43 	public void resize(RecursiveOptions options) throws RecursiveException, ResizeException {
    44 		resizeDirectory(options.getInput(), options);
    45 	}
    46 
    47 	private void resizeFile(File inputFile, RecursiveOptions options) throws ResizeException {
    48 		File inputFileRelative = relativize(options.getInput(), inputFile);
    49 		log.log(Level.FINER, "Resizing file: {0}", inputFileRelative);
    50 
    51 		ImageFormat format = ImageFormat.getMatching(inputFile.getName());
    52 
    53 		if (format == null) {
    54 			log.log(Level.FINER, "Skipping file: {0} (no image format matched this extension)", inputFileRelative);
    55 		} else {
    56 			try {
    57 				for (SizeSpecification size : options.getSizes()) {
    58 					File sizeRoot = new File(options.getOutput(), size.getDirectory());
    59 					File outputFile = new File(sizeRoot, inputFileRelative.getPath());
    60 					try (FileInputStream input = new FileInputStream(inputFile)) {
    61 						BufferedImage image = readImage(input);
    62 						if (shouldResize(image, size)) {
    63 							try (FileOutputStream output = new FileOutputStream(outputFile)) {
    64 								resizer.resize(image, output, size, format);
    65 							}
    66 						} else {
    67 							log.log(Level.FINER, "File: {0} has already required size → just copy", inputFileRelative);
    68 							justCopy(inputFile, outputFile);
    69 						}
    70 					}
    71 				}
    72 			} catch (FileNotFoundException e) {
    73 				throw new ResizeException("Error while opening stream", e);
    74 			} catch (IOException e) {
    75 				throw new ResizeException("Error while closing stream", e);
    76 			}
    77 		}
    78 	}
    79 
    80 	private static boolean shouldResize(BufferedImage input, SizeSpecification requested) {
    81 		if (requested.isResizeSmaller()) {
    82 			return input.getHeight() != requested.getHeight() || input.getWidth() != requested.getWidth();
    83 		} else {
    84 			return input.getHeight() > requested.getHeight() || input.getWidth() > requested.getWidth();
    85 		}
    86 	}
    87 
    88 	private static BufferedImage readImage(InputStream input) throws ResizeException {
    89 		try {
    90 			return ImageIO.read(input);
    91 		} catch (IOException e) {
    92 			throw new ResizeException("Unable to read image from stream", e);
    93 		}
    94 	}
    95 
    96 	private static void justCopy(File inputFile, File outputFile) throws ResizeException {
    97 		try {
    98 
    99 			if (!outputFile.exists()) {
   100 				outputFile.createNewFile();
   101 			}
   102 
   103 			try (FileChannel input = new FileInputStream(inputFile).getChannel()) {
   104 				try (FileChannel output = new FileOutputStream(outputFile).getChannel()) {
   105 					output.transferFrom(input, 0, input.size());
   106 				}
   107 			}
   108 
   109 		} catch (IOException e) {
   110 			throw new ResizeException("Unable copy stream/channel", e);
   111 		}
   112 
   113 	}
   114 
   115 	private void resizeDirectory(File directory, RecursiveOptions options) throws ResizeException {
   116 
   117 		log.log(Level.FINE, "Resizing directory: {0}", directory);
   118 
   119 		for (SizeSpecification size : options.getSizes()) {
   120 			File relative = relativize(options.getInput(), directory);
   121 			File sizeRoot = new File(options.getOutput(), size.getDirectory());
   122 			File dir = new File(sizeRoot, relative.getPath());
   123 			dir.mkdirs();
   124 		}
   125 
   126 		for (File entry : directory.listFiles()) {
   127 			if (entry.isDirectory()) {
   128 				resizeDirectory(entry, options);
   129 			} else {
   130 				resizeFile(entry, options);
   131 			}
   132 		}
   133 
   134 	}
   135 
   136 	private static File relativize(File root, File child) {
   137 		return root.toPath().relativize(child.toPath()).toFile();
   138 	}
   139 
   140 }