1.1 --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java Sun Nov 16 23:47:28 2014 +0100
1.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java Mon Nov 17 01:17:02 2014 +0100
1.3 @@ -17,13 +17,69 @@
1.4 */
1.5 package cz.frantovo.copyImageResizer;
1.6
1.7 +import cz.frantovo.copyImageResizer.SingleImageResizer.ImageFormat;
1.8 +import java.io.File;
1.9 +import java.io.FileInputStream;
1.10 +import java.io.FileNotFoundException;
1.11 +import java.io.FileOutputStream;
1.12 +import java.util.logging.Level;
1.13 +import java.util.logging.Logger;
1.14 +
1.15 /**
1.16 *
1.17 * @author Ing. František Kučera (frantovo.cz)
1.18 */
1.19 public class RecursiveImageResizer {
1.20
1.21 + private static final Logger log = Logger.getLogger(RecursiveImageResizer.class.getName());
1.22 +
1.23 + private final SingleImageResizer resizer = new SingleImageResizer();
1.24 +
1.25 public void resize(RecursiveOptions options) throws RecursiveException, ResizeException {
1.26 + resizeDirectory(options.getInput(), options);
1.27 + }
1.28 +
1.29 + private void resizeFile(File file, RecursiveOptions options) throws ResizeException {
1.30 + log.log(Level.FINE, "Resizing file: {0}", relativize(options.getInput(), file));
1.31 +
1.32 + ImageFormat format = ImageFormat.getMatching(file.getName());
1.33 +
1.34 + if (format == null) {
1.35 + log.log(Level.FINE, "Skipping file: {0} (no image format matched this extension)", relativize(options.getInput(), file));
1.36 + } else {
1.37 + try {
1.38 + FileInputStream input = new FileInputStream(file);
1.39 + File relative = relativize(options.getInput(), file);
1.40 + File outputFile = new File(options.getOutput(), relative.getPath());
1.41 + FileOutputStream output = new FileOutputStream(outputFile);
1.42 + resizer.resize(input, output, format);
1.43 + } catch (FileNotFoundException e) {
1.44 + throw new ResizeException("Error while opening stream", e);
1.45 + }
1.46 + }
1.47 +
1.48 + }
1.49 +
1.50 + private void resizeDirectory(File directory, RecursiveOptions options) throws ResizeException {
1.51 +
1.52 + log.log(Level.FINE, "Resizing directory: {0}", directory);
1.53 +
1.54 + File relative = relativize(options.getInput(), directory);
1.55 + File output = new File(options.getOutput(), relative.getPath());
1.56 + output.mkdirs();
1.57 +
1.58 + for (File entry : directory.listFiles()) {
1.59 + if (entry.isDirectory()) {
1.60 + resizeDirectory(entry, options);
1.61 + } else {
1.62 + resizeFile(entry, options);
1.63 + }
1.64 + }
1.65 +
1.66 + }
1.67 +
1.68 + private static File relativize(File root, File child) {
1.69 + return root.toPath().relativize(child.toPath()).toFile();
1.70 }
1.71
1.72 }
2.1 --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveOptions.java Sun Nov 16 23:47:28 2014 +0100
2.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveOptions.java Mon Nov 17 01:17:02 2014 +0100
2.3 @@ -22,7 +22,7 @@
2.4 import java.util.Collection;
2.5
2.6 /**
2.7 - *
2.8 + *
2.9 * @author Ing. František Kučera (frantovo.cz)
2.10 */
2.11 public class RecursiveOptions {
2.12 @@ -40,7 +40,7 @@
2.13 */
2.14 private final Collection<SizeSpecification> sizes = new ArrayList<>();
2.15
2.16 - public File getSourceDir() {
2.17 + public File getInput() {
2.18 return input;
2.19 }
2.20
2.21 @@ -48,7 +48,7 @@
2.22 this.input = input;
2.23 }
2.24
2.25 - public File getDestinationDir() {
2.26 + public File getOutput() {
2.27 return output;
2.28 }
2.29
2.30 @@ -87,5 +87,10 @@
2.31 public String getDirectory() {
2.32 return directory;
2.33 }
2.34 +
2.35 + @Override
2.36 + public String toString() {
2.37 + return width + "×" + height + " → " + directory;
2.38 + }
2.39 }
2.40 }
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/SingleImageResizer.java Mon Nov 17 01:17:02 2014 +0100
3.3 @@ -0,0 +1,94 @@
3.4 +/**
3.5 + * copy-image-resizer
3.6 + * Copyright © 2014 František Kučera (frantovo.cz)
3.7 + *
3.8 + * This program is free software: you can redistribute it and/or modify
3.9 + * it under the terms of the GNU General Public License as published by
3.10 + * the Free Software Foundation, either version 3 of the License, or
3.11 + * (at your option) any later version.
3.12 + *
3.13 + * This program is distributed in the hope that it will be useful,
3.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3.16 + * GNU General Public License for more details.
3.17 + *
3.18 + * You should have received a copy of the GNU General Public License
3.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
3.20 + */
3.21 +package cz.frantovo.copyImageResizer;
3.22 +
3.23 +import java.awt.Graphics2D;
3.24 +import java.awt.image.BufferedImage;
3.25 +import java.io.IOException;
3.26 +import java.io.InputStream;
3.27 +import java.io.OutputStream;
3.28 +import java.util.regex.Pattern;
3.29 +import javax.imageio.ImageIO;
3.30 +
3.31 +/**
3.32 + *
3.33 + * @author Ing. František Kučera (frantovo.cz)
3.34 + */
3.35 +public class SingleImageResizer {
3.36 +
3.37 + public void resize(InputStream input, OutputStream output, ImageFormat outputFormat) throws ResizeException {
3.38 + try {
3.39 + BufferedImage image = ImageIO.read(input);
3.40 + BufferedImage resized = resize(image, 64, 64, image.getType());
3.41 +
3.42 + ImageIO.write(resized, outputFormat.getFormat(), output);
3.43 + } catch (IOException e) {
3.44 + throw new ResizeException("Error while resizing image", e);
3.45 + }
3.46 + }
3.47 +
3.48 + private BufferedImage resize(BufferedImage original, int maxWidth, int maxHeight, int type) {
3.49 +
3.50 + int width;
3.51 + int height;
3.52 + float aspectRatio = (float) original.getWidth() / (float) original.getHeight();
3.53 +
3.54 + if (aspectRatio > 1) {
3.55 + width = maxWidth;
3.56 + height = Math.max(Math.round(maxWidth / aspectRatio), 1);
3.57 + } else {
3.58 + width = Math.max(Math.round(maxHeight * aspectRatio), 1);
3.59 + height = maxHeight;
3.60 + }
3.61 +
3.62 + BufferedImage resized = new BufferedImage(width, height, type);
3.63 + Graphics2D g = resized.createGraphics();
3.64 + g.drawImage(original, 0, 0, width, height, null);
3.65 + g.dispose();
3.66 +
3.67 + return resized;
3.68 + }
3.69 +
3.70 + public static enum ImageFormat {
3.71 +
3.72 + JPEG("jpg", Pattern.compile(".*\\.(jpg|jpeg)$", Pattern.CASE_INSENSITIVE)),
3.73 + PNG("png", Pattern.compile(".*\\.png$", Pattern.CASE_INSENSITIVE)),
3.74 + GIF("gif", Pattern.compile(".*\\.gif$", Pattern.CASE_INSENSITIVE));
3.75 +
3.76 + private final String format;
3.77 + private final Pattern regex;
3.78 +
3.79 + private ImageFormat(String format, Pattern regex) {
3.80 + this.format = format;
3.81 + this.regex = regex;
3.82 + }
3.83 +
3.84 + public String getFormat() {
3.85 + return format;
3.86 + }
3.87 +
3.88 + public static ImageFormat getMatching(String fileName) {
3.89 + for (ImageFormat type : values()) {
3.90 + if (type.regex.matcher(fileName).matches()) {
3.91 + return type;
3.92 + }
3.93 + }
3.94 + return null;
3.95 + }
3.96 + }
3.97 +}