franta-hg@6: /** franta-hg@6: * copy-image-resizer franta-hg@6: * Copyright © 2014 František Kučera (frantovo.cz) franta-hg@6: * franta-hg@6: * This program is free software: you can redistribute it and/or modify franta-hg@6: * it under the terms of the GNU General Public License as published by franta-hg@6: * the Free Software Foundation, either version 3 of the License, or franta-hg@6: * (at your option) any later version. franta-hg@6: * franta-hg@6: * This program is distributed in the hope that it will be useful, franta-hg@6: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@6: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@6: * GNU General Public License for more details. franta-hg@6: * franta-hg@6: * You should have received a copy of the GNU General Public License franta-hg@6: * along with this program. If not, see . franta-hg@6: */ franta-hg@6: package cz.frantovo.copyImageResizer; franta-hg@6: franta-hg@6: import java.awt.Graphics2D; franta-hg@6: import java.awt.image.BufferedImage; franta-hg@6: import java.io.IOException; franta-hg@6: import java.io.InputStream; franta-hg@6: import java.io.OutputStream; franta-hg@6: import java.util.regex.Pattern; franta-hg@6: import javax.imageio.ImageIO; franta-hg@6: franta-hg@6: /** franta-hg@6: * franta-hg@6: * @author Ing. František Kučera (frantovo.cz) franta-hg@6: */ franta-hg@6: public class SingleImageResizer { franta-hg@6: franta-hg@6: public void resize(InputStream input, OutputStream output, ImageFormat outputFormat) throws ResizeException { franta-hg@6: try { franta-hg@6: BufferedImage image = ImageIO.read(input); franta-hg@6: BufferedImage resized = resize(image, 64, 64, image.getType()); franta-hg@6: franta-hg@6: ImageIO.write(resized, outputFormat.getFormat(), output); franta-hg@6: } catch (IOException e) { franta-hg@6: throw new ResizeException("Error while resizing image", e); franta-hg@6: } franta-hg@6: } franta-hg@6: franta-hg@6: private BufferedImage resize(BufferedImage original, int maxWidth, int maxHeight, int type) { franta-hg@6: franta-hg@6: int width; franta-hg@6: int height; franta-hg@6: float aspectRatio = (float) original.getWidth() / (float) original.getHeight(); franta-hg@6: franta-hg@6: if (aspectRatio > 1) { franta-hg@6: width = maxWidth; franta-hg@6: height = Math.max(Math.round(maxWidth / aspectRatio), 1); franta-hg@6: } else { franta-hg@6: width = Math.max(Math.round(maxHeight * aspectRatio), 1); franta-hg@6: height = maxHeight; franta-hg@6: } franta-hg@6: franta-hg@6: BufferedImage resized = new BufferedImage(width, height, type); franta-hg@6: Graphics2D g = resized.createGraphics(); franta-hg@6: g.drawImage(original, 0, 0, width, height, null); franta-hg@6: g.dispose(); franta-hg@6: franta-hg@6: return resized; franta-hg@6: } franta-hg@6: franta-hg@6: public static enum ImageFormat { franta-hg@6: franta-hg@6: JPEG("jpg", Pattern.compile(".*\\.(jpg|jpeg)$", Pattern.CASE_INSENSITIVE)), franta-hg@6: PNG("png", Pattern.compile(".*\\.png$", Pattern.CASE_INSENSITIVE)), franta-hg@6: GIF("gif", Pattern.compile(".*\\.gif$", Pattern.CASE_INSENSITIVE)); franta-hg@6: franta-hg@6: private final String format; franta-hg@6: private final Pattern regex; franta-hg@6: franta-hg@6: private ImageFormat(String format, Pattern regex) { franta-hg@6: this.format = format; franta-hg@6: this.regex = regex; franta-hg@6: } franta-hg@6: franta-hg@6: public String getFormat() { franta-hg@6: return format; franta-hg@6: } franta-hg@6: franta-hg@6: public static ImageFormat getMatching(String fileName) { franta-hg@6: for (ImageFormat type : values()) { franta-hg@6: if (type.regex.matcher(fileName).matches()) { franta-hg@6: return type; franta-hg@6: } franta-hg@6: } franta-hg@6: return null; franta-hg@6: } franta-hg@6: } franta-hg@6: }