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