# HG changeset patch # User František Kučera # Date 1416183422 -3600 # Node ID b329573c76d7cc60a1e175823c13418e3f1e59dd # Parent 5019e3e93a4e3e1141674e52294658e6a5f88736 resize, fixed size, TODO: configured size + multiple sizes diff -r 5019e3e93a4e -r b329573c76d7 java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java Sun Nov 16 23:47:28 2014 +0100 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java Mon Nov 17 01:17:02 2014 +0100 @@ -17,13 +17,69 @@ */ package cz.frantovo.copyImageResizer; +import cz.frantovo.copyImageResizer.SingleImageResizer.ImageFormat; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.util.logging.Level; +import java.util.logging.Logger; + /** * * @author Ing. František Kučera (frantovo.cz) */ public class RecursiveImageResizer { + private static final Logger log = Logger.getLogger(RecursiveImageResizer.class.getName()); + + private final SingleImageResizer resizer = new SingleImageResizer(); + public void resize(RecursiveOptions options) throws RecursiveException, ResizeException { + resizeDirectory(options.getInput(), options); + } + + private void resizeFile(File file, RecursiveOptions options) throws ResizeException { + log.log(Level.FINE, "Resizing file: {0}", relativize(options.getInput(), file)); + + ImageFormat format = ImageFormat.getMatching(file.getName()); + + if (format == null) { + log.log(Level.FINE, "Skipping file: {0} (no image format matched this extension)", relativize(options.getInput(), file)); + } else { + try { + FileInputStream input = new FileInputStream(file); + File relative = relativize(options.getInput(), file); + File outputFile = new File(options.getOutput(), relative.getPath()); + FileOutputStream output = new FileOutputStream(outputFile); + resizer.resize(input, output, format); + } catch (FileNotFoundException e) { + throw new ResizeException("Error while opening stream", e); + } + } + + } + + private void resizeDirectory(File directory, RecursiveOptions options) throws ResizeException { + + log.log(Level.FINE, "Resizing directory: {0}", directory); + + File relative = relativize(options.getInput(), directory); + File output = new File(options.getOutput(), relative.getPath()); + output.mkdirs(); + + for (File entry : directory.listFiles()) { + if (entry.isDirectory()) { + resizeDirectory(entry, options); + } else { + resizeFile(entry, options); + } + } + + } + + private static File relativize(File root, File child) { + return root.toPath().relativize(child.toPath()).toFile(); } } diff -r 5019e3e93a4e -r b329573c76d7 java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveOptions.java --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveOptions.java Sun Nov 16 23:47:28 2014 +0100 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveOptions.java Mon Nov 17 01:17:02 2014 +0100 @@ -22,7 +22,7 @@ import java.util.Collection; /** - * + * * @author Ing. František Kučera (frantovo.cz) */ public class RecursiveOptions { @@ -40,7 +40,7 @@ */ private final Collection sizes = new ArrayList<>(); - public File getSourceDir() { + public File getInput() { return input; } @@ -48,7 +48,7 @@ this.input = input; } - public File getDestinationDir() { + public File getOutput() { return output; } @@ -87,5 +87,10 @@ public String getDirectory() { return directory; } + + @Override + public String toString() { + return width + "×" + height + " → " + directory; + } } } diff -r 5019e3e93a4e -r b329573c76d7 java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java Mon Nov 17 01:17:02 2014 +0100 @@ -0,0 +1,94 @@ +/** + * copy-image-resizer + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +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.regex.Pattern; +import javax.imageio.ImageIO; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class SingleImageResizer { + + public void resize(InputStream input, OutputStream output, ImageFormat outputFormat) throws ResizeException { + try { + BufferedImage image = ImageIO.read(input); + BufferedImage resized = resize(image, 64, 64, image.getType()); + + ImageIO.write(resized, outputFormat.getFormat(), output); + } 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; + } + + BufferedImage resized = new BufferedImage(width, height, type); + Graphics2D g = resized.createGraphics(); + g.drawImage(original, 0, 0, width, height, null); + g.dispose(); + + return resized; + } + + 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; + } + + 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; + } + } +}