You can add graphics and even texts to a existing image or generate a new one from this method.Just think that you need to add some text on some background,And generate it dynamically.this is the method for that.here it says how to give a picture and change it as we need (Adding some graphics) and generate it as a separate jpg or png, If you need to generate a completely new one,Just use
File file = new File();insted of the URL as a parameter.
import javax.imageio.*;
import java.io.*;
import java.awt.image.*;
import java.awt.*;
import java.lang.*;
import java.net.*;
public class Main {
public static void main(String[] args) {
// TODO code application logic here
// Create an image to save
try
{
WriteImage();
}
catch(IOException e)
{
System.out.println(e.toString());
}
}
public static void WriteImage() throws IOException
{
RenderedImage rendImage = myCreateImage();
// Save as PNG
File file = new File("C:/Program Files/
netbeans-5.5.1/enterprise3/apache-tomcat-5.5.17/webapps/ROOT/FormattedPics/Background.png"); //file path,can be any where elsein your pc, here it uses tomcat webapps folder for some other purpose.
ImageIO.write(rendImage, "png", file);
System.out.println("New Png Created");
}
// Returns a generated image.
public static RenderedImage myCreateImage() throws IOException
{
// int width = 100;
// int height = 100;
try
{
// Create a buffered image in which to draw
// BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//location of the image to be modofied
BufferedImage bufferedImage = ImageIO.read(new URL("http://localhost:8084/SourcePics/newimage.png"));
// Create a graphics contents on the buffered image
Graphics2D g2d = bufferedImage.createGraphics();
// Draw graphics
// g2d.setColor(Color.BLUE);
// g2d.fillRect(0, 0, width, height);
// g2d.setColor(Color.GREEN);
// g2d.fillOval(0, 0, width, height);
g2d.drawString("text",40,80);
// Graphics context no longer needed so dispose it
g2d.dispose();
return bufferedImage;
}
catch(Exception e)
{
System.out.println("Error" + e.toString());
return null;
}
}
}
Plz send your comments here
0 comments:
Post a Comment