mirror of
https://github.com/13hannes11/xkcd_poster_creator.git
synced 2024-09-03 21:50:58 +02:00
added possibility to choose image paths via UI; TODO: enforce Model-View-Controller + add image downloader
This commit is contained in:
117
src/Main.java
117
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.ImageArranger;
|
||||||
import xkcd.construction.ImageDataLoader;
|
import xkcd.construction.ImageDataLoader;
|
||||||
import xkcd.construction.ImageSticher;
|
import xkcd.construction.ImageSticher;
|
||||||
import xkcd.rectangle.imagepath.PathRectangle;
|
import xkcd.rectangle.imagepath.PathRectangle;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
static final String PATH = "C:\\Users\\Hannes\\xkcd\\";
|
private double aspectRatio;
|
||||||
|
private Collection<File> inputImages;
|
||||||
|
private File outputImagePath;
|
||||||
|
|
||||||
|
public Main() {
|
||||||
|
this.aspectRatio = 3D / 2D;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Main(final double outputAspectRatio) {
|
||||||
|
this.aspectRatio = outputAspectRatio;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Main(final Collection<File> input, final File output) {
|
||||||
|
this.inputImages = input;
|
||||||
|
this.outputImagePath = output;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Main(final Collection<File> 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;
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(final String[] args) throws IOException {
|
|
||||||
System.out.println("Loading Now!");
|
System.out.println("Loading Now!");
|
||||||
final ImageDataLoader loader = new ImageDataLoader();
|
final ImageDataLoader loader = new ImageDataLoader();
|
||||||
Collection<PathRectangle> images = loader.load(new File(PATH));
|
|
||||||
|
Collection<PathRectangle> 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!");
|
System.out.println("Arranging Now!");
|
||||||
final ImageArranger arranger = new ImageArranger();
|
final ImageArranger arranger = new ImageArranger();
|
||||||
images = arranger.arrangeRectangles(images, 3D / 2D);
|
images = arranger.arrangeRectangles(images, aspectRatio);
|
||||||
System.out.println("Count: " + images.size());
|
System.out.println("Count: " + images.size());
|
||||||
|
|
||||||
System.out.println("Saving Now!");
|
System.out.println("Saving Now!");
|
||||||
final ImageSticher sticher = new ImageSticher(images);
|
final ImageSticher sticher = new ImageSticher(images);
|
||||||
sticher.saveImage(new File("C:\\Users\\Hannes\\Desktop\\savedNew.png"));
|
|
||||||
|
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!");
|
System.out.println("DONE!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(final String[] args) throws IOException {
|
||||||
|
final Main main = new Main();
|
||||||
|
main.selectFilesDialog();
|
||||||
|
main.run();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,8 +15,7 @@ import java.util.Comparator;
|
|||||||
public class ImageMegapixelComparator implements Comparator<BufferedImage> {
|
public class ImageMegapixelComparator implements Comparator<BufferedImage> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compare(BufferedImage o1, BufferedImage o2) {
|
public int compare(final BufferedImage o1, final BufferedImage o2) {
|
||||||
return o2.getWidth() * o2.getHeight() - o1.getWidth() * o1.getHeight();
|
return o2.getWidth() * o2.getHeight() - o1.getWidth() * o1.getHeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,14 +5,15 @@
|
|||||||
*/
|
*/
|
||||||
package xkcd.construction;
|
package xkcd.construction;
|
||||||
|
|
||||||
import xkcd.rectangle.imagepath.PathRectangle;
|
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
|
import xkcd.rectangle.imagepath.PathRectangle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Hannes
|
* @author Hannes
|
||||||
@@ -21,27 +22,53 @@ public class ImageDataLoader {
|
|||||||
public ImageDataLoader() {
|
public ImageDataLoader() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Collection<PathRectangle> load(final File path) {
|
/**
|
||||||
final Collection<PathRectangle> rectangles = new ArrayList<>();
|
* loads images from a directory and all subdirectories
|
||||||
|
*
|
||||||
rectangles.addAll(loadFromDirectory(path));
|
* @param directoryPath
|
||||||
|
* path to the directory
|
||||||
return rectangles;
|
* @return returns a collection of loaded pathrectangles
|
||||||
|
*/
|
||||||
|
public Collection<PathRectangle> load(final File directoryPath) {
|
||||||
|
return load(loadFromDirectory(directoryPath));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Collection<PathRectangle> loadFromDirectory(final File directory) {
|
/**
|
||||||
final Collection<PathRectangle> rectangles = new ArrayList<>();
|
* 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<PathRectangle> load(final Collection<File> paths) {
|
||||||
|
final Collection<PathRectangle> images = new LinkedList<>();
|
||||||
|
for (final File file : paths) {
|
||||||
|
if (loadFile(file) != null) {
|
||||||
|
images.add(loadFile(file));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return images;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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<File> loadFromDirectory(final File directory) {
|
||||||
|
final Collection<File> files = new LinkedList<>();
|
||||||
|
|
||||||
for (final File fileEntry : directory.listFiles()) {
|
for (final File fileEntry : directory.listFiles()) {
|
||||||
if (fileEntry.isDirectory()) {
|
if (fileEntry.isDirectory()) {
|
||||||
rectangles.addAll(loadFromDirectory(fileEntry));
|
files.addAll(loadFromDirectory(fileEntry));
|
||||||
} else {
|
} else {
|
||||||
if (loadFile(fileEntry) != null) {
|
files.add(fileEntry);
|
||||||
rectangles.add(loadFile(fileEntry));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return files;
|
||||||
return rectangles;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private PathRectangle loadFile(final File file) {
|
private PathRectangle loadFile(final File file) {
|
||||||
|
|||||||
@@ -5,20 +5,21 @@
|
|||||||
*/
|
*/
|
||||||
package xkcd.construction;
|
package xkcd.construction;
|
||||||
|
|
||||||
import xkcd.rectangle.imagepath.PathRectangle;
|
import java.awt.Color;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
|
import xkcd.rectangle.imagepath.PathRectangle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Hannes
|
* @author Hannes
|
||||||
*/
|
*/
|
||||||
public class ImageSticher {
|
public class ImageSticher {
|
||||||
private final double scaleTo = 0.1D;
|
private static final double DEFAULT_SCALE = 0.1D;
|
||||||
private static final int IMAGE_TYPE = BufferedImage.TYPE_4BYTE_ABGR;
|
private static final int IMAGE_TYPE = BufferedImage.TYPE_4BYTE_ABGR;
|
||||||
private static final Color BACKGROUND_COLOR = Color.BLACK;
|
private static final Color BACKGROUND_COLOR = Color.BLACK;
|
||||||
|
|
||||||
@@ -27,18 +28,21 @@ public class ImageSticher {
|
|||||||
|
|
||||||
public ImageSticher(final Collection<PathRectangle> rect) {
|
public ImageSticher(final Collection<PathRectangle> rect) {
|
||||||
this.rectangles = rect;
|
this.rectangles = rect;
|
||||||
//System.out.println("Count: " + rectangles.size());
|
|
||||||
this.initBufferedImage();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveImage(final File saveFile) throws IOException {
|
public void saveImage(final File saveFile) throws IOException {
|
||||||
initBufferedImage();
|
saveImage(saveFile, DEFAULT_SCALE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveImage(final File saveFile, final double scale) throws IOException {
|
||||||
|
initBufferedImage(scale);
|
||||||
for (final PathRectangle rectangle : rectangles) {
|
for (final PathRectangle rectangle : rectangles) {
|
||||||
try {
|
try {
|
||||||
final BufferedImage image = loadImage(rectangle.getPath());
|
final BufferedImage image = loadImage(rectangle.getPath());
|
||||||
// System.out.println(rectangle);
|
// System.out.println(rectangle);
|
||||||
collage.getGraphics().drawImage(image, (int) (rectangle.getX() * scaleTo), (int) (rectangle.getY() * scaleTo),
|
collage.getGraphics().drawImage(image, (int) (rectangle.getX() * scale),
|
||||||
(int) (rectangle.getWidth() * scaleTo), (int) (rectangle.getHeight() * scaleTo), null);
|
(int) (rectangle.getY() * scale), (int) (rectangle.getWidth() * scale),
|
||||||
|
(int) (rectangle.getHeight() * scale), null);
|
||||||
} catch (final IOException ex) {
|
} catch (final IOException ex) {
|
||||||
System.err.println(ex.getMessage());
|
System.err.println(ex.getMessage());
|
||||||
}
|
}
|
||||||
@@ -46,14 +50,13 @@ public class ImageSticher {
|
|||||||
ImageIO.write(collage, "png", saveFile);
|
ImageIO.write(collage, "png", saveFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initBufferedImage() {
|
private void initBufferedImage(final double scale) {
|
||||||
int width = 0;
|
int width = 0;
|
||||||
int height = 0;
|
int height = 0;
|
||||||
for (final PathRectangle rectangle : rectangles) {
|
for (final PathRectangle rectangle : rectangles) {
|
||||||
final int tmpWidth = (int) (rectangle.getWidth() + rectangle.getX());
|
final int tmpWidth = (int) (rectangle.getWidth() + rectangle.getX());
|
||||||
final int tmpHeight = (int) (rectangle.getHeight() + rectangle.getY());
|
final int tmpHeight = (int) (rectangle.getHeight() + rectangle.getY());
|
||||||
|
|
||||||
|
|
||||||
if (tmpWidth > width) {
|
if (tmpWidth > width) {
|
||||||
width = tmpWidth;
|
width = tmpWidth;
|
||||||
}
|
}
|
||||||
@@ -62,11 +65,13 @@ public class ImageSticher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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().setColor(BACKGROUND_COLOR);
|
||||||
//collage.getGraphics().drawRect(0, 0, collage.getWidth(), collage.getHeight());
|
// collage.getGraphics().drawRect(0, 0, collage.getWidth(),
|
||||||
//collage.getGraphics().fillRect(0, 0, collage.getWidth(), collage.getHeight());
|
// collage.getHeight());
|
||||||
|
// collage.getGraphics().fillRect(0, 0, collage.getWidth(),
|
||||||
|
// collage.getHeight());
|
||||||
}
|
}
|
||||||
|
|
||||||
private BufferedImage loadImage(final File filePath) throws IOException {
|
private BufferedImage loadImage(final File filePath) throws IOException {
|
||||||
|
|||||||
Reference in New Issue
Block a user