diff -r 4634176c6602 -r 505031965440 java/copy-image-resizer/src/cz/frantovo/copyImageResizer/JavaSingleImageResizer.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/JavaSingleImageResizer.java Mon Nov 17 21:59:20 2014 +0100 @@ -0,0 +1,127 @@ +/** + * copy-image-resizer + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.copyImageResizer; + +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.regex.Pattern; +import javax.imageio.ImageIO; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class JavaSingleImageResizer implements SingleImageResizer { + + private static final Logger log = Logger.getLogger(SingleImageResizer.class.getName()); + + @Override + public boolean resize(InputStream input, OutputStream output, SizeSpecification size, ImageFormat outputFormat) throws ResizeException { + try { + BufferedImage image = readImage(input); + if (shouldResize(image, size)) { + BufferedImage resized = resize(image, size.getWidth(), size.getHeight(), image.getType()); + ImageIO.write(resized, outputFormat.getFormat(), output); + return true; + } else { + return false; + } + } catch (IOException e) { + throw new ResizeException("Error while resizing image", e); + } + } + + private BufferedImage resize(BufferedImage original, int maxWidth, int maxHeight, int type) { + + int width; + int height; + float aspectRatio = (float) original.getWidth() / (float) original.getHeight(); + + if (aspectRatio > 1) { + width = maxWidth; + height = Math.max(Math.round(maxWidth / aspectRatio), 1); + } else { + width = Math.max(Math.round(maxHeight * aspectRatio), 1); + height = maxHeight; + } + + if (type == BufferedImage.TYPE_CUSTOM) { + log.log(Level.FINER, "Setting default image type: from TYPE_CUSTOM to TYPE_INT_ARGB"); + type = BufferedImage.TYPE_INT_ARGB; + } + + BufferedImage resized = new BufferedImage(width, height, type); + Graphics2D g = resized.createGraphics(); + g.drawImage(original, 0, 0, width, height, null); + g.dispose(); + + return resized; + } + + private static BufferedImage readImage(InputStream input) throws ResizeException { + try { + return ImageIO.read(input); + } catch (IOException e) { + throw new ResizeException("Unable to read image from stream", e); + } + } + + private static boolean shouldResize(BufferedImage input, SizeSpecification requested) { + if (requested.isResizeSmaller()) { + return input.getHeight() != requested.getHeight() || input.getWidth() != requested.getWidth(); + } else { + return input.getHeight() > requested.getHeight() || input.getWidth() > requested.getWidth(); + } + } + + public static enum ImageFormat { + + JPEG("jpg", Pattern.compile(".*\\.(jpg|jpeg)$", Pattern.CASE_INSENSITIVE)), + PNG("png", Pattern.compile(".*\\.png$", Pattern.CASE_INSENSITIVE)), + GIF("gif", Pattern.compile(".*\\.gif$", Pattern.CASE_INSENSITIVE)); + + private final String format; + private final Pattern regex; + + private ImageFormat(String format, Pattern regex) { + this.format = format; + this.regex = regex; + } + + /** + * @return format name for {@linkplain ImageIO#write(java.awt.image.RenderedImage, java.lang.String, java.io.File) + */ + public String getFormat() { + return format; + } + + public static ImageFormat getMatching(String fileName) { + for (ImageFormat type : values()) { + if (type.regex.matcher(fileName).matches()) { + return type; + } + } + return null; + } + } +}