效果:
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();}}