java/copy-image-resizer/src/cz/frantovo/copyImageResizer/JavaSingleImageResizer.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 17 Nov 2014 21:59:20 +0100
changeset 17 505031965440
parent 15 java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java@93fa6ce675e5
child 19 cda209705642
permissions -rw-r--r--
preparation for later support of NativeSingleImageResizer
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@17
    34
public class JavaSingleImageResizer implements 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@17
    38
	@Override
franta-hg@15
    39
	public boolean resize(InputStream input, OutputStream output, SizeSpecification size, ImageFormat outputFormat) throws ResizeException {
franta-hg@6
    40
		try {
franta-hg@15
    41
			BufferedImage image = readImage(input);
franta-hg@15
    42
			if (shouldResize(image, size)) {
franta-hg@15
    43
				BufferedImage resized = resize(image, size.getWidth(), size.getHeight(), image.getType());
franta-hg@15
    44
				ImageIO.write(resized, outputFormat.getFormat(), output);
franta-hg@15
    45
				return true;
franta-hg@15
    46
			} else {
franta-hg@15
    47
				return false;
franta-hg@15
    48
			}
franta-hg@6
    49
		} catch (IOException e) {
franta-hg@6
    50
			throw new ResizeException("Error while resizing image", e);
franta-hg@6
    51
		}
franta-hg@6
    52
	}
franta-hg@6
    53
franta-hg@6
    54
	private BufferedImage resize(BufferedImage original, int maxWidth, int maxHeight, int type) {
franta-hg@6
    55
franta-hg@6
    56
		int width;
franta-hg@6
    57
		int height;
franta-hg@6
    58
		float aspectRatio = (float) original.getWidth() / (float) original.getHeight();
franta-hg@6
    59
franta-hg@6
    60
		if (aspectRatio > 1) {
franta-hg@6
    61
			width = maxWidth;
franta-hg@6
    62
			height = Math.max(Math.round(maxWidth / aspectRatio), 1);
franta-hg@6
    63
		} else {
franta-hg@6
    64
			width = Math.max(Math.round(maxHeight * aspectRatio), 1);
franta-hg@6
    65
			height = maxHeight;
franta-hg@6
    66
		}
franta-hg@6
    67
franta-hg@7
    68
		if (type == BufferedImage.TYPE_CUSTOM) {
franta-hg@10
    69
			log.log(Level.FINER, "Setting default image type: from TYPE_CUSTOM to TYPE_INT_ARGB");
franta-hg@7
    70
			type = BufferedImage.TYPE_INT_ARGB;
franta-hg@7
    71
		}
franta-hg@7
    72
franta-hg@6
    73
		BufferedImage resized = new BufferedImage(width, height, type);
franta-hg@6
    74
		Graphics2D g = resized.createGraphics();
franta-hg@6
    75
		g.drawImage(original, 0, 0, width, height, null);
franta-hg@6
    76
		g.dispose();
franta-hg@6
    77
franta-hg@6
    78
		return resized;
franta-hg@6
    79
	}
franta-hg@6
    80
franta-hg@15
    81
	private static BufferedImage readImage(InputStream input) throws ResizeException {
franta-hg@15
    82
		try {
franta-hg@15
    83
			return ImageIO.read(input);
franta-hg@15
    84
		} catch (IOException e) {
franta-hg@15
    85
			throw new ResizeException("Unable to read image from stream", e);
franta-hg@15
    86
		}
franta-hg@15
    87
	}
franta-hg@15
    88
franta-hg@15
    89
	private static boolean shouldResize(BufferedImage input, SizeSpecification requested) {
franta-hg@15
    90
		if (requested.isResizeSmaller()) {
franta-hg@15
    91
			return input.getHeight() != requested.getHeight() || input.getWidth() != requested.getWidth();
franta-hg@15
    92
		} else {
franta-hg@15
    93
			return input.getHeight() > requested.getHeight() || input.getWidth() > requested.getWidth();
franta-hg@15
    94
		}
franta-hg@15
    95
	}
franta-hg@15
    96
franta-hg@6
    97
	public static enum ImageFormat {
franta-hg@6
    98
franta-hg@6
    99
		JPEG("jpg", Pattern.compile(".*\\.(jpg|jpeg)$", Pattern.CASE_INSENSITIVE)),
franta-hg@6
   100
		PNG("png", Pattern.compile(".*\\.png$", Pattern.CASE_INSENSITIVE)),
franta-hg@6
   101
		GIF("gif", Pattern.compile(".*\\.gif$", Pattern.CASE_INSENSITIVE));
franta-hg@6
   102
franta-hg@6
   103
		private final String format;
franta-hg@6
   104
		private final Pattern regex;
franta-hg@6
   105
franta-hg@6
   106
		private ImageFormat(String format, Pattern regex) {
franta-hg@6
   107
			this.format = format;
franta-hg@6
   108
			this.regex = regex;
franta-hg@6
   109
		}
franta-hg@6
   110
franta-hg@7
   111
		/**
franta-hg@7
   112
		 * @return format name for {@linkplain ImageIO#write(java.awt.image.RenderedImage, java.lang.String, java.io.File)
franta-hg@7
   113
		 */
franta-hg@6
   114
		public String getFormat() {
franta-hg@6
   115
			return format;
franta-hg@6
   116
		}
franta-hg@6
   117
franta-hg@6
   118
		public static ImageFormat getMatching(String fileName) {
franta-hg@6
   119
			for (ImageFormat type : values()) {
franta-hg@6
   120
				if (type.regex.matcher(fileName).matches()) {
franta-hg@6
   121
					return type;
franta-hg@6
   122
				}
franta-hg@6
   123
			}
franta-hg@6
   124
			return null;
franta-hg@6
   125
		}
franta-hg@6
   126
	}
franta-hg@6
   127
}