Generating Dynamic Image Posters in Java
Creating dynamic images in Java can be a fun and rewarding process, whether you're looking to create graphics for a web application, generate posters for articles, or simply explore your creative side. In this guide, we'll walk through the proces...

public static String downloadImageAsBase64(String imgSrc) {
try {
URL url = new URL(imgSrc);
BufferedImage image = ImageIO.read(url);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
byte[] bytes = baos.toByteArray();
return Base64.getEncoder().encodeToString(bytes);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static String createArticlePoster(String title, String description, String imgSrc, String tags) {
String base64Image = downloadImageAsBase64(imgSrc);
String logoBase64 = "iVBORw0KGgoAAAANSUhE...";
try {
// Decode Base64 images
byte[] imageBytes = Base64.getDecoder().decode(base64Image);
byte[] logoImageBytes = Base64.getDecoder().decode(logoBase64);
// Create input streams from byte arrays
BufferedImage baseImage = ImageIO.read(new ByteArrayInputStream(imageBytes));
BufferedImage baseImageLogo = ImageIO.read(new ByteArrayInputStream(logoImageBytes));
// Create combined image with padding
int width = baseImage.getWidth() + 40;
int height = baseImage.getHeight() + 450;
BufferedImage combinedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = combinedImage.createGraphics();
// Set background and draw elements
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, width, height);
g2d.drawImage(baseImageLogo, 20, 20, null);
g2d.setColor(Color.BLACK);
g2d.setFont(new Font("Arial", Font.BOLD, 30));
g2d.drawString("Your Logo Here", 100, 55);
g2d.setFont(new Font("Arial", Font.ITALIC, 16));
g2d.drawString("Your Slogan Here", 100, 75);
g2d.drawImage(baseImage, 20, 100, null);
drawWrappedText(g2d, 20, baseImage.getHeight() + 150, 40, title,
Color.decode("#52ace2"), new Font("Arial", Font.BOLD, 35), baseImage.getWidth());
String formatDescription = description.length() > 460 ? description.substring(0, 460) + "..." : description;
drawWrappedText(g2d, 20, baseImage.getHeight() + 230, 23, formatDescription,
Color.BLACK, new Font("Arial", Font.ITALIC, 20), baseImage.getWidth());
// Draw tags
g2d.setColor(Color.decode("#ff9b26"));
g2d.setFont(new Font("Arial", Font.PLAIN, 18));
g2d.drawString(tags, 20, baseImage.getHeight() + 430);
g2d.dispose();
// Save the final image
String filePath = "/poster-" + RandomUtil.generateRandomString(5) + "-" + System.currentTimeMillis() + ".png";
ImageIO.write(combinedImage, "png", new File(filePath));
return filePath;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void drawWrappedText(Graphics2D g2d, int startX, int startY, int lineHeight, String text, Color color, Font font, int maxWidth) {
g2d.setColor(color);
g2d.setFont(font);
String[] words = text.split(" ");
StringBuilder currentLine = new StringBuilder();
for (String word : words) {
String testLine = currentLine.toString() + word + " ";
FontMetrics fm = g2d.getFontMetrics();
int lineWidth = fm.stringWidth(testLine);
// If the line exceeds max width, draw the current line and reset
if (lineWidth > maxWidth) {
g2d.drawString(currentLine.toString(), startX, startY);
currentLine = new StringBuilder(word + " ");
startY += lineHeight;
} else {
currentLine.append(word).append(" ");
}
}
// Draw the last line if it exists
if (currentLine.length() > 0) {
g2d.drawString(currentLine.toString(), startX, startY);
}
}
public static void main(String[] args) {
String title = "Exploring Java Image Generation";
String description = "This article provides a comprehensive guide to generating images in Java.";
String imgSrc = "https://example.com/sample-image.png"; // Use a valid image URL
String tags = "#Java #Image #Tutorial";
String imagePath = createArticlePoster(title, description, imgSrc, tags);
System.out.println("Generated image at: " + imagePath);
}
Read more at : Generating Dynamic Image Posters in Java
About the author
I am Tuanh.net. As of 2024, I have accumulated 8 years of experience in backend programming. I am delighted to connect and share my knowledge with everyone.
Comments
Join the discussionNo comments yet. Be the first to comment.





