Wednesday, February 11, 2009

Drawing String on image

Hi,

Suppose you want to edit any image and want to write some text permanently
on image.

Here i posting code for do so.

It opens a image name test.jpg and write a text and then save on same place with name "output.jpg".

You just change the name of files as you required.

For source code see below

/*

* Program for drawing string or text over a image and save with that

* Copyright 2009 @ yuvadeveloper

* Code By:- Prashant Chandrakar

*

*/

import java.awt.*;

import java.awt.image.*;

import java.io.*;

import javax.imageio.*;

class DrawString

{

public static void main(String a[])

{

try

{

////pass image file path open for drawing

BufferedImage image = ImageIO.read(new File("test.JPEG"));

////taking graphics from image

Graphics g = image.getGraphics();

/////drawing string on graphics of opened image

g.drawString("I am a string drawn on image pixel", 10, image.getHeight() - 10);

////saving image by name output.jpg

ImageIO.write(image, "jpg", new File("output.jpg"));

}

catch (Exception e)

{

System.out.println(e);

}

}

}

No comments:

Post a Comment