wasResized is decided in SingleImageResizer
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 17 Nov 2014 20:38:38 +0100
changeset 1593fa6ce675e5
parent 14 dec0dd934a64
child 16 4634176c6602
wasResized is decided in SingleImageResizer
java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java
java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java
     1.1 --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java	Mon Nov 17 20:23:58 2014 +0100
     1.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java	Mon Nov 17 20:38:38 2014 +0100
     1.3 @@ -18,18 +18,15 @@
     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.FileChannel;
    1.15  import java.util.logging.Level;
    1.16  import java.util.logging.LogRecord;
    1.17  import java.util.logging.Logger;
    1.18 -import javax.imageio.ImageIO;
    1.19  
    1.20  /**
    1.21   *
    1.22 @@ -70,16 +67,16 @@
    1.23  					File sizeRoot = new File(options.getOutput(), size.getDirectory());
    1.24  					File outputFile = new File(sizeRoot, inputFileRelative.getPath());
    1.25  					try (FileInputStream input = new FileInputStream(inputFile)) {
    1.26 -						BufferedImage image = readImage(input);
    1.27 -						if (shouldResize(image, size)) {
    1.28 -							try (FileOutputStream output = new FileOutputStream(outputFile)) {
    1.29 -								resizer.resize(image, output, size, format);
    1.30 +
    1.31 +						try (FileOutputStream output = new FileOutputStream(outputFile)) {
    1.32 +							boolean wasResized = resizer.resize(input, output, size, format);
    1.33 +							if (wasResized) {
    1.34  								counters.increment(Counters.COUNTER_TYPE.RESIZED);
    1.35 +							} else {
    1.36 +								log.log(Level.FINER, "File: {0} has already required (or smaller) size → just copy", inputFileRelative);
    1.37 +								justCopy(inputFile, outputFile);
    1.38 +								counters.increment(Counters.COUNTER_TYPE.JUST_COPIED);
    1.39  							}
    1.40 -						} else {
    1.41 -							log.log(Level.FINER, "File: {0} has already required (or smaller) size → just copy", inputFileRelative);
    1.42 -							justCopy(inputFile, outputFile);
    1.43 -							counters.increment(Counters.COUNTER_TYPE.JUST_COPIED);
    1.44  						}
    1.45  					}
    1.46  				}
    1.47 @@ -91,22 +88,6 @@
    1.48  		}
    1.49  	}
    1.50  
    1.51 -	private static boolean shouldResize(BufferedImage input, SizeSpecification requested) {
    1.52 -		if (requested.isResizeSmaller()) {
    1.53 -			return input.getHeight() != requested.getHeight() || input.getWidth() != requested.getWidth();
    1.54 -		} else {
    1.55 -			return input.getHeight() > requested.getHeight() || input.getWidth() > requested.getWidth();
    1.56 -		}
    1.57 -	}
    1.58 -
    1.59 -	private static BufferedImage readImage(InputStream input) throws ResizeException {
    1.60 -		try {
    1.61 -			return ImageIO.read(input);
    1.62 -		} catch (IOException e) {
    1.63 -			throw new ResizeException("Unable to read image from stream", e);
    1.64 -		}
    1.65 -	}
    1.66 -
    1.67  	private static void justCopy(File inputFile, File outputFile) throws ResizeException {
    1.68  		try {
    1.69  
     2.1 --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java	Mon Nov 17 20:23:58 2014 +0100
     2.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java	Mon Nov 17 20:38:38 2014 +0100
     2.3 @@ -20,6 +20,7 @@
     2.4  import java.awt.Graphics2D;
     2.5  import java.awt.image.BufferedImage;
     2.6  import java.io.IOException;
     2.7 +import java.io.InputStream;
     2.8  import java.io.OutputStream;
     2.9  import java.util.logging.Level;
    2.10  import java.util.logging.Logger;
    2.11 @@ -34,11 +35,27 @@
    2.12  
    2.13  	private static final Logger log = Logger.getLogger(SingleImageResizer.class.getName());
    2.14  
    2.15 -	public void resize(BufferedImage input, OutputStream output, SizeSpecification size, ImageFormat outputFormat) throws ResizeException {
    2.16 +	/**
    2.17 +	 *
    2.18 +	 * @param input
    2.19 +	 * @param output
    2.20 +	 * @param size
    2.21 +	 * @param outputFormat
    2.22 +	 * @return whether image was resized → if false, you should just copy the image after calling
    2.23 +	 * this method. Image is resized if larger than requested size or if {@linkplain SizeSpecification#isResizeSmaller()
    2.24 +	 * }.
    2.25 +	 * @throws ResizeException
    2.26 +	 */
    2.27 +	public boolean resize(InputStream input, OutputStream output, SizeSpecification size, ImageFormat outputFormat) throws ResizeException {
    2.28  		try {
    2.29 -			BufferedImage resized = resize(input, size.getWidth(), size.getHeight(), input.getType());
    2.30 -
    2.31 -			ImageIO.write(resized, outputFormat.getFormat(), output);
    2.32 +			BufferedImage image = readImage(input);
    2.33 +			if (shouldResize(image, size)) {
    2.34 +				BufferedImage resized = resize(image, size.getWidth(), size.getHeight(), image.getType());
    2.35 +				ImageIO.write(resized, outputFormat.getFormat(), output);
    2.36 +				return true;
    2.37 +			} else {
    2.38 +				return false;
    2.39 +			}
    2.40  		} catch (IOException e) {
    2.41  			throw new ResizeException("Error while resizing image", e);
    2.42  		}
    2.43 @@ -71,6 +88,22 @@
    2.44  		return resized;
    2.45  	}
    2.46  
    2.47 +	private static BufferedImage readImage(InputStream input) throws ResizeException {
    2.48 +		try {
    2.49 +			return ImageIO.read(input);
    2.50 +		} catch (IOException e) {
    2.51 +			throw new ResizeException("Unable to read image from stream", e);
    2.52 +		}
    2.53 +	}
    2.54 +
    2.55 +	private static boolean shouldResize(BufferedImage input, SizeSpecification requested) {
    2.56 +		if (requested.isResizeSmaller()) {
    2.57 +			return input.getHeight() != requested.getHeight() || input.getWidth() != requested.getWidth();
    2.58 +		} else {
    2.59 +			return input.getHeight() > requested.getHeight() || input.getWidth() > requested.getWidth();
    2.60 +		}
    2.61 +	}
    2.62 +
    2.63  	public static enum ImageFormat {
    2.64  
    2.65  		JPEG("jpg", Pattern.compile(".*\\.(jpg|jpeg)$", Pattern.CASE_INSENSITIVE)),