使用AWT在图片上绘制文字或图片

一、依赖

这2个依赖都不是必备的,只不过为了方便,使用到这2个依赖包的地方可以自己重写

 <dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
     <version>1.18.24</version>
 </dependency>

 <dependency>
     <groupId>cn.hutool</groupId>
     <artifactId>hutool-all</artifactId>
     <version>5.8.5</version>
 </dependency>

二、工具类

package com.ruoyi.common.core.utils;

import cn.hutool.core.util.StrUtil;
import lombok.Builder;
import lombok.Data;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;

/**
 * @author xiaxing
 * @describe aqi
 * @since 2023/7/31 14:58
 */
@Data
@Builder
public class DrawImage {

    private Graphics2D graphics2D;

    private String backImageType;

    private BufferedImage bufferedImage;

    private Font font;

    /**
     * 默认字体
     */
    private final static String FONT_NAME = "fonts-wqy-zenhei";

    public static void main(String[] args) throws IOException {
        // 底图上绘制文字
        DrawImage.builder().build()
                .init("url")
                .font(25)
                .color(Color.WHITE)
                .drawString("7月31日 MU5100", 35, 75)


                .font(70)
                .font("123", 1, 12)
                .drawString("07:00", 35, 200)
                .drawString("09:20", 360, 200)

                .font(30)
                .drawString("首都北京", 35, 250)
                .drawString("上海虹桥", 400, 250)

                .write("D:\\text.png");

        // 底图上覆盖图片
        DrawImage.builder().build()
                .init("url")
                .drawImageCenterAligned("url")
                .write("D:\\image.png");

    }

    /**
     * 初始化graphics2D信息
     * @param backImageFileUrl 背景图片
     */
    public DrawImage init(String backImageFileUrl) throws IOException {
        // 读取图片
        this.bufferedImage = ImageIO.read(new URL(backImageFileUrl));
        // 创建Graphics2D对象
        this.graphics2D = this.bufferedImage.createGraphics();
        // 解决文本存在抗锯齿的问题
        this.graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // 封装图片后缀,用于最后文件输出
        this.backImageType = StrUtil.subAfter(backImageFileUrl, ".", true);
        return this;
    }

    /**
     * 设置字体,数字越大字体越大
     * @param fontSize 字体大小
     */
    public DrawImage font(int fontSize) {
        this.font = new Font(FONT_NAME, Font.PLAIN, fontSize);
        this.graphics2D.setFont(this.font);
        return this;
    }

    /**
     * 设置字体
     * @param fontName 字体名称
     * @param fontSize 字体大小,计算公式(不准确):pt = (px * 72) / 96
     */
    public DrawImage font(String fontName, int fontSize) {
        this.graphics2D.setFont(new Font(fontName, Font.PLAIN, fontSize));
        return this;
    }

    /**
     * 设置字体
     * @param fontName 字体名称
     * @param fontStyle 字体样式
     * @param fontSize 字体大小
     */
    public DrawImage font(String fontName, int fontStyle, int fontSize) {
        this.graphics2D.setFont(new Font(fontName, fontStyle, fontSize));
        return this;
    }

    /**
     * 设置字体颜色
     * @param color 颜色
     */
    public DrawImage color(Color color) {
        this.graphics2D.setColor(color);
        return this;
    }

    /**
     * 写文本右对齐
     * x,y轴以图片的左上角为零点
     * @param text 文本内容
     * @param x 文本距离图片右侧的距离(文本最右侧和图片边缘的距离)
     * @param y 文本y轴
     */
    public DrawImage drawStringRightAligned(String text, int x, int y) {
        // 获取整张图片的宽度
        int width = this.bufferedImage.getWidth();
        // 获取文本的宽度
        int textWidth = this.graphics2D.getFontMetrics(font).stringWidth(text);
        this.graphics2D.drawString(text, width - x - textWidth, y);
        return this;
    }

    /**
     * 在底图上绘制图片,图片位置居中
     * @param url 覆盖图片地址
     */
    public DrawImage drawImageCenterAligned(String url) throws IOException {
        int width = this.bufferedImage.getWidth();
        int height = this.bufferedImage.getHeight();

        BufferedImage overlay = ImageIO.read(new URL(url));

        // 计算要放置的位置(这里为居中)
        int xPos = (width - overlay.getWidth()) / 2;
        int yPos = (height - overlay.getHeight()) / 2;

        this.graphics2D.drawImage(overlay, xPos, yPos, null);
        return this;
    }

    /**
     * 写文本
     * x,y轴以图片的左上角为零点
     * @param text 文本内容
     * @param x 文本x轴
     * @param y 文本y轴
     */
    public DrawImage drawString(String text, int x, int y) {
        this.graphics2D.drawString(text, x, y);
        return this;
    }

    /**
     * 图片覆盖
     * @param url 覆盖图片地址
     * @param x x轴
     * @param y y轴
     */
    public DrawImage drawImage(String url, int x, int y) throws IOException {
        BufferedImage read = ImageIO.read(new URL(url));
        this.graphics2D.drawImage(read, x, y, null);
        return this;
    }

    /**
     * 输出图片
     */
    public void write(String writePath) throws IOException {
        // 释放资源
        this.graphics2D.dispose();
        // 输出图片
        ImageIO.write(this.bufferedImage, this.backImageType, new File(writePath));
    }

    /**
     * 输出图片
     */
    public void write(HttpServletResponse response) throws IOException {
        // 释放资源
        this.graphics2D.dispose();
        ImageIO.write(this.bufferedImage, this.backImageType, response.getOutputStream());
    }

    /**
     * 输出图片
     */
    public void write(OutputStream out) throws IOException {
        // 释放资源
        this.graphics2D.dispose();
        // 将文件上传到minio
        ImageIO.write(this.bufferedImage, this.backImageType, out);
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值