Also, ich wollte nur mal schnell was ausprobieren, nämlich eine Collage zu erstellen mit Bildern beliebiger Weite und Höhe, so dass die Collage die kleinsten Ausmaße hat… Dafür schaue ich jede Sekunde einmal im Clipboard nach, ob dort ein neues Image ist. Wenn ja, wird es der Collage hinzugefügt. Die Collage soll die Images in einem 15x15-Raster platzieren. Ein Platz hat dabei immer die Ausmaße 100x100 Pixel. Mir ist aufgefallen, dass die Collage für JEDES Image JEDES MAL alle 225 Möglichkeiten ausprobiert, lässt sich das nicht vereinfachen? Bei drei Images ist es langsam, bei vier Images ist es seeehr langsam und bei fünf Images hält es nicht mehr an. Vielen Dank fürs Lesen.
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import javax.imageio.ImageIO;
public class PlaceClipboardCollage {
public static Image getImageFromClipboard() throws Exception {
Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.imageFlavor)) {
return (Image) transferable.getTransferData(DataFlavor.imageFlavor);
} else {
return null;
}
}
public static class GridImage {
Image image;
int x, y;
int w, h;
}
public static boolean place(boolean[][] grid, int x, int y, int w, int h, boolean val) {
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
if (i + x >= grid.length || j + y >= grid.length || grid[i + x][j + y] == val) {
return false;
}
}
}
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
grid[i + x][j + y] = val;
}
}
return true;
}
public static ArrayList<GridImage> bestGridImages = null;
public static Comparator<ArrayList<GridImage>> comparator = new Comparator<ArrayList<GridImage>>() {
@Override
public int compare(ArrayList<GridImage> o1, ArrayList<GridImage> o2) {
int wmax1 = 0;
int hmax1 = 0;
int wmax2 = 0;
int hmax2 = 0;
for (GridImage gridImage : o1) {
if (gridImage.x + gridImage.w > wmax1) {
wmax1 = gridImage.x + gridImage.w;
}
if (gridImage.y + gridImage.h > hmax1) {
hmax1 = gridImage.y + gridImage.h;
}
}
for (GridImage gridImage : o2) {
if (gridImage.x + gridImage.w > wmax2) {
wmax2 = gridImage.x + gridImage.w;
}
if (gridImage.y + gridImage.h > hmax2) {
hmax2 = gridImage.y + gridImage.h;
}
}
return Math.max(wmax1, hmax1) - Math.max(wmax2, hmax2);
}
};
public static void placeAll(ArrayList<Image> images, ArrayList<GridImage> gridImages, boolean[][] grid, int i) {
if (i == images.size()) {
if (bestGridImages == null || comparator.compare(gridImages, bestGridImages) < 0) {
bestGridImages = new ArrayList<>(gridImages);
}
return;
}
Image image = images.get(i);
int w = (int) Math.ceil(image.getWidth(null) / 100.0);
int h = (int) Math.ceil(image.getHeight(null) / 100.0);
for (int x = 0; x < grid.length; x++) {
for (int y = 0; y < grid.length; y++) {
if (place(grid, x, y, w, h, true)) {
GridImage gi = new GridImage();
gi.image = image;
gi.x = x;
gi.y = y;
gi.w = w;
gi.h = h;
gridImages.add(gi);
placeAll(images, gridImages, grid, i + 1);
gridImages.remove(gridImages.size() - 1);
place(grid, x, y, w, h, false);
}
}
}
}
public static void saveImages(ArrayList<Image> images) {
placeAll(images, new ArrayList<>(), new boolean[15][15], 0);
// Create a buffered image with transparency
BufferedImage bimage = new BufferedImage(15 * 100, 15 * 100, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bimage.createGraphics();
for (GridImage gridImage : bestGridImages) {
g2d.drawImage(gridImage.image, gridImage.x * 100, gridImage.y * 100, null);
}
g2d.dispose();
bestGridImages = null;
try {
ImageIO.write(bimage, "PNG", new File("test-image-" + System.currentTimeMillis() + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
ArrayList<Image> images = new ArrayList<>();
while (true) {
try {
Image imageFromClipboard = getImageFromClipboard();
if (imageFromClipboard != null) {
System.out.println("Found image.");
images.add(imageFromClipboard);
StringSelection stringSelection = new StringSelection("");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
saveImages(images);
}
} catch (Exception e) {
}
Thread.sleep(1000);
}
}
}