java 文字生成图片
发布日期:2022-01-11 03:10:17
浏览次数:3
分类:技术文章
本文共 3988 字,大约阅读时间需要 13 分钟。
直接上代码:
import java.awt.*; import java.awt.image.*; import java.awt.font.*; import java.awt.geom.*; import java.io.File;import java.io.IOException;import java.io.OutputStream;import java.util.UUID;import javax.imageio.ImageIO;/** * 创建文字图片 * @author yuki_ho * */public class FontImage { // 默认格式 private static final String FORMAT_NAME = "JPG"; // 默认 宽度 private static final int WIDTH = 100; // 默认 高度 private static final int HEIGHT =100; /** * 创建图片 * @param content 内容 * @param font 字体 * @param width 宽 * @param height 高 * @return */ private static BufferedImage createImage(String content,Font font,Integer width,Integer height){ BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = (Graphics2D)bi.getGraphics(); g2.setBackground(Color.WHITE); g2.clearRect(0, 0, width, height); g2.setPaint(Color.BLACK); FontRenderContext context = g2.getFontRenderContext(); Rectangle2D bounds = font.getStringBounds(content, context); double x = (width - bounds.getWidth()) / 2; double y = (height - bounds.getHeight()) / 2; double ascent = -bounds.getY(); double baseY = y + ascent; g2.drawString(content, (int)x, (int)baseY); return bi; } /** * 获取 图片 * @param content 内容 * @param font 字体 * @param width 宽 * @param height 高 * @return */ public static BufferedImage getImage(String content,Font font,Integer width,Integer height){ width=width==null?WIDTH:width; height=height==null?HEIGHT:height; if(null==font) font = new Font("Serif", Font.BOLD, 11); return createImage(content, font, width, height); } /** * 获取 图片 * @param content 内容 * @param width 宽 * @param height 高 * @return */ public static BufferedImage getImage(String content,Integer width,Integer height){ return getImage(content, null,width, height); } /** * 获取图片 * @param content 内容 * @return */ public static BufferedImage getImage(String content){ return getImage(content, null, null); } /** * 获取图片 * @param content 内容 * @param font 字体 * @param width 宽 * @param height 高 * @param destPath 输出路径 * @throws IOException */ public static void getImage(String content,Font font ,Integer width,Integer height,String destPath) throws IOException{ mkdirs(destPath); String file = UUID.randomUUID().toString()+".jpg"; ImageIO.write(getImage(content,font,width,height),FORMAT_NAME, new File(destPath+"/"+file)); } /** * 获取图片 * @param content 内容 * @param font 字体 * @param width 宽 * @param height 高 * @param output 输出流 * @throws IOException */ public static void getImage(String content,Font font,Integer width,Integer height, OutputStream output) throws IOException{ ImageIO.write(getImage(content,font,width,height), FORMAT_NAME, output); } /** * 获取图片 * @param content 内容 * @param width 宽 * @param height 高 * @param destPath 输出路径 * @throws IOException */ public static void getImage(String content,Integer width,Integer height,String destPath) throws IOException{ getImage(content, null, width, height, destPath); } /** * 获取图片 * @param content 内容 * @param width 宽 * @param height 高 * @param output 输出流 * @throws IOException */ public static void getImage(String content,Integer width,Integer height, OutputStream output) throws IOException{ getImage(content, null, width, height, output); } /** * 创建 目录 * @param destPath */ public static void mkdirs(String destPath) { File file =new File(destPath); //当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常) if (!file.exists() && !file.isDirectory()) { file.mkdirs(); } } public static void main(String[] args) throws Exception { getImage("MAS-123456", 100, 100, "d:/test"); }}
可以配合二维码 一起使用:
转载地址:https://yuki-ho.blog.csdn.net/article/details/69568566 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2024年04月22日 13时35分59秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
SAP MM模块相关透明表收集
2019-04-28
王峰记忆法 解密王峰记忆法之迷
2019-04-28
传统BI为什么注定会失败?
2019-04-28
ETL工具大全,你了解多少
2019-04-28
TRFC/QRFC
2019-04-28
18、Power Query-SQL筛选
2019-04-28
46、Power Query-Table.FillDown函数简介
2019-04-28
CDS – One Concept, Two Flavors
2019-04-28
SAP Development Tools
2019-04-28
ABAP算术运算函数
2019-04-28
用新语法写更简洁的ABAP代码
2019-04-28
ABAP类的方法
2019-04-28
SAP用户增强总结-采购订单建立增加客户数据增强示例
2019-04-28
物料帐结算,库存差异为何在下月初冲回
2019-04-28
SAP中创建分部机构凭证号码并且按年度编号
2019-04-28
ALV OO的栏位属性
2019-04-28
如何取得用户参数文件中的parameters
2019-04-28
汇兑损益如何结转
2019-04-28
采购订单收货后不能修改价格的增强
2019-04-28