diff -r 4634176c6602 -r 505031965440 java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java Mon Nov 17 21:58:52 2014 +0100 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java Mon Nov 17 21:59:20 2014 +0100 @@ -17,23 +17,14 @@ */ package cz.frantovo.copyImageResizer; -import java.awt.Graphics2D; -import java.awt.image.BufferedImage; -import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.util.logging.Level; -import java.util.logging.Logger; -import java.util.regex.Pattern; -import javax.imageio.ImageIO; /** * * @author Ing. František Kučera (frantovo.cz) */ -public class SingleImageResizer { - - private static final Logger log = Logger.getLogger(SingleImageResizer.class.getName()); +public interface SingleImageResizer { /** * @@ -46,92 +37,5 @@ * }. * @throws ResizeException */ - public boolean resize(InputStream input, OutputStream output, SizeSpecification size, ImageFormat outputFormat) throws ResizeException { - try { - BufferedImage image = readImage(input); - if (shouldResize(image, size)) { - BufferedImage resized = resize(image, size.getWidth(), size.getHeight(), image.getType()); - ImageIO.write(resized, outputFormat.getFormat(), output); - return true; - } else { - return false; - } - } catch (IOException e) { - throw new ResizeException("Error while resizing image", e); - } - } - - private BufferedImage resize(BufferedImage original, int maxWidth, int maxHeight, int type) { - - int width; - int height; - float aspectRatio = (float) original.getWidth() / (float) original.getHeight(); - - if (aspectRatio > 1) { - width = maxWidth; - height = Math.max(Math.round(maxWidth / aspectRatio), 1); - } else { - width = Math.max(Math.round(maxHeight * aspectRatio), 1); - height = maxHeight; - } - - if (type == BufferedImage.TYPE_CUSTOM) { - log.log(Level.FINER, "Setting default image type: from TYPE_CUSTOM to TYPE_INT_ARGB"); - type = BufferedImage.TYPE_INT_ARGB; - } - - BufferedImage resized = new BufferedImage(width, height, type); - Graphics2D g = resized.createGraphics(); - g.drawImage(original, 0, 0, width, height, null); - g.dispose(); - - return resized; - } - - private static BufferedImage readImage(InputStream input) throws ResizeException { - try { - return ImageIO.read(input); - } catch (IOException e) { - throw new ResizeException("Unable to read image from stream", e); - } - } - - private static boolean shouldResize(BufferedImage input, SizeSpecification requested) { - if (requested.isResizeSmaller()) { - return input.getHeight() != requested.getHeight() || input.getWidth() != requested.getWidth(); - } else { - return input.getHeight() > requested.getHeight() || input.getWidth() > requested.getWidth(); - } - } - - public static enum ImageFormat { - - JPEG("jpg", Pattern.compile(".*\\.(jpg|jpeg)$", Pattern.CASE_INSENSITIVE)), - PNG("png", Pattern.compile(".*\\.png$", Pattern.CASE_INSENSITIVE)), - GIF("gif", Pattern.compile(".*\\.gif$", Pattern.CASE_INSENSITIVE)); - - private final String format; - private final Pattern regex; - - private ImageFormat(String format, Pattern regex) { - this.format = format; - this.regex = regex; - } - - /** - * @return format name for {@linkplain ImageIO#write(java.awt.image.RenderedImage, java.lang.String, java.io.File) - */ - public String getFormat() { - return format; - } - - public static ImageFormat getMatching(String fileName) { - for (ImageFormat type : values()) { - if (type.regex.matcher(fileName).matches()) { - return type; - } - } - return null; - } - } + public boolean resize(InputStream input, OutputStream output, SizeSpecification size, JavaSingleImageResizer.ImageFormat outputFormat) throws ResizeException; }