java/copy-image-resizer/src/cz/frantovo/copyImageResizer/JavaSingleImageResizer.java
3 * Copyright © 2014 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package cz.frantovo.copyImageResizer;
20 import java.awt.Graphics2D;
21 import java.awt.image.BufferedImage;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import java.util.regex.Pattern;
28 import javax.imageio.ImageIO;
32 * @author Ing. František Kučera (frantovo.cz)
34 public class JavaSingleImageResizer implements SingleImageResizer {
36 private static final Logger log = Logger.getLogger(SingleImageResizer.class.getName());
39 public boolean resize(InputStream input, OutputStream output, SizeSpecification size, ImageFormat outputFormat) throws ResizeException {
41 BufferedImage image = readImage(input);
42 if (shouldResize(image, size)) {
43 BufferedImage resized = resize(image, size.getWidth(), size.getHeight(), image.getType());
44 ImageIO.write(resized, outputFormat.getFormat(), output);
49 } catch (IOException e) {
50 throw new ResizeException("Error while resizing image", e);
54 private BufferedImage resize(BufferedImage original, int maxWidth, int maxHeight, int type) {
58 float originalAspectRatio = (float) original.getWidth() / (float) original.getHeight();
59 float requestedAspectRatio = (float) maxWidth / (float) maxHeight;
61 if (originalAspectRatio > requestedAspectRatio) {
63 height = Math.max(Math.round(maxWidth / originalAspectRatio), 1);
65 width = Math.max(Math.round(maxHeight * originalAspectRatio), 1);
69 if (type == BufferedImage.TYPE_CUSTOM) {
70 log.log(Level.FINER, "Setting default image type: from TYPE_CUSTOM to TYPE_INT_ARGB");
71 type = BufferedImage.TYPE_INT_ARGB;
74 BufferedImage resized = new BufferedImage(width, height, type);
75 Graphics2D g = resized.createGraphics();
76 g.drawImage(original, 0, 0, width, height, null);
82 private static BufferedImage readImage(InputStream input) throws ResizeException {
84 return ImageIO.read(input);
85 } catch (IOException e) {
86 throw new ResizeException("Unable to read image from stream", e);
90 private static boolean shouldResize(BufferedImage input, SizeSpecification requested) {
91 if (requested.isResizeSmaller()) {
92 return input.getHeight() != requested.getHeight() || input.getWidth() != requested.getWidth();
94 return input.getHeight() > requested.getHeight() || input.getWidth() > requested.getWidth();
98 public static enum ImageFormat {
100 JPEG("jpg", Pattern.compile(".*\\.(jpg|jpeg)$", Pattern.CASE_INSENSITIVE)),
101 PNG("png", Pattern.compile(".*\\.png$", Pattern.CASE_INSENSITIVE)),
102 GIF("gif", Pattern.compile(".*\\.gif$", Pattern.CASE_INSENSITIVE));
104 private final String format;
105 private final Pattern regex;
107 private ImageFormat(String format, Pattern regex) {
108 this.format = format;
113 * @return format name for {@linkplain ImageIO#write(java.awt.image.RenderedImage, java.lang.String, java.io.File)
115 public String getFormat() {
119 public static ImageFormat getMatching(String fileName) {
120 for (ImageFormat type : values()) {
121 if (type.regex.matcher(fileName).matches()) {