java后台生成图片(加载外部字体)

Java后台生成图片:

  1. 自定义图片内文字和图片位置。
  2. 可背景透明。
  3. 字体尺寸、颜色、间距等基本设置和透明度设置。
  4. 文本换行与居中。
  5. 加载外部字体。

原理

使用java图形化工具绘制图片。

注意:linux系统只支持ttf文件

 

代码实现

创建所需参数的实体类

import lombok.Data;
import java.awt.*;
import java.awt.image.BufferedImage;
/**
 * 生成图片参数控制
 *
 * @author h
 * @date Created in 2020/5/30 17:25
 */
@Data
public class ImageCreateEntity {
    /**
     * 宽度
     */
    private Integer width = 100;
    /**
     * 高度
     */
    private Integer height = 100;
    /*==============图片内容==============*/
    /**
     * 图片内容
     */
    private BufferedImage imgContent;
    /**
     * 图片宽度
     */
    private int imgWidth = 100;
    /**
     * 图片宽度
     */
    private int imgHeight = 100;
    /**
     * 图片渲染X起点
     */
    private int imgX;
    /**
     * 图片渲染Y轴起点
     */
    private int imgY;
    /*=====================文本内容===================*/
    /**
     * 文本内容
     */
    private String textContent;
    /**
     * 字体名称
     */
    private String fontName = "";
    /**
     * 字体文件路径
     */
    private String fontFilePath = "";
    /**
     * 字体尺寸
     */
    private float fontSize = 20f;
    /**
     * 字体风格
     */
    private int fontStyle = Font.PLAIN;
    /**
     * 字体颜色
     */
    private Color fontColor = Color.BLACK;
    /**
     * 字体间距,默认值为零,与当前字体尺寸相关
     */
    private Integer fontSpace = 0;
    /**
     * 行距
     */
    private Integer linePadding = 10;
    /**
     * 文本透明度:值从0-1.0,依次变得不透明
     */
    private float textTransparency = 1.0f;
    /**
     * 文本渲染X起点
     */
    private Integer textX = 0;
    /**
     * 文本渲染Y轴起点
     */
    private Integer textY = 0;
    /**
     * 左边距
     */
    private Integer textLeftPadding = 0;
    /**
     * 右边距
     */
    private Integer textRightPadding = 0;
    /**
     * 每行居中
     */
    private boolean isCenterLine;
    /*=================背景==============*/
    /**
     * 背景颜色
     */
    private Color backgroundColor = Color.WHITE;
    /**
     * 背景是否透明
     */
    private boolean isTransparentBackground = false;
    /**
     * 背景图片
     */
    private BufferedImage backgroundImg;
}
复制代码

生成图片基本方法

创建一个 BufferedImage ,再通过Graphics2D 进行绘制,就可以得到一个简单的图片缓存。

Graphics2D class extends the Graphics class to provide more sophisticated control over geometry, coordinate transformations, color management,and text layout. This is the fundamental class for rendering 2-dimensional shapes, text and images on the Java(tm) platform.

Graphics2D类扩展了Graphics类,以提供对几何图形,坐标转换,颜色管理和文本布局的更复杂的控制。 这是在Java(tm)平台上渲染二维形状,文本和图像的基本类。

	BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	Graphics2D g2d = bf.createGraphics();
	//设置图片背景颜色
	g2d.setBackground(Color.WHITE);
	g2d.clearRect(0, 0, width, height);
	//绘制图片内容
  // 中间内容框画到背景图上
	BufferedImage imgContent = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	//绘制图片
	g2d.drawImage(imgContent.getSca
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xuxizhou1994

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值