java生成图片【奖状】

我是用搭了个javaWeb的项目写的,大家根据自己的需求看

package com.jiyun.servlet;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


public class cServlet {

    public static class CertificateGenerator {

        public static void main(String[] args) {
            String backgroundImagePath = "a.png"; // 奖状背景图片路径
            String outputImagePath = "aaa.png"; // 输出图片路径
            String name = "“最佳MVP”"; // 姓名
            String date = "2024-04-11"; // 日期
            String classRoom = "xxx"; // 班级

            // 创建带有背景图片的奖状
            try {
                BufferedImage backgroundImage = ImageIO.read(new File(backgroundImagePath));
                int width = backgroundImage.getWidth();
                int height = backgroundImage.getHeight();
                BufferedImage certificate = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
                Graphics2D g2d = certificate.createGraphics();
                g2d.drawImage(backgroundImage, 0, 0, null);

                // 设置文本属性
                Font font = new Font("宋体", Font.BOLD, 32);
                g2d.setFont(font);
                g2d.setColor(Color.BLACK);

                // 添加文本内容
                String title = "_______同学";
                Font titleFont = new Font("宋体", Font.BOLD, 48);
                g2d.setFont(titleFont);
                FontMetrics metrics = g2d.getFontMetrics();
                int titleX = (width - metrics.stringWidth(title)) / 12;
                int titleY = 510; // 距离顶部的距离
                g2d.drawString(title, titleX, titleY);

                String mvpText = "在今日分享中取得____的最高分,被评为“最佳MVP”,相信在今后的求职路上";
                g2d.setFont(font);
                //int mvpX = (width - metrics.stringWidth(mvpText)) / 1;
                int mvpY = titleY + 70; // 距离标题的距离
                g2d.drawString(mvpText, 120, mvpY);

                String mvp2Text = "依然会保持好心态,勇往直前";
                g2d.setFont(font);
                int mvp2X = (width - metrics.stringWidth(mvp2Text)) / 9;
                int mvp2Y = mvpY + 50; // 距离标题的距离
                g2d.drawString(mvp2Text, mvp2X, mvp2Y);

                String nameText = "" + name;
                Font nameFont = new Font("宋体", Font.ITALIC, 100);
                g2d.setFont(nameFont);
                g2d.setColor(Color.RED);
                int nameX = (width - metrics.stringWidth(nameText)) / 3;
                int nameY = mvpY + 160; // 距离MVP文本的距离
                g2d.drawString(nameText, nameX, nameY);

                String dateText = "日期: " + date;
                Font dateFont = new Font("宋体", Font.BOLD, 40);
                g2d.setFont(dateFont);
                g2d.setColor(Color.BLACK);
                int dateX = (width - metrics.stringWidth(dateText)) / 1;
                int dateY = nameY + 100; // 距离姓名文本的距离
                g2d.drawString(dateText, dateX, dateY);

                String classRoomText = "班级: " + classRoom;
                //int classRoomX = (width - metrics.stringWidth(classRoomText)) / 1;
                int classRoomY = dateY + 60; // 距离日期文本的距离
                g2d.drawString(classRoomText, 955, classRoomY);

                // 释放资源
                g2d.dispose();

                // 保存生成的奖状图片
                ImageIO.write(certificate, "PNG", new File(outputImagePath));
                System.out.println("奖状已生成至: " + outputImagePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值