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@15: import java.io.InputStream; franta-hg@6: import java.io.OutputStream; franta-hg@7: import java.util.logging.Level; franta-hg@7: import java.util.logging.Logger; 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@17: public class JavaSingleImageResizer implements SingleImageResizer { franta-hg@6: franta-hg@7: private static final Logger log = Logger.getLogger(SingleImageResizer.class.getName()); franta-hg@7: franta-hg@17: @Override franta-hg@15: public boolean resize(InputStream input, OutputStream output, SizeSpecification size, ImageFormat outputFormat) throws ResizeException { franta-hg@6: try { franta-hg@15: BufferedImage image = readImage(input); franta-hg@15: if (shouldResize(image, size)) { franta-hg@15: BufferedImage resized = resize(image, size.getWidth(), size.getHeight(), image.getType()); franta-hg@15: ImageIO.write(resized, outputFormat.getFormat(), output); franta-hg@15: return true; franta-hg@15: } else { franta-hg@15: return false; franta-hg@15: } 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@7: if (type == BufferedImage.TYPE_CUSTOM) { franta-hg@10: log.log(Level.FINER, "Setting default image type: from TYPE_CUSTOM to TYPE_INT_ARGB"); franta-hg@7: type = BufferedImage.TYPE_INT_ARGB; franta-hg@7: } franta-hg@7: 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@15: private static BufferedImage readImage(InputStream input) throws ResizeException { franta-hg@15: try { franta-hg@15: return ImageIO.read(input); franta-hg@15: } catch (IOException e) { franta-hg@15: throw new ResizeException("Unable to read image from stream", e); franta-hg@15: } franta-hg@15: } franta-hg@15: franta-hg@15: private static boolean shouldResize(BufferedImage input, SizeSpecification requested) { franta-hg@15: if (requested.isResizeSmaller()) { franta-hg@15: return input.getHeight() != requested.getHeight() || input.getWidth() != requested.getWidth(); franta-hg@15: } else { franta-hg@15: return input.getHeight() > requested.getHeight() || input.getWidth() > requested.getWidth(); franta-hg@15: } franta-hg@15: } franta-hg@15: 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@7: /** franta-hg@7: * @return format name for {@linkplain ImageIO#write(java.awt.image.RenderedImage, java.lang.String, java.io.File) franta-hg@7: */ 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: }