下载地址:https://www.pan38.com/share.php?code=MXPVK 提取码:8888 【仅供学习参考】
import java.util.Random;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DataGenerator {
private static final String[] NAMES = {"用户A","用户B","用户C"};
private static final String[] COMMENTS = {"真不错","点赞","666"};
public static String generateRandomComment() {
Random rand = new Random();
String name = NAMES[rand.nextInt(NAMES.length)];
String content = COMMENTS[rand.nextInt(COMMENTS.length)];
String time = LocalDateTime.now()
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
return String.format("%s:%s\n%s", name, content, time);
}
}
这个工具类可以生成随机的模拟评论数据,包含用户名、内容和时间戳。
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
public class ImageComposer {
public static void composeCommentImage(String comment, String outputPath)
throws Exception {
BufferedImage template = ImageIO.read(
new File("template.png"));
Graphics2D g = template.createGraphics();
g.setFont(new Font("微软雅黑", Font.PLAIN, 14));
g.setColor(Color.BLACK);
// 计算文字位置
int x = 50;
int y = 100;
for (String line : comment.split("\n")) {
g.drawString(line, x, y);
y += 20;
}
ImageIO.write(template, "png", new File(outputPath));
g.dispose();
}
}
该模块实现将文字内容绘制到图片模板上的功能,支持自定义字体和位置。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。