java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java
changeset 6 b329573c76d7
child 7 8e9983260624
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java	Mon Nov 17 01:17:02 2014 +0100
     1.3 @@ -0,0 +1,94 @@
     1.4 +/**
     1.5 + * copy-image-resizer
     1.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, either version 3 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License
    1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package cz.frantovo.copyImageResizer;
    1.22 +
    1.23 +import java.awt.Graphics2D;
    1.24 +import java.awt.image.BufferedImage;
    1.25 +import java.io.IOException;
    1.26 +import java.io.InputStream;
    1.27 +import java.io.OutputStream;
    1.28 +import java.util.regex.Pattern;
    1.29 +import javax.imageio.ImageIO;
    1.30 +
    1.31 +/**
    1.32 + *
    1.33 + * @author Ing. František Kučera (frantovo.cz)
    1.34 + */
    1.35 +public class SingleImageResizer {
    1.36 +
    1.37 +	public void resize(InputStream input, OutputStream output, ImageFormat outputFormat) throws ResizeException {
    1.38 +		try {
    1.39 +			BufferedImage image = ImageIO.read(input);
    1.40 +			BufferedImage resized = resize(image, 64, 64, image.getType());
    1.41 +
    1.42 +			ImageIO.write(resized, outputFormat.getFormat(), output);
    1.43 +		} catch (IOException e) {
    1.44 +			throw new ResizeException("Error while resizing image", e);
    1.45 +		}
    1.46 +	}
    1.47 +
    1.48 +	private BufferedImage resize(BufferedImage original, int maxWidth, int maxHeight, int type) {
    1.49 +
    1.50 +		int width;
    1.51 +		int height;
    1.52 +		float aspectRatio = (float) original.getWidth() / (float) original.getHeight();
    1.53 +
    1.54 +		if (aspectRatio > 1) {
    1.55 +			width = maxWidth;
    1.56 +			height = Math.max(Math.round(maxWidth / aspectRatio), 1);
    1.57 +		} else {
    1.58 +			width = Math.max(Math.round(maxHeight * aspectRatio), 1);
    1.59 +			height = maxHeight;
    1.60 +		}
    1.61 +
    1.62 +		BufferedImage resized = new BufferedImage(width, height, type);
    1.63 +		Graphics2D g = resized.createGraphics();
    1.64 +		g.drawImage(original, 0, 0, width, height, null);
    1.65 +		g.dispose();
    1.66 +
    1.67 +		return resized;
    1.68 +	}
    1.69 +
    1.70 +	public static enum ImageFormat {
    1.71 +
    1.72 +		JPEG("jpg", Pattern.compile(".*\\.(jpg|jpeg)$", Pattern.CASE_INSENSITIVE)),
    1.73 +		PNG("png", Pattern.compile(".*\\.png$", Pattern.CASE_INSENSITIVE)),
    1.74 +		GIF("gif", Pattern.compile(".*\\.gif$", Pattern.CASE_INSENSITIVE));
    1.75 +
    1.76 +		private final String format;
    1.77 +		private final Pattern regex;
    1.78 +
    1.79 +		private ImageFormat(String format, Pattern regex) {
    1.80 +			this.format = format;
    1.81 +			this.regex = regex;
    1.82 +		}
    1.83 +
    1.84 +		public String getFormat() {
    1.85 +			return format;
    1.86 +		}
    1.87 +
    1.88 +		public static ImageFormat getMatching(String fileName) {
    1.89 +			for (ImageFormat type : values()) {
    1.90 +				if (type.regex.matcher(fileName).matches()) {
    1.91 +					return type;
    1.92 +				}
    1.93 +			}
    1.94 +			return null;
    1.95 +		}
    1.96 +	}
    1.97 +}