java 生成字体图片 如何描边加入?

cz12980 2017-04-15 03:58:21
以下代码是我的生成文字 BufferedImage 函数:



// 水印函数
public static W_P createImage(String str) throws Exception {
// 获取font的样式应用在str上的整个矩形
Font font = new Font(Constant_.W_Font, Font.BOLD, Constant_.W_Size);
Rectangle2D r = font.getStringBounds(str, new FontRenderContext(
AffineTransform.getScaleInstance(1, 1), false, false));

int unitHeight = (int) Math.floor(r.getHeight());// 获取单个字符的高度
// 获取整个str用了font样式的宽度这里用四舍五入后+1保证宽度绝对能容纳这个字符串作为图片的宽度
int width = (int) Math.round(r.getWidth()) + 1;
int height = unitHeight;// 把单个字符的高度+3保证高度绝对能容纳字符串作为图片的高度

// 创建图片
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();

// 测试加的黑色背景
// g.setColor(new Color(0Xff000000));
// 设置透明 start
image = g.getDeviceConfiguration().createCompatibleImage(width, height,
Transparency.TRANSLUCENT);
g = image.createGraphics();
// 设置透明 end
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
Constant_.W_alpha);
g.setComposite(ac);
g.setBackground(Color.BLACK);
g.setColor(Color.WHITE);// 在换成白色
g.setFont(font);// 设置画笔字体

/* 消除java.awt.Font字体的锯齿 */
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.drawString(str, 0, font.getSize());// 画出字符串
g.dispose();

W_P w = new W_P();
w.setBufferedImage(image);
w.setHeight(height);
w.setWidth(width);
return w;
}


在这里 通过w.setBufferedImage(image); 储存了image,也就是我需要的文字BufferedImage,以便调用。
如下是我当前的效果:



希望修改为这个效果,也就是加一个黑色描边就可以:




请问应该如何修改我这个代码?感谢感谢





以下是W_P 类:



import java.awt.image.BufferedImage;

public class W_P {
public int width;
public int height;
public BufferedImage bufferedImage;

public int getWidth() {
return width;
}

public void setWidth(int width) {
this.width = width;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}

public BufferedImage getBufferedImage() {
return bufferedImage;
}

public void setBufferedImage(BufferedImage bufferedImage) {
this.bufferedImage = bufferedImage;
}
}

...全文
307 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧