java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java
changeset 15 93fa6ce675e5
parent 10 a5a723467a39
child 17 505031965440
     1.1 --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java	Mon Nov 17 20:23:58 2014 +0100
     1.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java	Mon Nov 17 20:38:38 2014 +0100
     1.3 @@ -20,6 +20,7 @@
     1.4  import java.awt.Graphics2D;
     1.5  import java.awt.image.BufferedImage;
     1.6  import java.io.IOException;
     1.7 +import java.io.InputStream;
     1.8  import java.io.OutputStream;
     1.9  import java.util.logging.Level;
    1.10  import java.util.logging.Logger;
    1.11 @@ -34,11 +35,27 @@
    1.12  
    1.13  	private static final Logger log = Logger.getLogger(SingleImageResizer.class.getName());
    1.14  
    1.15 -	public void resize(BufferedImage input, OutputStream output, SizeSpecification size, ImageFormat outputFormat) throws ResizeException {
    1.16 +	/**
    1.17 +	 *
    1.18 +	 * @param input
    1.19 +	 * @param output
    1.20 +	 * @param size
    1.21 +	 * @param outputFormat
    1.22 +	 * @return whether image was resized → if false, you should just copy the image after calling
    1.23 +	 * this method. Image is resized if larger than requested size or if {@linkplain SizeSpecification#isResizeSmaller()
    1.24 +	 * }.
    1.25 +	 * @throws ResizeException
    1.26 +	 */
    1.27 +	public boolean resize(InputStream input, OutputStream output, SizeSpecification size, ImageFormat outputFormat) throws ResizeException {
    1.28  		try {
    1.29 -			BufferedImage resized = resize(input, size.getWidth(), size.getHeight(), input.getType());
    1.30 -
    1.31 -			ImageIO.write(resized, outputFormat.getFormat(), output);
    1.32 +			BufferedImage image = readImage(input);
    1.33 +			if (shouldResize(image, size)) {
    1.34 +				BufferedImage resized = resize(image, size.getWidth(), size.getHeight(), image.getType());
    1.35 +				ImageIO.write(resized, outputFormat.getFormat(), output);
    1.36 +				return true;
    1.37 +			} else {
    1.38 +				return false;
    1.39 +			}
    1.40  		} catch (IOException e) {
    1.41  			throw new ResizeException("Error while resizing image", e);
    1.42  		}
    1.43 @@ -71,6 +88,22 @@
    1.44  		return resized;
    1.45  	}
    1.46  
    1.47 +	private static BufferedImage readImage(InputStream input) throws ResizeException {
    1.48 +		try {
    1.49 +			return ImageIO.read(input);
    1.50 +		} catch (IOException e) {
    1.51 +			throw new ResizeException("Unable to read image from stream", e);
    1.52 +		}
    1.53 +	}
    1.54 +
    1.55 +	private static boolean shouldResize(BufferedImage input, SizeSpecification requested) {
    1.56 +		if (requested.isResizeSmaller()) {
    1.57 +			return input.getHeight() != requested.getHeight() || input.getWidth() != requested.getWidth();
    1.58 +		} else {
    1.59 +			return input.getHeight() > requested.getHeight() || input.getWidth() > requested.getWidth();
    1.60 +		}
    1.61 +	}
    1.62 +
    1.63  	public static enum ImageFormat {
    1.64  
    1.65  		JPEG("jpg", Pattern.compile(".*\\.(jpg|jpeg)$", Pattern.CASE_INSENSITIVE)),