java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 17 Nov 2014 18:13:59 +0100
changeset 10 a5a723467a39
parent 7 8e9983260624
child 15 93fa6ce675e5
permissions -rw-r--r--
logging, imports, constants
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@6
    23
import java.io.OutputStream;
franta-hg@7
    24
import java.util.logging.Level;
franta-hg@7
    25
import java.util.logging.Logger;
franta-hg@6
    26
import java.util.regex.Pattern;
franta-hg@6
    27
import javax.imageio.ImageIO;
franta-hg@6
    28
franta-hg@6
    29
/**
franta-hg@6
    30
 *
franta-hg@6
    31
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@6
    32
 */
franta-hg@6
    33
public class SingleImageResizer {
franta-hg@6
    34
franta-hg@7
    35
	private static final Logger log = Logger.getLogger(SingleImageResizer.class.getName());
franta-hg@7
    36
franta-hg@7
    37
	public void resize(BufferedImage input, OutputStream output, SizeSpecification size, ImageFormat outputFormat) throws ResizeException {
franta-hg@6
    38
		try {
franta-hg@7
    39
			BufferedImage resized = resize(input, size.getWidth(), size.getHeight(), input.getType());
franta-hg@6
    40
franta-hg@6
    41
			ImageIO.write(resized, outputFormat.getFormat(), output);
franta-hg@6
    42
		} catch (IOException e) {
franta-hg@6
    43
			throw new ResizeException("Error while resizing image", e);
franta-hg@6
    44
		}
franta-hg@6
    45
	}
franta-hg@6
    46
franta-hg@6
    47
	private BufferedImage resize(BufferedImage original, int maxWidth, int maxHeight, int type) {
franta-hg@6
    48
franta-hg@6
    49
		int width;
franta-hg@6
    50
		int height;
franta-hg@6
    51
		float aspectRatio = (float) original.getWidth() / (float) original.getHeight();
franta-hg@6
    52
franta-hg@6
    53
		if (aspectRatio > 1) {
franta-hg@6
    54
			width = maxWidth;
franta-hg@6
    55
			height = Math.max(Math.round(maxWidth / aspectRatio), 1);
franta-hg@6
    56
		} else {
franta-hg@6
    57
			width = Math.max(Math.round(maxHeight * aspectRatio), 1);
franta-hg@6
    58
			height = maxHeight;
franta-hg@6
    59
		}
franta-hg@6
    60
franta-hg@7
    61
		if (type == BufferedImage.TYPE_CUSTOM) {
franta-hg@10
    62
			log.log(Level.FINER, "Setting default image type: from TYPE_CUSTOM to TYPE_INT_ARGB");
franta-hg@7
    63
			type = BufferedImage.TYPE_INT_ARGB;
franta-hg@7
    64
		}
franta-hg@7
    65
franta-hg@6
    66
		BufferedImage resized = new BufferedImage(width, height, type);
franta-hg@6
    67
		Graphics2D g = resized.createGraphics();
franta-hg@6
    68
		g.drawImage(original, 0, 0, width, height, null);
franta-hg@6
    69
		g.dispose();
franta-hg@6
    70
franta-hg@6
    71
		return resized;
franta-hg@6
    72
	}
franta-hg@6
    73
franta-hg@6
    74
	public static enum ImageFormat {
franta-hg@6
    75
franta-hg@6
    76
		JPEG("jpg", Pattern.compile(".*\\.(jpg|jpeg)$", Pattern.CASE_INSENSITIVE)),
franta-hg@6
    77
		PNG("png", Pattern.compile(".*\\.png$", Pattern.CASE_INSENSITIVE)),
franta-hg@6
    78
		GIF("gif", Pattern.compile(".*\\.gif$", Pattern.CASE_INSENSITIVE));
franta-hg@6
    79
franta-hg@6
    80
		private final String format;
franta-hg@6
    81
		private final Pattern regex;
franta-hg@6
    82
franta-hg@6
    83
		private ImageFormat(String format, Pattern regex) {
franta-hg@6
    84
			this.format = format;
franta-hg@6
    85
			this.regex = regex;
franta-hg@6
    86
		}
franta-hg@6
    87
franta-hg@7
    88
		/**
franta-hg@7
    89
		 * @return format name for {@linkplain ImageIO#write(java.awt.image.RenderedImage, java.lang.String, java.io.File)
franta-hg@7
    90
		 */
franta-hg@6
    91
		public String getFormat() {
franta-hg@6
    92
			return format;
franta-hg@6
    93
		}
franta-hg@6
    94
franta-hg@6
    95
		public static ImageFormat getMatching(String fileName) {
franta-hg@6
    96
			for (ImageFormat type : values()) {
franta-hg@6
    97
				if (type.regex.matcher(fileName).matches()) {
franta-hg@6
    98
					return type;
franta-hg@6
    99
				}
franta-hg@6
   100
			}
franta-hg@6
   101
			return null;
franta-hg@6
   102
		}
franta-hg@6
   103
	}
franta-hg@6
   104
}