java - 图像上的粗体文字

标签 java image bufferedimage

我想在图像上附加粗体文本,只有选定的文本应该是粗体。

String word="这是虚拟文本,应为 BOLD"

final BufferedImage image = ImageIO.read(new File(Background));
Graphics g = image.getGraphics();
g.drawString(word, curX, curY);
g.dispose();
ImageIO.write(image, "bmp", new File("output.bmp"));

最佳答案

您想要使用AttributedString并将其迭代器传递给drawString

static String Background = "input.png";
static int curX = 10;
static int curY = 50;

public static void main(String[] args) throws Exception {
    AttributedString word= new AttributedString("This is text. This should be BOLD");

    word.addAttribute(TextAttribute.FONT, new Font("TimesRoman", Font.PLAIN, 18));
    word.addAttribute(TextAttribute.FOREGROUND, Color.BLACK);

    // Sets the font to bold from index 29 (inclusive)
    // to index 33 (exclusive)
    word.addAttribute(TextAttribute.FONT, new Font("TimesRoman", Font.BOLD, 18), 29,33);
    word.addAttribute(TextAttribute.FOREGROUND, Color.BLUE, 29,33);

    final BufferedImage image = ImageIO.read(new File(Background));
    Graphics g = image.getGraphics();
    g.drawString(word.getIterator(), curX, curY);
    g.dispose();
    ImageIO.write(image, "png", new File("output.png"));
}

输出.png:

This is text. This should be BOLD

关于java - 图像上的粗体文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16789156/

相关文章:

java - 代号一 javafx.util.pair

Java 将 Image 转换为 BufferedImage

c# - 如何使用 .NET 在电子邮件正文中嵌入多个图像

Javascript - onchange 图像更改

android - Skobbler Android - 如何在 map 上的注释中设置自定义图像?

java - Graphics2D 文本 - 字体大小变化导致 X 轴移动

java - 如何绘制大型 BufferedImage 的一部分?

java - 获取 JPanel 的 PaintComponent 元素以与其中的另一个 JPanel 一起使用

java - 降低环复杂度

java - 将图像放入 webview 并适合其宽度