【问题标题】:How to add text outline using java.awt.*?如何使用 java.awt.* 添加文本轮廓?
【发布时间】:2014-09-17 15:17:08
【问题描述】:

如何使用 java.awt.* 向图像添加带有轮廓的文本?

下面的例子:

【问题讨论】:

  • 转到oracle.com/technetwork/java/javase/downloads 并下载“Demos and Samples”包(JDK 7 或 8 都可以)。 Java2Demo 示例程序(又名“Java2D Demo”)演示了您想要做什么以及许多其他事情,并包含所有源代码。
  • 在图片上写文字(或者至少是写文字的代码)吗?

标签: 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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    相关资源
    最近更新 更多