Added background color option, error handling
This commit is contained in:
parent
bfc8667b4e
commit
80dff3eeb3
@ -12,14 +12,30 @@ import java.io.InputStream;
|
||||
|
||||
public class GenerateRoute implements Route {
|
||||
@Override
|
||||
public Object handle(Request request, Response response) throws Exception {
|
||||
request.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement("./temp"));
|
||||
try (InputStream is = request.raw().getPart("image").getInputStream()) {
|
||||
response.type("image/png");
|
||||
public Object handle(Request req, Response res) throws Exception {
|
||||
req.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement("./temp"));
|
||||
try (InputStream is = req.raw().getPart("image").getInputStream()) {
|
||||
BufferedImage img = ImageIO.read(is);
|
||||
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ImageIO.write(Images.placeImageOnTop(img), "png", baos);
|
||||
|
||||
int color;
|
||||
boolean customDimensions;
|
||||
int width;
|
||||
int height;
|
||||
try {
|
||||
color = Integer.parseInt(req.queryParams("color"));
|
||||
customDimensions = Boolean.parseBoolean(req.queryParams("customDimensions"));
|
||||
width = Integer.parseInt(req.queryParams("width"));
|
||||
height = Integer.parseInt(req.queryParams("height"));
|
||||
} catch (final Exception e) {
|
||||
res.type("plain/text");
|
||||
res.status(400);
|
||||
return "Error in sent parameters: " + e.getMessage();
|
||||
}
|
||||
|
||||
ImageIO.write(Images.renderImage(img, color, customDimensions, width, height), "png", baos);
|
||||
|
||||
res.type("image/png");
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
||||
|
@ -4,38 +4,44 @@ import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class Images {
|
||||
private static final BufferedImage BACKGROUND = blankWhiteBackground();
|
||||
public static BufferedImage renderImage(BufferedImage img, int color, boolean customDimensions, int customWidth, int customHeight) {
|
||||
BufferedImage image = generateBackground(color);
|
||||
Graphics2D g = image.createGraphics();
|
||||
|
||||
public static BufferedImage placeImageOnTop(BufferedImage fgImage) {
|
||||
BufferedImage image = BACKGROUND;
|
||||
Graphics2D imageG2D = image.createGraphics();
|
||||
|
||||
int width = fgImage.getWidth();
|
||||
int height = fgImage.getHeight();
|
||||
int newWidth;
|
||||
int newHeight;
|
||||
int width;
|
||||
int height;
|
||||
int xPos;
|
||||
int yPos;
|
||||
|
||||
if (width > height) {
|
||||
newWidth = 1048;
|
||||
newHeight = (newWidth * height) / width;
|
||||
xPos = 16;
|
||||
yPos = (1080 - newHeight) / 2;
|
||||
} else if (height > width) {
|
||||
newHeight = 1048;
|
||||
newWidth = (width * newHeight) / height;
|
||||
xPos = (1080 - newWidth) / 2;
|
||||
yPos = 16;
|
||||
if (customDimensions) {
|
||||
width = customWidth;
|
||||
height = customHeight;
|
||||
xPos = 0;
|
||||
yPos = 0;
|
||||
} else {
|
||||
newWidth = 1048;
|
||||
newHeight = 1048;
|
||||
xPos = 16;
|
||||
yPos = 16;
|
||||
int oldWidth = img.getWidth();
|
||||
int oldHeight = img.getHeight();
|
||||
|
||||
if (oldWidth > oldHeight) {
|
||||
width = 1048;
|
||||
height = (width * oldHeight) / oldWidth;
|
||||
xPos = 16;
|
||||
yPos = (1080 - height) / 2;
|
||||
} else if (oldHeight > oldWidth) {
|
||||
height = 1048;
|
||||
width = (oldWidth * height) / oldHeight;
|
||||
xPos = (1080 - width) / 2;
|
||||
yPos = 16;
|
||||
} else {
|
||||
width = 1048;
|
||||
height = 1048;
|
||||
xPos = 16;
|
||||
yPos = 16;
|
||||
}
|
||||
}
|
||||
|
||||
imageG2D.drawImage(resizeImage(fgImage, newWidth, newHeight), xPos, yPos, newWidth, newHeight, null);
|
||||
imageG2D.dispose();
|
||||
g.drawImage(resizeImage(img, width, height), xPos, yPos, width, height, null);
|
||||
g.dispose();
|
||||
|
||||
return image;
|
||||
}
|
||||
@ -50,11 +56,11 @@ public class Images {
|
||||
return resizedImage;
|
||||
}
|
||||
|
||||
public static BufferedImage blankWhiteBackground() {
|
||||
public static BufferedImage generateBackground(int color) {
|
||||
BufferedImage image = new BufferedImage(1080, 1080, BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics2D g2d = image.createGraphics();
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.setPaint(Color.WHITE);
|
||||
g2d.setColor(new Color(color));
|
||||
g2d.setPaint(new Color(color));
|
||||
g2d.fillRect(0, 0, 1080, 1080);
|
||||
g2d.dispose();
|
||||
return image;
|
||||
|
Loading…
Reference in New Issue
Block a user