java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java
changeset 7 8e9983260624
parent 6 b329573c76d7
child 10 a5a723467a39
     1.1 --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java	Mon Nov 17 01:17:02 2014 +0100
     1.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java	Mon Nov 17 17:02:48 2014 +0100
     1.3 @@ -18,12 +18,20 @@
     1.4  package cz.frantovo.copyImageResizer;
     1.5  
     1.6  import cz.frantovo.copyImageResizer.SingleImageResizer.ImageFormat;
     1.7 +import java.awt.image.BufferedImage;
     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.io.IOException;
    1.13 +import java.io.InputStream;
    1.14 +import java.nio.channels.Channels;
    1.15 +import java.nio.channels.FileChannel;
    1.16 +import java.nio.channels.ReadableByteChannel;
    1.17 +import java.nio.channels.WritableByteChannel;
    1.18  import java.util.logging.Level;
    1.19  import java.util.logging.Logger;
    1.20 +import javax.imageio.ImageIO;
    1.21  
    1.22  /**
    1.23   *
    1.24 @@ -39,24 +47,71 @@
    1.25  		resizeDirectory(options.getInput(), options);
    1.26  	}
    1.27  
    1.28 -	private void resizeFile(File file, RecursiveOptions options) throws ResizeException {
    1.29 -		log.log(Level.FINE, "Resizing file: {0}", relativize(options.getInput(), file));
    1.30 +	private void resizeFile(File inputFile, RecursiveOptions options) throws ResizeException {
    1.31 +		File inputFileRelative = relativize(options.getInput(), inputFile);
    1.32 +		log.log(Level.FINE, "Resizing file: {0}", inputFileRelative);
    1.33  
    1.34 -		ImageFormat format = ImageFormat.getMatching(file.getName());
    1.35 +		ImageFormat format = ImageFormat.getMatching(inputFile.getName());
    1.36  
    1.37  		if (format == null) {
    1.38 -			log.log(Level.FINE, "Skipping file: {0} (no image format matched this extension)", relativize(options.getInput(), file));
    1.39 +			log.log(Level.FINE, "Skipping file: {0} (no image format matched this extension)", inputFileRelative);
    1.40  		} else {
    1.41  			try {
    1.42 -				FileInputStream input = new FileInputStream(file);
    1.43 -				File relative = relativize(options.getInput(), file);
    1.44 -				File outputFile = new File(options.getOutput(), relative.getPath());
    1.45 -				FileOutputStream output = new FileOutputStream(outputFile);
    1.46 -				resizer.resize(input, output, format);
    1.47 +				for (SizeSpecification size : options.getSizes()) {
    1.48 +					File sizeRoot = new File(options.getOutput(), size.getDirectory());
    1.49 +					File outputFile = new File(sizeRoot, inputFileRelative.getPath());
    1.50 +					try (FileInputStream input = new FileInputStream(inputFile)) {
    1.51 +						BufferedImage image = readImage(input);
    1.52 +						if (shouldResize(image, size)) {
    1.53 +							try (FileOutputStream output = new FileOutputStream(outputFile)) {
    1.54 +								resizer.resize(image, output, size, format);
    1.55 +							}
    1.56 +						} else {
    1.57 +							log.log(Level.INFO, "File: {0} has already required size → just copy", inputFileRelative);
    1.58 +							justCopy(inputFile, outputFile);
    1.59 +						}
    1.60 +					}
    1.61 +				}
    1.62  			} catch (FileNotFoundException e) {
    1.63  				throw new ResizeException("Error while opening stream", e);
    1.64 +			} catch (IOException e) {
    1.65 +				throw new ResizeException("Error while closing stream", e);
    1.66  			}
    1.67  		}
    1.68 +	}
    1.69 +
    1.70 +	private static boolean shouldResize(BufferedImage input, SizeSpecification requested) {
    1.71 +		if (requested.isResizeSmaller()) {
    1.72 +			return input.getHeight() != requested.getHeight() || input.getWidth() != requested.getWidth();
    1.73 +		} else {
    1.74 +			return input.getHeight() > requested.getHeight() || input.getWidth() > requested.getWidth();
    1.75 +		}
    1.76 +	}
    1.77 +
    1.78 +	private static BufferedImage readImage(InputStream input) throws ResizeException {
    1.79 +		try {
    1.80 +			return ImageIO.read(input);
    1.81 +		} catch (IOException e) {
    1.82 +			throw new ResizeException("Unable to read image from stream", e);
    1.83 +		}
    1.84 +	}
    1.85 +
    1.86 +	private static void justCopy(File inputFile, File outputFile) throws ResizeException {
    1.87 +		try {
    1.88 +
    1.89 +			if (!outputFile.exists()) {
    1.90 +				outputFile.createNewFile();
    1.91 +			}
    1.92 +
    1.93 +			try (FileChannel input = new FileInputStream(inputFile).getChannel()) {
    1.94 +				try (FileChannel output = new FileOutputStream(outputFile).getChannel()) {
    1.95 +					output.transferFrom(input, 0, input.size());
    1.96 +				}
    1.97 +			}
    1.98 +
    1.99 +		} catch (IOException e) {
   1.100 +			throw new ResizeException("Unable copy stream/channel", e);
   1.101 +		}
   1.102  
   1.103  	}
   1.104  
   1.105 @@ -64,9 +119,12 @@
   1.106  
   1.107  		log.log(Level.FINE, "Resizing directory: {0}", directory);
   1.108  
   1.109 -		File relative = relativize(options.getInput(), directory);
   1.110 -		File output = new File(options.getOutput(), relative.getPath());
   1.111 -		output.mkdirs();
   1.112 +		for (SizeSpecification size : options.getSizes()) {
   1.113 +			File relative = relativize(options.getInput(), directory);
   1.114 +			File sizeRoot = new File(options.getOutput(), size.getDirectory());
   1.115 +			File dir = new File(sizeRoot, relative.getPath());
   1.116 +			dir.mkdirs();
   1.117 +		}
   1.118  
   1.119  		for (File entry : directory.listFiles()) {
   1.120  			if (entry.isDirectory()) {