java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 17 Nov 2014 20:38:38 +0100
changeset 15 93fa6ce675e5
parent 10 a5a723467a39
child 17 505031965440
permissions -rw-r--r--
wasResized is decided in SingleImageResizer
franta-hg@6
     1
/**
franta-hg@6
     2
 * copy-image-resizer
franta-hg@6
     3
 * Copyright © 2014 František Kučera (frantovo.cz)
franta-hg@6
     4
 *
franta-hg@6
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@6
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@6
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@6
     8
 * (at your option) any later version.
franta-hg@6
     9
 *
franta-hg@6
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@6
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@6
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@6
    13
 * GNU General Public License for more details.
franta-hg@6
    14
 *
franta-hg@6
    15
 * You should have received a copy of the GNU General Public License
franta-hg@6
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@6
    17
 */
franta-hg@6
    18
package cz.frantovo.copyImageResizer;
franta-hg@6
    19
franta-hg@6
    20
import java.awt.Graphics2D;
franta-hg@6
    21
import java.awt.image.BufferedImage;
franta-hg@6
    22
import java.io.IOException;
franta-hg@15
    23
import java.io.InputStream;
franta-hg@6
    24
import java.io.OutputStream;
franta-hg@7
    25
import java.util.logging.Level;
franta-hg@7
    26
import java.util.logging.Logger;
franta-hg@6
    27
import java.util.regex.Pattern;
franta-hg@6
    28
import javax.imageio.ImageIO;
franta-hg@6
    29
franta-hg@6
    30
/**
franta-hg@6
    31
 *
franta-hg@6
    32
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@6
    33
 */
franta-hg@6
    34
public class SingleImageResizer {
franta-hg@6
    35
franta-hg@7
    36
	private static final Logger log = Logger.getLogger(SingleImageResizer.class.getName());
franta-hg@7
    37
franta-hg@15
    38
	/**
franta-hg@15
    39
	 *
franta-hg@15
    40
	 * @param input
franta-hg@15
    41
	 * @param output
franta-hg@15
    42
	 * @param size
franta-hg@15
    43
	 * @param outputFormat
franta-hg@15
    44
	 * @return whether image was resized → if false, you should just copy the image after calling
franta-hg@15
    45
	 * this method. Image is resized if larger than requested size or if {@linkplain SizeSpecification#isResizeSmaller()
franta-hg@15
    46
	 * }.
franta-hg@15
    47
	 * @throws ResizeException
franta-hg@15
    48
	 */
franta-hg@15
    49
	public boolean resize(InputStream input, OutputStream output, SizeSpecification size, ImageFormat outputFormat) throws ResizeException {
franta-hg@6
    50
		try {
franta-hg@15
    51
			BufferedImage image = readImage(input);
franta-hg@15
    52
			if (shouldResize(image, size)) {
franta-hg@15
    53
				BufferedImage resized = resize(image, size.getWidth(), size.getHeight(), image.getType());
franta-hg@15
    54
				ImageIO.write(resized, outputFormat.getFormat(), output);
franta-hg@15
    55
				return true;
franta-hg@15
    56
			} else {
franta-hg@15
    57
				return false;
franta-hg@15
    58
			}
franta-hg@6
    59
		} catch (IOException e) {
franta-hg@6
    60
			throw new ResizeException("Error while resizing image", e);
franta-hg@6
    61
		}
franta-hg@6
    62
	}
franta-hg@6
    63
franta-hg@6
    64
	private BufferedImage resize(BufferedImage original, int maxWidth, int maxHeight, int type) {
franta-hg@6
    65
franta-hg@6
    66
		int width;
franta-hg@6
    67
		int height;
franta-hg@6
    68
		float aspectRatio = (float) original.getWidth() / (float) original.getHeight();
franta-hg@6
    69
franta-hg@6
    70
		if (aspectRatio > 1) {
franta-hg@6
    71
			width = maxWidth;
franta-hg@6
    72
			height = Math.max(Math.round(maxWidth / aspectRatio), 1);
franta-hg@6
    73
		} else {
franta-hg@6
    74
			width = Math.max(Math.round(maxHeight * aspectRatio), 1);
franta-hg@6
    75
			height = maxHeight;
franta-hg@6
    76
		}
franta-hg@6
    77
franta-hg@7
    78
		if (type == BufferedImage.TYPE_CUSTOM) {
franta-hg@10
    79
			log.log(Level.FINER, "Setting default image type: from TYPE_CUSTOM to TYPE_INT_ARGB");
franta-hg@7
    80
			type = BufferedImage.TYPE_INT_ARGB;
franta-hg@7
    81
		}
franta-hg@7
    82
franta-hg@6
    83
		BufferedImage resized = new BufferedImage(width, height, type);
franta-hg@6
    84
		Graphics2D g = resized.createGraphics();
franta-hg@6
    85
		g.drawImage(original, 0, 0, width, height, null);
franta-hg@6
    86
		g.dispose();
franta-hg@6
    87
franta-hg@6
    88
		return resized;
franta-hg@6
    89
	}
franta-hg@6
    90
franta-hg@15
    91
	private static BufferedImage readImage(InputStream input) throws ResizeException {
franta-hg@15
    92
		try {
franta-hg@15
    93
			return ImageIO.read(input);
franta-hg@15
    94
		} catch (IOException e) {
franta-hg@15
    95
			throw new ResizeException("Unable to read image from stream", e);
franta-hg@15
    96
		}
franta-hg@15
    97
	}
franta-hg@15
    98
franta-hg@15
    99
	private static boolean shouldResize(BufferedImage input, SizeSpecification requested) {
franta-hg@15
   100
		if (requested.isResizeSmaller()) {
franta-hg@15
   101
			return input.getHeight() != requested.getHeight() || input.getWidth() != requested.getWidth();
franta-hg@15
   102
		} else {
franta-hg@15
   103
			return input.getHeight() > requested.getHeight() || input.getWidth() > requested.getWidth();
franta-hg@15
   104
		}
franta-hg@15
   105
	}
franta-hg@15
   106
franta-hg@6
   107
	public static enum ImageFormat {
franta-hg@6
   108
franta-hg@6
   109
		JPEG("jpg", Pattern.compile(".*\\.(jpg|jpeg)$", Pattern.CASE_INSENSITIVE)),
franta-hg@6
   110
		PNG("png", Pattern.compile(".*\\.png$", Pattern.CASE_INSENSITIVE)),
franta-hg@6
   111
		GIF("gif", Pattern.compile(".*\\.gif$", Pattern.CASE_INSENSITIVE));
franta-hg@6
   112
franta-hg@6
   113
		private final String format;
franta-hg@6
   114
		private final Pattern regex;
franta-hg@6
   115
franta-hg@6
   116
		private ImageFormat(String format, Pattern regex) {
franta-hg@6
   117
			this.format = format;
franta-hg@6
   118
			this.regex = regex;
franta-hg@6
   119
		}
franta-hg@6
   120
franta-hg@7
   121
		/**
franta-hg@7
   122
		 * @return format name for {@linkplain ImageIO#write(java.awt.image.RenderedImage, java.lang.String, java.io.File)
franta-hg@7
   123
		 */
franta-hg@6
   124
		public String getFormat() {
franta-hg@6
   125
			return format;
franta-hg@6
   126
		}
franta-hg@6
   127
franta-hg@6
   128
		public static ImageFormat getMatching(String fileName) {
franta-hg@6
   129
			for (ImageFormat type : values()) {
franta-hg@6
   130
				if (type.regex.matcher(fileName).matches()) {
franta-hg@6
   131
					return type;
franta-hg@6
   132
				}
franta-hg@6
   133
			}
franta-hg@6
   134
			return null;
franta-hg@6
   135
		}
franta-hg@6
   136
	}
franta-hg@6
   137
}