How to add text to an image in java?

試著忘記壹切 提交于 2019-12-17 06:32:17

问题


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.


回答1:


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"));
}

Output (test.png):



来源:https://stackoverflow.com/questions/10929524/how-to-add-text-to-an-image-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!