Venkat Maridu
Venkat Maridu

Reputation: 902

How to add text to image in java?

I am using Prime-faces. Here i need to add dynamic text to bottom of the image using java. i.e, I need to "write" on the image and I need to save image with text in my desired PC location.

I have tried this:

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

How do I achieve it?

Upvotes: 1

Views: 5573

Answers (2)

rido
rido

Reputation: 1

you have code for insert txt file to images?

sample file insert.txt content

  1. Cobra
  2. Anaconda
  3. Phyton
  4. Comodo

Note. Wildlife of Amazon

Upvotes: 0

Joni
Joni

Reputation: 111359

You can draw on a BufferedImage using the the Graphics object:

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

Upvotes: 0

Related Questions