The technique of realizing picture verification code in JSP

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.