102 lines
3.4 KiB
Java
102 lines
3.4 KiB
Java
package tech.nevets.vcardgen;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.JsonObject;
|
|
|
|
import javax.imageio.ImageIO;
|
|
import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
|
|
import static spark.Spark.*;
|
|
|
|
public class Main {
|
|
|
|
private static final Color WHITE = new Color(255, 255, 255);
|
|
private static final Color GREEN = new Color(101, 142, 61);
|
|
|
|
private static final Font ARIAL65 = new Font("Arial", Font.PLAIN, 65);
|
|
private static final Font ARIAL45 = new Font("Arial", Font.PLAIN, 45);
|
|
private static final Font ARIAL45I = new Font("Arial", Font.ITALIC, 45);
|
|
private static final Font ARIAL44 = new Font("Arial", Font.ITALIC, 44);
|
|
private static final Font ARIAL38 = new Font("Arial", Font.ITALIC, 38);
|
|
|
|
public static void main(String[] args) {
|
|
|
|
port(8080);
|
|
post("/", (req, res) -> {
|
|
res.type("image/png");
|
|
JsonObject data = new Gson().fromJson(req.body(), JsonObject.class);
|
|
return generate(data.get("name").getAsString(), data.get("title").getAsString(), data.get("email").getAsString(), data.get("location").getAsString(), data.get("address").getAsString(), data.get("schoolNumber").getAsString(), data.get("extension").getAsString(), data.get("hasExtension").getAsBoolean(), data.get("cellNumber").getAsString(), data.get("hasCell").getAsBoolean());
|
|
});
|
|
|
|
}
|
|
|
|
private static byte[] generate(String name, String title, String email, String location, String address, String schoolNumber, String extension, boolean hasExtension, String cellNumber, boolean hasCell) {
|
|
BufferedImage finalImage = new BufferedImage(1080, 602, BufferedImage.TYPE_INT_ARGB);
|
|
BufferedImage background;
|
|
|
|
try {
|
|
background = ImageIO.read(Main.class.getResource("/background.png"));
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
|
|
Graphics2D g = finalImage.createGraphics();
|
|
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
|
|
|
g.drawImage(background, 0, 0, null);
|
|
|
|
g.setColor(WHITE);
|
|
g.setFont(ARIAL65);
|
|
g.drawString(name, 85, 112);
|
|
|
|
g.setFont(ARIAL45I);
|
|
g.drawString(title, 91, 176);
|
|
|
|
int yOffset = 0;
|
|
if (hasExtension && hasCell) {
|
|
yOffset = 40;
|
|
}
|
|
|
|
g.setFont(ARIAL45);
|
|
g.drawString(email, 62, 380 - yOffset);
|
|
|
|
g.setColor(GREEN);
|
|
g.setFont(ARIAL44);
|
|
g.drawString(location, 59, 447 - yOffset);
|
|
|
|
g.setFont(ARIAL38);
|
|
g.drawString(address, 59, 491 - yOffset);
|
|
|
|
String number = "";
|
|
if (hasExtension && !hasCell) {
|
|
number = "W: " + schoolNumber + " x" + extension;
|
|
} else if (!hasExtension && hasCell) {
|
|
number = "C: " + cellNumber;
|
|
}
|
|
|
|
if (!(hasExtension && hasCell)) {
|
|
g.drawString(number, 59, 536);
|
|
} else {
|
|
g.drawString("W: " + schoolNumber + " x" + extension, 59, 496);
|
|
g.drawString("C: " + cellNumber, 59, 540);
|
|
}
|
|
|
|
g.dispose();
|
|
|
|
ByteArrayOutputStream baos;
|
|
try {
|
|
baos = new ByteArrayOutputStream();
|
|
ImageIO.write(finalImage, "png", baos);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
|
|
return baos.toByteArray();
|
|
}
|
|
}
|