如何将文本添加到Java中的图像? [英] How to add text to an image in java?

查看:125
本文介绍了如何将文本添加到Java中的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一些文本添加到现有的表格图像(png)。
这意味着我需要在图像上写,并且我需要选择文本位置的选项。
我该怎么做?
非常感谢。

I need to add some texts to an existing table image (png). Which means that I need to "write" on the image and I need the option to select the text location. How can I do it? Thank you very much.

推荐答案

很简单,只需取得 Graphics 对象从图像中绘制并将您的字符串绘制到图像上。这个例子(和输出图像)是这样做的:

It's easy, just get the Graphics object from the image and draw your string onto the image. This example (and output image) is doing that:

public static void main(String[] args) throws Exception {
    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"));
}

输出( test.png ):

这篇关于如何将文本添加到Java中的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆