当前位置: 首页 > news >正文

Java文字描边效果实现

效果:

FontUtil工具类的完整代码如下:

其中实现描边效果的函数为:generateAdaptiveStrokeFontImage()

package com.ncarzone.data.contentcenter.biz.img.util;import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import sun.font.FontDesignMetrics;import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;/*** 字体处理工具类*/
public class FontUtil {public static String TTF_ZHIZUN = "ZHIZUN"; // 至尊体public static String TTF_BIAOTIHEI = "BIAOTIHEI"; // 标题黑/*** 自适应方式的文字转描边的艺术字图片* @param text 文本内容* @param fontTTFName 字体名称* @param fontWeight 字体大小* @param fontColor 字体颜色* @param isItalic 是否倾斜* @return 字体图片信息*/public static BufferedImage generateAdaptiveStrokeFontImage(String text, String fontTTFName, float fontWeight,Color fontColor, boolean isItalic){//字体加载以及设置Font font = loadFont(fontTTFName, fontWeight, isItalic);int textWidth = new Double(getWordFontWidth(font, text) * 1.06).intValue();  // 考虑倾斜的影响,稍微增加长度int textHeight = getFontHeight(font);BufferedImage bi = new BufferedImage(textWidth, textHeight, BufferedImage.TYPE_INT_RGB);Graphics2D g2d = (Graphics2D) bi.getGraphics();// 增加下面代码使得背景透明bi = g2d.getDeviceConfiguration().createCompatibleImage(textWidth, textHeight, Transparency.TRANSLUCENT);g2d.dispose();g2d = (Graphics2D) bi.getGraphics();//消除锯齿ImageUtil.antialiasing(g2d);FontRenderContext frc = g2d.getFontRenderContext();TextLayout tl = new TextLayout(text, font, frc);Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(0,fontWeight));g2d.setStroke(new BasicStroke((float)(fontWeight * 0.1)));g2d.setColor(Color.BLACK);g2d.draw(sha);g2d.setColor(fontColor);g2d.fill(sha);g2d.dispose();return bi;}/*** 加载字体* @param fontTTFName 字体名称* @param fontWeight 字体大小* @param isItalic 字体是否倾斜* @return*/public static Font loadFont(String fontTTFName, float fontWeight, boolean isItalic){//字体加载以及设置Font font = null;try {Resource fontResource = new ClassPathResource("fonts/" + fontTTFName + ".TTF");InputStream inputStream = fontResource.getInputStream();Font f = Font.createFont(Font.TRUETYPE_FONT, inputStream);if( isItalic ){font = f.deriveFont(Font.PLAIN, fontWeight).deriveFont(AffineTransform.getShearInstance(-0.18, 0));}else{font = f.deriveFont(Font.PLAIN, fontWeight);}}catch (IOException | FontFormatException e){e.printStackTrace();}return font;}/*** 计算文字占用的字体宽度总和* @param font   字体* @param content 文字* @return*/public static int getWordFontWidth(Font font, String content){FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);int width = 0;for( int i = 0; i < content.length(); i++ ){width += metrics.charWidth(content.charAt(i));}return width;}/*** 获取字体的高度* @param font* @return*/public static int getFontHeight(Font font){FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);int height = metrics.getHeight();return height;}
}

消除锯齿函数的代码如下:

// 消除锯齿public static void antialiasing(Graphics2D g2d){g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);}

测试代码如下:

BufferedImage bi = FontUtil.generateAdaptiveStrokeFontImage("中秋节快乐", "BIAOTIHEI", 300, Color.WHITE, true);
ImageUtil.storeImg2LocalStorage(bi, "font", 3);

其中保存测试图片的代码为:

// 保存图片到本地public static void storeImg2LocalStorage(BufferedImage bi, String name, int i){try{ImageIO.write(bi, "png", new File("D:\\temp\\image\\output\\" + name + i + ".png"));}catch (IOException e){e.printStackTrace();}}

http://www.lryc.cn/news/169673.html

相关文章:

  • 【Web_环境搭建_Python3_pip】pip的升级、安装、更新、卸载,以及pipupgrade和pip-review的基础使用
  • 农民朋友有福利啦!建行江门市分行“裕农通+农资结算”平台正式上线
  • super详解
  • GMS地下水数值模拟丨GMS各模块、三维地质模型构建及与MODFLOW耦合、地下水流动数值模拟及报告编制、地下水溶质运移模型、反应性溶质运移等
  • Redis 配置文件详解 - 持久化(RDB、AOF)
  • 在线Excel转JSON工具
  • Spring编程常见错误50例-Spring Bean依赖注入常见错误(下)
  • SpringBoot整合Canal实现MySQL与ES数据同步
  • Zookeeper 源码分析流程
  • 计数排序与基数排序
  • Mysql—表操作
  • SpringCloud——微服务
  • 深入理解Java单例模式和优化多线程任务处理
  • 已解决 Kotlin Error: Type mismatch: inferred type is String but Int was expected
  • Web应用系统的小安全漏洞及相应的攻击方式
  • git工具下载和安装
  • 腾讯mini项目-【指标监控服务重构】2023-08-04
  • 怎么推广自己抖店的商品?最适合0经验新手操作的办法,来看看
  • 线性代数的本质(三)——线性方程组
  • 轻量级性能测试工具 wrk 如何使用?
  • WebGL 视图矩阵、模型视图矩阵
  • Python 3 – 文件 readline() 方法
  • 如何在微软Edge浏览器上一键观看高清视频?
  • Telegram BoT的主流项目盘点
  • PTA 甲级 1044 Shopping in Mars
  • Linux学习之MyCat实现分库分表
  • DirectX12(d3d12)初始化
  • 算法通关村-----回溯模板如何解决排列组合问题
  • 【1++的C++进阶】之智能指针
  • 一百七十九、Linux——Linux报错No package epel-release available