Skip to content

Commit

Permalink
Fixed centering, clipping issues still prominant
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamDman committed Nov 22, 2016
1 parent 7596e8d commit 2601264
Showing 1 changed file with 128 additions and 116 deletions.
244 changes: 128 additions & 116 deletions src/PhotoLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,120 +8,132 @@
import java.util.Arrays;

public class PhotoLayout {
private static ArrayList<File> images = new ArrayList<>();
private static File path_save;

public static FormPrompt inst_prompt;
public static JFrame form_prompt;

public static File getSavePath() {
return path_save;
}

public static void setSavePath(File path_save) {
PhotoLayout.path_save = path_save;
}

public static ArrayList<File> getImages() {
return images;
}

public static void addImages(File[] toAdd) {
images.addAll(Arrays.asList(toAdd));
}

public static void clearImages() {
images.clear();
}

public static void main(String[] args) {
FormPrompt.main(args);
}

public static void beginParse() {
if (path_save == null) {
JPanel gui = new JPanel(new BorderLayout(3, 3));
JLabel label = new JLabel();
label.setText("You did not set a save path!");
gui.add(label);
JOptionPane.showMessageDialog(null, gui);
} else {
stitchImages();
}
}

private static void stitchImages() {
Thread thread = new Thread() {
@Override
public void run() {
for (int index = 0; index < images.size() - 1; index += 2) {
ImagePair imagePair = new ImagePair(images.get(index), images.get(index + 1));
try {
BufferedImage imgA = ImageIO.read(imagePair.fileA);
BufferedImage imgB = ImageIO.read(imagePair.fileB);

BufferedImage imgBuffer = new BufferedImage(1200, 1800, BufferedImage.TYPE_INT_RGB);

AffineTransform topShift = buildTransform(imgA);
AffineTransform bottomShift = buildTransform(imgB);

Graphics2D graphics = imgBuffer.createGraphics();
graphics.setBackground(Color.WHITE);
graphics.drawImage(imgA, topShift, null);
graphics.translate(0,900);
graphics.drawImage(imgB, bottomShift, null);
graphics.dispose();

String name = imagePair.fileA.getName().substring(0, 5) + imagePair.fileB.getName().substring(0, 5);
File output = new File(PhotoLayout.getSavePath().getAbsoluteFile() + File.separator + name + ".jpg");

ImageIO.write(imgBuffer, "JPEG", output);


if (index > 0) {
int prog = index * 100 / images.size();
inst_prompt.updateProgress(prog);
}
} catch (Exception e) {
e.printStackTrace();
}
}
inst_prompt.updateProgress(0);
}
};
thread.start();

}

private static AffineTransform buildTransform(BufferedImage image) {
AffineTransform trans = new AffineTransform();
if (image.getHeight() > image.getWidth()) {
trans.setToScale(1.0 / (image.getHeight() / 1200.0), 1.0 / (image.getWidth() / 900.0));
trans.translate(image.getHeight() / 2, image.getWidth() / 2);
trans.rotate(Math.PI / 2);
trans.translate(-image.getWidth() / 2, -image.getHeight() / 2);
} else {
trans.setToScale(1200.0 / image.getWidth(),900.0 / image.getHeight());
}
return trans;
}

private static int lesserOf(int a, int b) {
return a < b ? a : b;
}

private static int greaterOf(int a, int b) {
return a > b ? a : b;
}

private static class ImagePair {
private final File fileA;
private final File fileB;

public ImagePair(File fileA, File fileB) {
this.fileA = fileA;
this.fileB = fileB;
}

}
private static ArrayList<File> images = new ArrayList<>();
private static File path_save;

public static FormPrompt inst_prompt;
public static JFrame form_prompt;

public static File getSavePath() {
return path_save;
}

public static void setSavePath(File path_save) {
PhotoLayout.path_save = path_save;
}

public static ArrayList<File> getImages() {
return images;
}

public static void addImages(File[] toAdd) {
images.addAll(Arrays.asList(toAdd));
}

public static void clearImages() {
images.clear();
}

public static void main(String[] args) {
FormPrompt.main(args);
}

public static void beginParse() {
if (path_save == null) {
JPanel gui = new JPanel(new BorderLayout(3, 3));
JLabel label = new JLabel();
label.setText("You did not set a save path!");
gui.add(label);
JOptionPane.showMessageDialog(null, gui);
} else {
stitchImages();
}
}

private static void stitchImages() {
Thread thread = new Thread() {
@Override
public void run() {
for (int index = 0; index < images.size() - 1; index += 2) {
ImagePair imagePair = new ImagePair(images.get(index), images.get(index + 1));
try {
BufferedImage imgA = ImageIO.read(imagePair.fileA);
BufferedImage imgB = ImageIO.read(imagePair.fileB);

BufferedImage imgBuffer = new BufferedImage(1200, 1800, BufferedImage.TYPE_INT_RGB);

Graphics2D graphics = imgBuffer.createGraphics();
graphics.setBackground(Color.WHITE);

drawCentered(graphics, imgA);
graphics.translate(0, 900);
drawCentered(graphics, imgB);
graphics.dispose();

String name = imagePair.fileA.getName().substring(0, 5) + imagePair.fileB.getName().substring(0, 5);
File output = new File(PhotoLayout.getSavePath().getAbsoluteFile() + File.separator + name + ".jpg");

ImageIO.write(imgBuffer, "JPEG", output);


if (index > 0) {
int prog = index * 100 / images.size();
inst_prompt.updateProgress(prog);
}
} catch (Exception e) {
e.printStackTrace();
}
}
inst_prompt.updateProgress(0);
}
};
thread.start();
}

private static void drawCentered(Graphics2D graphics, BufferedImage image) {
AffineTransform trans = new AffineTransform();
int width = image.getWidth();
int height = image.getHeight();
double scale;
if (height > width) {
scale = getScale(height, width);
trans.setToScale(scale, scale);
trans.translate(height / 2, width / 2);
trans.rotate(Math.PI / 2);
trans.translate(-image.getWidth() / 2, -height / 2);
int temp = width;
width = height;
height = temp;
} else {
scale = getScale(image.getWidth(), height);
trans.setToScale(scale, scale);
}

width = (int) (width * scale);
height = (int) (height * scale);
int dx = (width - 1200) / 2;
int dy = (height - 900) / 2;
graphics.translate(-dx, -dy);
graphics.drawImage(image, trans, null);
graphics.translate(dx, -dy);
}

private static double getScale(int width, int height) {
double scale;
scale = 1200.0 / width;
if (height * scale < 900.0)
scale = 900.0 / height;
return scale;
}

private static class ImagePair {
private final File fileA;
private final File fileB;

public ImagePair(File fileA, File fileB) {
this.fileA = fileA;
this.fileB = fileB;
}

}
}

0 comments on commit 2601264

Please sign in to comment.