如何在java中向图像添加文本?

问题描述 投票:0回答:2

我正在使用 Prime-faces。这里我需要使用java将动态文本添加到图像底部。 即,我需要在图像上“书写”,并且需要将带有文本的图像保存在我想要的 PC 位置。

我试过这个:

public void writeToImage() throws MalformedURLException, IOException {
        final BufferedImage image = ImageIO.read(new URL(
                "http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"));

        Graphics g = image.getGraphics();
        g.setFont(g.getFont().deriveFont(30f));
        g.drawString("Hello World!", 100, 100);
        g.dispose();

        ImageIO.write(image, "png", new File("test.png"));
    }

如何实现?

java image text graphics image-editing
2个回答
0
投票

您可以使用 Graphics 对象在 BufferedImage 上绘图:

Graphics2D g = (Graphics2D) image.getGraphics();
g.drawString("hello", x, y);

0
投票

你有将txt文件插入图像的代码吗?

示例文件insert.txt内容

  1. 眼镜蛇
  2. 蟒蛇
  3. 芬顿
  4. 科摩多

注意。 亚马逊野生动物

© www.soinside.com 2019 - 2024. All rights reserved.