【问题标题】:How to add text outline using java.awt.*?如何使用 java.awt.* 添加文本轮廓?
【发布时间】:2014-09-17 15:17:08
【问题描述】:
如何使用 java.awt.* 向图像添加带有轮廓的文本?
下面的例子:
【问题讨论】:
标签:
java
image
image-processing
text
awt
【解决方案1】:
我可以指导你,但你应该知道awt 工作的基本功能。
看看平滑 Smoothing a jagged。在最终版本中,获得(粗)轮廓的算法相对较快。创建GeneralPath 比添加Area 对象快得多。For better understanding refer this
重要的部分是这个方法:
public Area getOutline(Color target, BufferedImage bi) {
// construct the GeneralPath
GeneralPath gp = new GeneralPath();
boolean cont = false;
int targetRGB = target.getRGB();
for (int xx=0; xx<bi.getWidth(); xx++) {
for (int yy=0; yy<bi.getHeight(); yy++) {
if (bi.getRGB(xx,yy)==targetRGB) {
if (cont) {
gp.lineTo(xx,yy);
gp.lineTo(xx,yy+1);
gp.lineTo(xx+1,yy+1);
gp.lineTo(xx+1,yy);
gp.lineTo(xx,yy);
} else {
gp.moveTo(xx,yy);
}
cont = true;
} else {
cont = false;
}
}
cont = false;
}
gp.closePath();
// construct the Area from the GP & return it
return new Area(gp);
}