package java456.com.utils;
import com.alibaba.fastjson2.JSONObject;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Date;
public class HaiBaoUtil {
public static void main(String[] s){
try {
System.out.println("系统编码: " + System.getProperty("file.encoding"));
System.out.println("默认字体: " + new Font(null).getFontName());
String uploadFile2 = "/static/xcx_haibao/123456/";
String fileName = DateUtil.formatDate(new Date(), "yyyyMMddHHmmssSSS") + ".png";
JSONObject param = new JSONObject();
String webPath = "D:\\idea_space\\xcx_api_lvyou2\\src\\main\\webapp\\";
param.put("img1", webPath+"/static/upload_image/blog_cover/20250624/20250624004044.jpg");
param.put("img2",webPath+"/static/upload_image/blog_cover/20250624/20250624004044.jpg");
param.put("font", "D:\\idea_space\\xcx_api_lvyou2\\src\\main\\resources\\font\\msyh.ttc");
FileUtil.makeDirs(webPath + uploadFile2);
HaiBaoUtil.create(param,webPath + uploadFile2+ fileName);
// create();
}catch (Exception e){
System.out.println(e);
}
}
/**
* 创建一个海报 保存到指定位置。
*/
public static void create (JSONObject param, String outputPath) throws Exception{
// 创建海报画布
int width = 450;
int height = 800;
BufferedImage poster = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = poster.createGraphics();
// 设置背景色为白色
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, width, height);
// 设置文字属性
// int fontSize = 15;
// Font font = new Font("Arial", Font.PLAIN, fontSize);
// graphics.setFont(font);
Font font = Font.createFont(
Font.TRUETYPE_FONT,
new File(param.getString("font")) // 微软雅黑字体
).deriveFont(24f); // 设置字号
graphics.setFont(font); // 应用字体
String fontColor = "#000000";
graphics.setColor(Color.decode(fontColor));
// 计算文字位置
FontMetrics metrics = graphics.getFontMetrics();
int textHeight = metrics.getHeight();
// 绘制第一行文字
String textLine1 ="小米商城";
int textWidth = metrics.stringWidth(textLine1);
int x1 = 100;
int y1 = 50; // 第一行文字起始Y坐标
graphics.drawString(textLine1, x1, y1);
// 绘制第二行文字
font = Font.createFont(
Font.TRUETYPE_FONT,
new File(param.getString("font")) // 微软雅黑字体
).deriveFont(15f); // 设置字号
graphics.setFont(font); // 应用字体
fontColor = "#424242";
graphics.setColor(Color.decode(fontColor));
String textLine2="第二行文字Y坐标";
int x2 =100;
int y2 =80;
graphics.drawString(textLine2, x2, y2);
// 加载并绘制第一张图片
double image1Scale = 1;
//背景透明的图片 画到上面也是透明的 不管是什么格式 jpg 和 png 都是一样效果
String image1Url = param.getString("img1") ;
BufferedImage img1 = ImageIO.read(new File(image1Url));
int img1Width = 60;
int img1Height = 60;
int img1X = 25;
int img1Y = 25;
int cornerRadius = 10; // 圆角半径,可调整
// 使用圆角绘制方法
RoundImageUtil.drawRoundImage(graphics, img1, img1X, img1Y,
img1Width, img1Height, cornerRadius);
//普通绘制 方形
//graphics.drawImage(img1, img1X, img1Y, img1Width, img1Height, null);
// 加载并绘制第二张图片
double image2Scale = 1;
String image2Url = param.getString("img2") ;
BufferedImage img2 = ImageIO.read(new File(image2Url));
int img2Width = 400;
int img2Height = 400;
int img2X = 25;
int img2Y = 125;
// 使用圆角绘制方法
RoundImageUtil.drawRoundImage(graphics, img2, img2X, img2Y,
img2Width, img2Height, cornerRadius);
//普通绘制 方形
//graphics.drawImage(img2, img2X, img2Y, img2Width, img2Height, null);
// 保存海报
File outputFile = new File(outputPath);
//String outputPath = "21111.png"; //保存工程 下面 和 src target 同级别
ImageIO.write(poster, "png", new File(outputPath));
graphics.dispose();
}
}package java456.com.utils;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class RoundImageUtil {
// 生成圆角图片
public static BufferedImage makeRoundedCorner(BufferedImage image, int cornerRadius) {
int width = image.getWidth();
int height = image.getHeight();
BufferedImage output = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = output.createGraphics();
// 启用抗锯齿
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 创建圆角矩形裁剪区域
RoundRectangle2D round = new RoundRectangle2D.Float(0, 0, width, height, cornerRadius, cornerRadius);
g2.setClip(round);
// 绘制原始图片
g2.drawImage(image, 0, 0, null);
g2.dispose();
return output;
}
// 在指定位置绘制圆角图片
public static void drawRoundImage(Graphics2D graphics, BufferedImage image,
int x, int y, int width, int height, int cornerRadius) {
// 缩放图片到目标尺寸
BufferedImage resized = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = resized.createGraphics();
g.drawImage(image.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
g.dispose();
// 应用圆角效果
BufferedImage rounded = makeRoundedCorner(resized, cornerRadius);
graphics.drawImage(rounded, x, y, null);
}
} /**
* /admin/blog/add
*/
@ResponseBody
@RequestMapping("/add")
public JSONObject add(Blog blog , HttpServletResponse response, HttpServletRequest request, HttpSession session) throws Exception {
JSONObject result = new JSONObject();
String webPath = request.getServletContext().getRealPath("");
blog.setCreateDateTime(new Date());
blog.setState(0);//0未审校 1已审核
blog.setClickHit(0);//查看 次数
blog.insert();
// //生成一个小程序二维码
// String xcx_qr_path = "pages/blog_view/blog_view";
// Integer xcx_qr_width = 80;
// String scene = blog.getId()+"__123";//如果推广的话 后的参数 应该是动态的。 不知道小程序 二维码 一个能创建几个。
// JSONObject token = (JSONObject) servletContext.getAttribute("token");
// String base64String = WeiXinBufferToImage.bufferToBase64(token.getString("access_token"), xcx_qr_path, xcx_qr_width, scene);
// String uploadFile = "/static/xcx_qr/" + DateUtil.formatDate(new Date(), "yyyyMMdd") + "/";
// String fileName = DateUtil.formatDate(new Date(), "yyyyMMddHHmmssSSS") + ".jpg";
// //调用产生文件夹的方法
// FileUtil.makeDirs(webPath + uploadFile);
// Base64Util.GenerateImage(base64String, webPath + uploadFile + fileName);
// blog.setQrUrl(uploadFile + fileName);
// blogService.updateById(blog);
String uploadFile2 = "/static/xcx_haibao/" + DateUtil.formatDate(new Date(), "yyyyMMdd") + "/";
FileUtil.makeDirs(webPath + uploadFile2);
String fileName = DateUtil.formatDate(new Date(), "yyyyMMddHHmmssSSS") + ".jpg";
JSONObject param = new JSONObject();
param.put("img1", webPath+"/static/upload_image/blog_cover/20250624/20250624004044.jpg");
param.put("img2",webPath+"/static/upload_image/blog_cover/20250624/20250624004044.jpg");
param.put("font",WeiXinUtil.FONT);
HaiBaoUtil.create(param,webPath + uploadFile2+ fileName);
result.put("success", true);
result.put("msg", "添加成功");
result.put("btn_disable", true);
return result;
}

字体下载点这里 字体有点大。没有上传成功。
字体是从 c://windows/font 下面复制的

这个复制出来就是 那3个文件。

站长微信:xiaomao0055
站长QQ:14496453