Picture Verification Code implementation The main technical point is how to generate a picture. The build picture can be implemented using the classes under the java.awt package. Let's write a simple program to generate a picture Helloimage.java. Here is the code section.
Package com.vogoal.test;
Import Java.awt.Color;
Import Java.awt.Graphics;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import java.io.IOException;
Import Javax.imageio.ImageIO;
/**
* Create a image
*/
public class Helloimage {
public static void Main (string[] args) {
BufferedImage image = New BufferedImage (80, 25,
BUFFEREDIMAGE.TYPE_INT_RGB);
Graphics g = image.getgraphics ();
G.setcolor (New Color (255,255,255));
G.fillrect (0, 0, 80, 25);
G.setcolor (New Color (0,0,0));
g.DrawString ("Helloimage", 6,16);
G.dispose ();
try{
Imageio.write (image, "JPEG", New File ("C:\\helloimage.jpeg"));
}catch (IOException e) {
E.printstacktrace ();
}
}
}
After compiling, in DOS down use this program, under normal circumstances, will be in the C packing directory to generate a name helloimage.jpeg for the picture. There are text helloimage in the picture.
A brief introduction to the process of generating pictures:
1. Establish the BufferedImage object. Specifies the width and color of the picture's length.
BufferedImage image = new BufferedImage (80,25,BUFFEREDIMAGE.TYPE_INT_RGB);
2. Get the Graphics object to draw the picture.
Graphics g = image.getgraphics ();
3. Draw picture background and text.
4. Release the resources occupied by the Graphics object.
G.dispose ();
5. Output the picture through the write static method of the ImageIO object.
Imageio.write (image, "JPEG", New File ("C:\\helloimage.jpeg"));