如何在java中向图像添加文本? [英] How to add text to an image in java?

查看:32
本文介绍了如何在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?

推荐答案

这很简单,只需从图像中获取 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天全站免登陆