java/copy-image-resizer/src/cz/frantovo/copyImageResizer/JavaSingleImageResizer.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/JavaSingleImageResizer.java Mon Nov 17 21:59:20 2014 +0100
1.3 @@ -0,0 +1,127 @@
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.logging.Level;
1.29 +import java.util.logging.Logger;
1.30 +import java.util.regex.Pattern;
1.31 +import javax.imageio.ImageIO;
1.32 +
1.33 +/**
1.34 + *
1.35 + * @author Ing. František Kučera (frantovo.cz)
1.36 + */
1.37 +public class JavaSingleImageResizer implements SingleImageResizer {
1.38 +
1.39 + private static final Logger log = Logger.getLogger(SingleImageResizer.class.getName());
1.40 +
1.41 + @Override
1.42 + public boolean resize(InputStream input, OutputStream output, SizeSpecification size, ImageFormat outputFormat) throws ResizeException {
1.43 + try {
1.44 + BufferedImage image = readImage(input);
1.45 + if (shouldResize(image, size)) {
1.46 + BufferedImage resized = resize(image, size.getWidth(), size.getHeight(), image.getType());
1.47 + ImageIO.write(resized, outputFormat.getFormat(), output);
1.48 + return true;
1.49 + } else {
1.50 + return false;
1.51 + }
1.52 + } catch (IOException e) {
1.53 + throw new ResizeException("Error while resizing image", e);
1.54 + }
1.55 + }
1.56 +
1.57 + private BufferedImage resize(BufferedImage original, int maxWidth, int maxHeight, int type) {
1.58 +
1.59 + int width;
1.60 + int height;
1.61 + float aspectRatio = (float) original.getWidth() / (float) original.getHeight();
1.62 +
1.63 + if (aspectRatio > 1) {
1.64 + width = maxWidth;
1.65 + height = Math.max(Math.round(maxWidth / aspectRatio), 1);
1.66 + } else {
1.67 + width = Math.max(Math.round(maxHeight * aspectRatio), 1);
1.68 + height = maxHeight;
1.69 + }
1.70 +
1.71 + if (type == BufferedImage.TYPE_CUSTOM) {
1.72 + log.log(Level.FINER, "Setting default image type: from TYPE_CUSTOM to TYPE_INT_ARGB");
1.73 + type = BufferedImage.TYPE_INT_ARGB;
1.74 + }
1.75 +
1.76 + BufferedImage resized = new BufferedImage(width, height, type);
1.77 + Graphics2D g = resized.createGraphics();
1.78 + g.drawImage(original, 0, 0, width, height, null);
1.79 + g.dispose();
1.80 +
1.81 + return resized;
1.82 + }
1.83 +
1.84 + private static BufferedImage readImage(InputStream input) throws ResizeException {
1.85 + try {
1.86 + return ImageIO.read(input);
1.87 + } catch (IOException e) {
1.88 + throw new ResizeException("Unable to read image from stream", e);
1.89 + }
1.90 + }
1.91 +
1.92 + private static boolean shouldResize(BufferedImage input, SizeSpecification requested) {
1.93 + if (requested.isResizeSmaller()) {
1.94 + return input.getHeight() != requested.getHeight() || input.getWidth() != requested.getWidth();
1.95 + } else {
1.96 + return input.getHeight() > requested.getHeight() || input.getWidth() > requested.getWidth();
1.97 + }
1.98 + }
1.99 +
1.100 + public static enum ImageFormat {
1.101 +
1.102 + JPEG("jpg", Pattern.compile(".*\\.(jpg|jpeg)$", Pattern.CASE_INSENSITIVE)),
1.103 + PNG("png", Pattern.compile(".*\\.png$", Pattern.CASE_INSENSITIVE)),
1.104 + GIF("gif", Pattern.compile(".*\\.gif$", Pattern.CASE_INSENSITIVE));
1.105 +
1.106 + private final String format;
1.107 + private final Pattern regex;
1.108 +
1.109 + private ImageFormat(String format, Pattern regex) {
1.110 + this.format = format;
1.111 + this.regex = regex;
1.112 + }
1.113 +
1.114 + /**
1.115 + * @return format name for {@linkplain ImageIO#write(java.awt.image.RenderedImage, java.lang.String, java.io.File)
1.116 + */
1.117 + public String getFormat() {
1.118 + return format;
1.119 + }
1.120 +
1.121 + public static ImageFormat getMatching(String fileName) {
1.122 + for (ImageFormat type : values()) {
1.123 + if (type.regex.matcher(fileName).matches()) {
1.124 + return type;
1.125 + }
1.126 + }
1.127 + return null;
1.128 + }
1.129 + }
1.130 +}