现在我只能够实现横排时文字可以实现描边,但将文字竖排时无法实现描边的效果。
横排:
竖排:
/**
* 描边
* @param str 文字
* @param fontType 字体
* @param strokeWidth 描边宽度
* @param fontSize 字号
* @param isBold 是否加粗(0否,1是)
* @param isItalic 是否斜体(0否,1是)
* @param strokeColor 描边颜色
* @param fontColor 字体颜色
* @param srcPath 图片地址
* @param newPath 图片地址
* @param fontDirection 字体方向 (0.横,1 竖)
* @param isUnderline 是否有下划线(0.否,1是)
* @throws Exception
*/
public static void stroke(String str,String fontType,int strokeWidth,int fontSize,Integer isBold, Integer isItalic,
Integer strokeColor,Integer fontColor, String srcPath,String newPath,Integer fontDirection,Integer isUnderline,
Integer x,Integer y ) throws Exception{
Font font = new Font(fontType, Font.PLAIN + (isBold== null ?0:isBold)+(isItalic == null ? 0 : (isItalic==1 ? 2 : 0)), fontSize);
AffineTransform affinetransform = new AffineTransform();
//获取font的样式应用在str上的整个矩形
Rectangle2D r=font.getStringBounds(str, new FontRenderContext(affinetransform,false,false));
int unitHeight = (int) Math.floor(r.getHeight());//获取单个字符的高度
//获取整个str用了font样式的宽度这里用四舍五入后+1保证宽度绝对能容纳这个字符串作为图片的宽度
int width = (int) Math.round(r.getWidth()) +strokeWidth;
int height = unitHeight + strokeWidth;//把单个字符的高度+3保证高度绝对能容纳字符串作为图片的高度
if(fontType != null && (fontType.equals("华文琥珀") || fontType.equals("华文彩云"))){
height += 10;
}
//字体方向 (0.横,1 竖)
/* if (fontDirection!=null && fontDirection==1){
int temp=height;
height=width;
width=temp;
}*/
if(fontDirection != null && fontDirection == 1){//竖向文字
Integer temp = width/str.length();
height = (unitHeight+strokeWidth) * str.length();
width = temp;
}
// 读取源文件
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
//设置背景透明
image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
g2d.dispose();
//抗锯齿
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d = image.createGraphics();
//字体方向 (0.横,1 竖)
if( fontDirection == 1){
g2d.fillRect(x,y,width,height);//清除图层的内容
if(fontColor != null){
g2d.setColor(new Color(fontColor, true));
}
for (int i = 0; i < str.length(); i++) {
AttributedString as = setFontAttribute(str.substring(i, i + 1), font,isUnderline);
g2d.drawString(as.getIterator(), 0, (i+1) * (font.getSize() + 1));
}
}else {
Graphics2D gg = g2d;
gg.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
//抗锯齿
gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
GlyphVector v = font.createGlyphVector(gg.getFontMetrics(font).getFontRenderContext(), str);
Shape shape = v.getOutline();
Rectangle bounds = shape.getBounds();
gg.translate(
(width - bounds.width) / 2 - bounds.x,
(height - bounds.height) / 2 - bounds.y
);
if(fontColor != null){
gg.setColor(new Color(fontColor, true));
}
gg.fill(shape);
gg.setColor(new Color(strokeColor));
gg.setStroke(new BasicStroke(strokeWidth));
gg.draw(shape);
}
File file=new File(newPath);
ImageIO.write(image, "png", file);//输出png图片
}
private static AttributedString setFontAttribute(String str,Font font,Integer isUnderline){
AttributedString as = new AttributedString(str);
as.addAttribute(TextAttribute.FONT, font);
if(isUnderline != null && isUnderline.equals(1)) {
as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
}
// as.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
return as;
}
https://jingyan.baidu.com/article/f25ef25435dac9092d1b8214.html