Skip to content

Commit aa5c436

Browse files
committed
Compositing done
1 parent 797d3b7 commit aa5c436

File tree

6 files changed

+106
-40
lines changed

6 files changed

+106
-40
lines changed

tweetprint/src/main/java/org/riskfirst/tweetprint/builder/CardType.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package org.riskfirst.tweetprint.builder;
22

3-
import static org.riskfirst.tweetprint.builder.compositing.CompositeFunction.POSTCARD_CF;
4-
5-
import org.riskfirst.tweetprint.builder.compositing.CompositeFunction;
3+
import org.riskfirst.tweetprint.image.CompositeFunction;
4+
import org.riskfirst.tweetprint.image.compositing.GreetingsCardCompositeFunction;
5+
import org.riskfirst.tweetprint.image.compositing.PostCardCompositeFunction;
66

77
public enum CardType {
88

99

10-
POSTCARD("CLASSIC-POST-GLOS-6X4", 6f/4f, "noun-postcard-1130482.svg", "Post Card", 3588 / 2, 1287, POSTCARD_CF),
11-
GREETINGSCARD("GLOBAL-GRE-MOH-7X5-DIR", 7f/5f, "noun-greeting-card-4384989.svg", "Greetings Card", 2161, 6118/4, null);
10+
POSTCARD("CLASSIC-POST-GLOS-6X4", 6f/4f, "noun-postcard-1130482.svg", "Post Card", 3588 / 2, 1287, PostCardCompositeFunction.INSTANCE),
11+
GREETINGSCARD("GLOBAL-GRE-MOH-7X5-DIR", 7f/5f, "noun-greeting-card-4384989.svg", "Greetings Card", 2161, 6118/4, GreetingsCardCompositeFunction.INSTANCE);
1212

1313
public final String sku;
1414
public final float ratio;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.riskfirst.tweetprint.image;
2+
3+
import java.awt.Color;
4+
import java.awt.Graphics2D;
5+
import java.awt.geom.AffineTransform;
6+
import java.awt.image.AffineTransformOp;
7+
import java.awt.image.BufferedImage;
8+
import java.awt.image.BufferedImageOp;
9+
import java.awt.image.RenderedImage;
10+
import java.io.ByteArrayInputStream;
11+
12+
import javax.imageio.ImageIO;
13+
14+
import org.riskfirst.tweetprint.builder.Arrangement;
15+
import org.riskfirst.tweetprint.builder.CardType;
16+
import org.riskfirst.tweetprint.builder.OrderDetails;
17+
18+
@FunctionalInterface
19+
public interface CompositeFunction {
20+
21+
public RenderedImage performCompositing(OrderDetails od, ImageBuilder ib);
22+
23+
24+
}

tweetprint/src/main/java/org/riskfirst/tweetprint/builder/compositing/CompositeFunction.java renamed to tweetprint/src/main/java/org/riskfirst/tweetprint/image/compositing/AbstractCompositeFunction.java

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
package org.riskfirst.tweetprint.builder.compositing;
1+
package org.riskfirst.tweetprint.image.compositing;
22

3-
import java.awt.Color;
4-
import java.awt.Graphics2D;
53
import java.awt.geom.AffineTransform;
64
import java.awt.image.AffineTransformOp;
75
import java.awt.image.BufferedImage;
@@ -14,20 +12,34 @@
1412
import org.riskfirst.tweetprint.builder.Arrangement;
1513
import org.riskfirst.tweetprint.builder.CardType;
1614
import org.riskfirst.tweetprint.builder.OrderDetails;
15+
import org.riskfirst.tweetprint.image.CompositeFunction;
1716
import org.riskfirst.tweetprint.image.ImageBuilder;
1817

19-
@FunctionalInterface
20-
public interface CompositeFunction {
18+
public abstract class AbstractCompositeFunction implements CompositeFunction {
2119

22-
public RenderedImage performCompositing(OrderDetails od, ImageBuilder ib);
20+
public static BufferedImageOp getRotateOp(boolean rotate, BufferedImage originalImage) {
21+
if (rotate) {
22+
AffineTransform tx = new AffineTransform();
2323

24-
public static CompositeFunction POSTCARD_CF = (od, ib) -> {
24+
// last, width = height and height = width :)
25+
tx.translate(originalImage.getHeight() / 2,originalImage.getWidth() / 2);
26+
tx.rotate(Math.PI / 2);
27+
// first - center image at the origin so rotate works OK
28+
tx.translate(-originalImage.getWidth() / 2,-originalImage.getHeight() / 2);
29+
30+
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
31+
return op;
32+
} else {
33+
return null;
34+
}
35+
}
36+
37+
@Override
38+
public RenderedImage performCompositing(OrderDetails od, ImageBuilder ib) {
2539
try {
2640
float panelWidth = CardType.POSTCARD.width;
2741
float panelHeight = CardType.POSTCARD.height;
28-
29-
BufferedImage out = new BufferedImage((int) (panelWidth * 2), (int) (panelHeight), BufferedImage.TYPE_INT_RGB);
30-
42+
3143
float width , height;
3244
if (od.arrangement == Arrangement.PORTRAIT) {
3345
width = panelHeight;
@@ -43,35 +55,15 @@ public interface CompositeFunction {
4355
BufferedImage tweetImage = ImageIO.read(new ByteArrayInputStream(tweetImageBytes));
4456
BufferedImage messageImage = ImageIO.read(new ByteArrayInputStream(messageImageBytes));
4557

46-
Graphics2D g2 = (Graphics2D) out.getGraphics();
47-
g2.setBackground(Color.WHITE);
48-
g2.fillRect(0, 0, out.getWidth(), out.getHeight());
49-
g2.drawImage(messageImage, 0, 0, null);
50-
BufferedImageOp op = getOp(od.arrangement, tweetImage);
51-
g2.drawImage(tweetImage, op, (int) panelWidth, 0);
58+
BufferedImage out = compositeAllImages(od, panelWidth, panelHeight, tweetImage, messageImage);
5259

5360
return out;
5461

5562
} catch (Exception e) {
5663
throw new RuntimeException("Couldn't produce composite image", e);
5764
}
58-
};
59-
60-
public static BufferedImageOp getOp(Arrangement arrangement, BufferedImage originalImage) {
61-
if (arrangement == Arrangement.PORTRAIT) {
62-
AffineTransform tx = new AffineTransform();
63-
64-
// last, width = height and height = width :)
65-
tx.translate(originalImage.getHeight() / 2,originalImage.getWidth() / 2);
66-
tx.rotate(Math.PI / 2);
67-
// first - center image at the origin so rotate works OK
68-
tx.translate(-originalImage.getWidth() / 2,-originalImage.getHeight() / 2);
69-
70-
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
71-
return op;
72-
} else {
73-
return null;
74-
}
7565
}
76-
66+
67+
protected abstract BufferedImage compositeAllImages(OrderDetails od, float panelWidth, float panelHeight, BufferedImage tweetImage, BufferedImage messageImage);
68+
7769
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.riskfirst.tweetprint.image.compositing;
2+
3+
import java.awt.Color;
4+
import java.awt.Graphics2D;
5+
import java.awt.image.BufferedImage;
6+
7+
import org.riskfirst.tweetprint.builder.Arrangement;
8+
import org.riskfirst.tweetprint.builder.OrderDetails;
9+
10+
public class GreetingsCardCompositeFunction extends AbstractCompositeFunction {
11+
12+
public static final GreetingsCardCompositeFunction INSTANCE = new GreetingsCardCompositeFunction();
13+
14+
protected BufferedImage compositeAllImages(OrderDetails od, float panelWidth, float panelHeight, BufferedImage tweetImage, BufferedImage messageImage) {
15+
BufferedImage out = new BufferedImage((int) (panelHeight * 4), (int) (panelWidth), BufferedImage.TYPE_INT_RGB);
16+
Graphics2D g2 = (Graphics2D) out.getGraphics();
17+
g2.setBackground(Color.WHITE);
18+
g2.fillRect(0, 0, out.getWidth(), out.getHeight());
19+
g2.drawImage(messageImage, getRotateOp(true, messageImage), (int) (3*panelHeight), 0);
20+
g2.drawImage(tweetImage, getRotateOp(od.arrangement == Arrangement.LANDSCAPE, out), (int) panelHeight, 0);
21+
return out;
22+
};
23+
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.riskfirst.tweetprint.image.compositing;
2+
3+
import java.awt.Color;
4+
import java.awt.Graphics2D;
5+
import java.awt.image.BufferedImage;
6+
import java.awt.image.BufferedImageOp;
7+
8+
import org.riskfirst.tweetprint.builder.Arrangement;
9+
import org.riskfirst.tweetprint.builder.OrderDetails;
10+
11+
public class PostCardCompositeFunction extends AbstractCompositeFunction {
12+
13+
public static final PostCardCompositeFunction INSTANCE = new PostCardCompositeFunction();
14+
15+
protected BufferedImage compositeAllImages(OrderDetails od, float panelWidth, float panelHeight, BufferedImage tweetImage, BufferedImage messageImage) {
16+
BufferedImage out = new BufferedImage((int) (panelWidth * 2), (int) (panelHeight), BufferedImage.TYPE_INT_RGB);
17+
Graphics2D g2 = (Graphics2D) out.getGraphics();
18+
g2.setBackground(Color.WHITE);
19+
g2.fillRect(0, 0, out.getWidth(), out.getHeight());
20+
g2.drawImage(messageImage, 0, 0, null);
21+
BufferedImageOp op = getRotateOp(od.arrangement == Arrangement.PORTRAIT, tweetImage);
22+
g2.drawImage(tweetImage, op, (int) panelWidth, 0);
23+
return out;
24+
};;
25+
26+
}

tweetprint/src/main/resources/templates/builder/preview.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<div class="wf-section">
1616
<div class="w-container">
1717
<div class="w-form">
18-
<form id="wf-form-layout" name="wf-form-layout" method="get" action="@{${'/confirm/'+data}}">
18+
<form id="wf-form-layout" name="wf-form-layout" method=post action="@{${'/confirm/'+data}}">
1919

2020
<input type="hidden" id="tweetId" name="tweetId" th:value="${order.tweetId}" />
2121

0 commit comments

Comments
 (0)