diff --git a/src/Main.java b/src/Main.java index cf48562..d127fd0 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,29 +1,128 @@ +import java.io.File; +import java.io.IOException; +import java.util.Collection; +import java.util.LinkedList; + +import javax.swing.JFileChooser; +import javax.swing.filechooser.FileFilter; + import xkcd.construction.ImageArranger; import xkcd.construction.ImageDataLoader; import xkcd.construction.ImageSticher; import xkcd.rectangle.imagepath.PathRectangle; -import java.io.File; -import java.io.IOException; -import java.util.Collection; - public class Main { - static final String PATH = "C:\\Users\\Hannes\\xkcd\\"; + private double aspectRatio; + private Collection inputImages; + private File outputImagePath; - public static void main(final String[] args) throws IOException { - System.out.println("Loading Now!"); - final ImageDataLoader loader = new ImageDataLoader(); - Collection images = loader.load(new File(PATH)); + public Main() { + this.aspectRatio = 3D / 2D; + } - System.out.println("Arranging Now!"); - final ImageArranger arranger = new ImageArranger(); - images = arranger.arrangeRectangles(images, 3D / 2D); - System.out.println("Count: " + images.size()); + public Main(final double outputAspectRatio) { + this.aspectRatio = outputAspectRatio; + } - System.out.println("Saving Now!"); - final ImageSticher sticher = new ImageSticher(images); - sticher.saveImage(new File("C:\\Users\\Hannes\\Desktop\\savedNew.png")); - System.out.println("DONE!"); - } + public Main(final Collection input, final File output) { + this.inputImages = input; + this.outputImagePath = output; + } + + public Main(final Collection input, final File output, final double outputAspectRatio) { + this.aspectRatio = outputAspectRatio; + this.inputImages = input; + this.outputImagePath = output; + } + + public void selectFilesDialog() { + this.inputImages = new LinkedList<>(); + + JFileChooser fileChooser; + fileChooser = new JFileChooser(); + + // FileOpenDialog + fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); + fileChooser.setDialogTitle("Load Images File or Directory"); + fileChooser.setMultiSelectionEnabled(true); + int returnVal = fileChooser.showOpenDialog(null); + + if (returnVal != JFileChooser.APPROVE_OPTION) { + System.out.println("Aborted!"); + return; + } + + for (final File file : fileChooser.getSelectedFiles()) { + inputImages.add(file); + } + + // FileSaveDialog + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + fileChooser.setDialogTitle("Save Image"); + fileChooser.setMultiSelectionEnabled(false); + fileChooser.setFileFilter(new FileFilter() { + + @Override + public String getDescription() { + return ".png"; + } + + @Override + public boolean accept(final File f) { + return true; + } + }); + + returnVal = fileChooser.showSaveDialog(null); + if (returnVal != JFileChooser.APPROVE_OPTION) { + System.out.println("Aborted!"); + return; + } + if (!fileChooser.getSelectedFile().getName().endsWith(".png")) { + fileChooser.setSelectedFile(new File(fileChooser.getSelectedFile().getAbsolutePath() + ".png")); + } + + this.outputImagePath = fileChooser.getSelectedFile(); + } + + public void run() { + if (inputImages == null || inputImages.size() == 0 || outputImagePath == null) { + return; + } + + System.out.println("Loading Now!"); + final ImageDataLoader loader = new ImageDataLoader(); + + Collection images; + if (inputImages.size() == 1 && ((File) inputImages.toArray()[0]).isDirectory()) { + images = loader.load(((File) inputImages.toArray()[0])); + } else { + images = loader.load(inputImages); + } + + System.out.println("Arranging Now!"); + final ImageArranger arranger = new ImageArranger(); + images = arranger.arrangeRectangles(images, aspectRatio); + System.out.println("Count: " + images.size()); + + System.out.println("Saving Now!"); + final ImageSticher sticher = new ImageSticher(images); + + try { + // TODO: take scaling into account + sticher.saveImage(outputImagePath); + } catch (final IOException e) { + // TODO: catch exception in appropriate way + e.printStackTrace(); + } + + System.out.println("DONE!"); + } + + public static void main(final String[] args) throws IOException { + final Main main = new Main(); + main.selectFilesDialog(); + main.run(); + } } diff --git a/src/xkcd/ImageMegapixelComparator.java b/src/xkcd/ImageMegapixelComparator.java index d0ec52f..d9dc5de 100644 --- a/src/xkcd/ImageMegapixelComparator.java +++ b/src/xkcd/ImageMegapixelComparator.java @@ -14,9 +14,8 @@ import java.util.Comparator; */ public class ImageMegapixelComparator implements Comparator { - @Override - public int compare(BufferedImage o1, BufferedImage o2) { - return o2.getWidth() * o2.getHeight() - o1.getWidth() * o1.getHeight(); - } - + @Override + public int compare(final BufferedImage o1, final BufferedImage o2) { + return o2.getWidth() * o2.getHeight() - o1.getWidth() * o1.getHeight(); + } } diff --git a/src/xkcd/construction/ImageDataLoader.java b/src/xkcd/construction/ImageDataLoader.java index 8be25c1..94bd215 100644 --- a/src/xkcd/construction/ImageDataLoader.java +++ b/src/xkcd/construction/ImageDataLoader.java @@ -5,56 +5,83 @@ */ package xkcd.construction; -import xkcd.rectangle.imagepath.PathRectangle; - -import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; -import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; + +import javax.imageio.ImageIO; + +import xkcd.rectangle.imagepath.PathRectangle; /** * @author Hannes */ public class ImageDataLoader { - public ImageDataLoader() { - } + public ImageDataLoader() { + } - public Collection load(final File path) { - final Collection rectangles = new ArrayList<>(); + /** + * loads images from a directory and all subdirectories + * + * @param directoryPath + * path to the directory + * @return returns a collection of loaded pathrectangles + */ + public Collection load(final File directoryPath) { + return load(loadFromDirectory(directoryPath)); + } - rectangles.addAll(loadFromDirectory(path)); + /** + * Loads images from the specified paths in the collection + * + * @param paths + * collection of paths to the images that should be loaded + * @return returns the loaded pathRectangles + */ + public Collection load(final Collection paths) { + final Collection images = new LinkedList<>(); + for (final File file : paths) { + if (loadFile(file) != null) { + images.add(loadFile(file)); + } + } + return images; + } - return rectangles; - } + /** + * Searches recursively through a directory and adds all files to a list that is + * returned + * + * @param directory + * the directory that will be searched through + * @return returns all the added files from the directory + */ + private Collection loadFromDirectory(final File directory) { + final Collection files = new LinkedList<>(); - private Collection loadFromDirectory(final File directory) { - final Collection rectangles = new ArrayList<>(); + for (final File fileEntry : directory.listFiles()) { + if (fileEntry.isDirectory()) { + files.addAll(loadFromDirectory(fileEntry)); + } else { + files.add(fileEntry); + } + } + return files; + } - for (final File fileEntry : directory.listFiles()) { - if (fileEntry.isDirectory()) { - rectangles.addAll(loadFromDirectory(fileEntry)); - } else { - if (loadFile(fileEntry) != null) { - rectangles.add(loadFile(fileEntry)); - } - } - } - return rectangles; - } + private PathRectangle loadFile(final File file) { + try { + final BufferedImage tmpImage = ImageIO.read(file); + // TODO use correct rotation data of an Image and rotate image if necessary + if (tmpImage == null) { + return null; + } + return new PathRectangle(file, 0, 0, tmpImage.getWidth(), tmpImage.getHeight()); - private PathRectangle loadFile(final File file) { - try { - final BufferedImage tmpImage = ImageIO.read(file); - //TODO use correct rotation data of an Image and rotate image if necessary - if (tmpImage == null) { - return null; - } - return new PathRectangle(file, 0, 0, tmpImage.getWidth(), tmpImage.getHeight()); - - } catch (final IOException ex) { - return null; - } - } + } catch (final IOException ex) { + return null; + } + } } diff --git a/src/xkcd/construction/ImageSticher.java b/src/xkcd/construction/ImageSticher.java index 029a445..0c3c11f 100644 --- a/src/xkcd/construction/ImageSticher.java +++ b/src/xkcd/construction/ImageSticher.java @@ -5,71 +5,76 @@ */ package xkcd.construction; -import xkcd.rectangle.imagepath.PathRectangle; - -import javax.imageio.ImageIO; -import java.awt.*; +import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Collection; +import javax.imageio.ImageIO; + +import xkcd.rectangle.imagepath.PathRectangle; + /** * @author Hannes */ public class ImageSticher { - private final double scaleTo = 0.1D; - private static final int IMAGE_TYPE = BufferedImage.TYPE_4BYTE_ABGR; - private static final Color BACKGROUND_COLOR = Color.BLACK; + private static final double DEFAULT_SCALE = 0.1D; + private static final int IMAGE_TYPE = BufferedImage.TYPE_4BYTE_ABGR; + private static final Color BACKGROUND_COLOR = Color.BLACK; - private BufferedImage collage; - private final Collection rectangles; + private BufferedImage collage; + private final Collection rectangles; - public ImageSticher(final Collection rect) { - this.rectangles = rect; - //System.out.println("Count: " + rectangles.size()); - this.initBufferedImage(); - } + public ImageSticher(final Collection rect) { + this.rectangles = rect; + } - public void saveImage(final File saveFile) throws IOException { - initBufferedImage(); - for (final PathRectangle rectangle : rectangles) { - try { - final BufferedImage image = loadImage(rectangle.getPath()); - //System.out.println(rectangle); - collage.getGraphics().drawImage(image, (int) (rectangle.getX() * scaleTo), (int) (rectangle.getY() * scaleTo), - (int) (rectangle.getWidth() * scaleTo), (int) (rectangle.getHeight() * scaleTo), null); - } catch (final IOException ex) { - System.err.println(ex.getMessage()); - } - } - ImageIO.write(collage, "png", saveFile); - } + public void saveImage(final File saveFile) throws IOException { + saveImage(saveFile, DEFAULT_SCALE); + } - private void initBufferedImage() { - int width = 0; - int height = 0; - for (final PathRectangle rectangle : rectangles) { - final int tmpWidth = (int) (rectangle.getWidth() + rectangle.getX()); - final int tmpHeight = (int) (rectangle.getHeight() + rectangle.getY()); + public void saveImage(final File saveFile, final double scale) throws IOException { + initBufferedImage(scale); + for (final PathRectangle rectangle : rectangles) { + try { + final BufferedImage image = loadImage(rectangle.getPath()); + // System.out.println(rectangle); + collage.getGraphics().drawImage(image, (int) (rectangle.getX() * scale), + (int) (rectangle.getY() * scale), (int) (rectangle.getWidth() * scale), + (int) (rectangle.getHeight() * scale), null); + } catch (final IOException ex) { + System.err.println(ex.getMessage()); + } + } + ImageIO.write(collage, "png", saveFile); + } + private void initBufferedImage(final double scale) { + int width = 0; + int height = 0; + for (final PathRectangle rectangle : rectangles) { + final int tmpWidth = (int) (rectangle.getWidth() + rectangle.getX()); + final int tmpHeight = (int) (rectangle.getHeight() + rectangle.getY()); - if (tmpWidth > width) { - width = tmpWidth; - } - if (tmpHeight > height) { - height = tmpHeight; - } - } + if (tmpWidth > width) { + width = tmpWidth; + } + if (tmpHeight > height) { + height = tmpHeight; + } + } - collage = new BufferedImage((int) (width * scaleTo), (int) (height * scaleTo), IMAGE_TYPE); + collage = new BufferedImage((int) (width * scale), (int) (height * scale), IMAGE_TYPE); - //collage.getGraphics().setColor(BACKGROUND_COLOR); - //collage.getGraphics().drawRect(0, 0, collage.getWidth(), collage.getHeight()); - //collage.getGraphics().fillRect(0, 0, collage.getWidth(), collage.getHeight()); - } + // collage.getGraphics().setColor(BACKGROUND_COLOR); + // collage.getGraphics().drawRect(0, 0, collage.getWidth(), + // collage.getHeight()); + // collage.getGraphics().fillRect(0, 0, collage.getWidth(), + // collage.getHeight()); + } - private BufferedImage loadImage(final File filePath) throws IOException { - return ImageIO.read(filePath); - } + private BufferedImage loadImage(final File filePath) throws IOException { + return ImageIO.read(filePath); + } }