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