Java generates the verification code image, and java generates the verification code

Source: Internet
Author: User

Java generates the verification code image, and java generates the verification code
Public class AuthImg extends HttpServlet {

/**
*
*/
Private static final long serialVersionUID = 4975974534946437434L;

// Set the font and size of the graphic verification code string
Private Font mFont = new Font ("", Font. ITALIC, 18 );

Private Random random = new Random ();

Public void init () throws ServletException {
Super. init ();
}

// Service method for generating Server Response
Public void service (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Prevents the generated page from being cached to ensure that a new verification code is generated each time.
Response. setHeader ("Pragma", "No-cache ");
Response. setHeader ("Cache-Control", "no-cache ");
Response. setDateHeader ("Expires", 0 );
Response. setContentType ("image/jpeg ");
// Specify the size of the Verification Code Image
Int width = 80, height = 24;
// Generate a new image
BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_3BYTE_BGR );
// Draw content in the image
Graphics g = image. getGraphics ();
G. setColor (new Color (252,252,252 ));
G. fillRect (1, 1, width, height );
G. setColor (new Color (252,252,252 ));
G. drawRect (0, 0, width, height );
G. setFont (mFont );
G. setColor (new Color (252,252,252 ));

// This variable is used to save the random variable string generated by the system.
String sRand = "";
Int colorR = random. nextInt (200 );
Int colorG = random. nextInt (200 );
Int colorB = random. nextInt (200 );
For (int I = 0; I <4; I ++ ){
// Obtain a random string
String tmp = getRandomChar ();
SRand + = tmp;
// Add the random string generated by the system to the image
G. setColor (new Color (colorR, colorG, colorB ));
G. drawString (tmp, 15 * I + 10, 20 );
}
// Obtain the user's Session
HttpSession session = request. getSession (true );
// Add the Random verification code generated by the system to the user Session
Session. setAttribute ("rand", sRand );
G. dispose ();
// Output the verification code Image
ImageIO. write (image, "JPEG", response. getOutputStream ());
}

// Method for generating random strings
Private String getRandomChar (){
Int itmp = random. nextInt (10 );
Return String. valueOf (itmp );
}

}


A verification code program generated in java

Encapsulate the program that generates the verification code image as a JavaBean. The source code of this JavaBean is as follows:
MakeCertPic. java
Package pic;
Import java. awt. Color;
Import java. awt. Font;
Import java. awt. Graphics;
Import java. awt. image. BufferedImage;
Import java. io. IOException;
Import java. io. OutputStream;
Import java. util. Random;
Import javax. imageio. ImageIO;
/**
* @ Author dzy
* Generate a verification code Image
*/
Public class makeCertPic {
// The character set that can appear in the verification code image, which can be modified as needed
Private char mapTable [] = {
'A', 'B', 'C', 'D', 'E', 'E', 'F ',
'G', 'h', 'I', 'J', 'k', 'l ',
'M', 'n', 'O', 'P', 'Q', 'R ',
'S ', 't', 'U', 'V', 'w', 'x ',
'Y', 'z', '0', '1', '2', '3 ',
'4', '5', '6', '7', '8', '9 '};
/**
* Function: generate a color Verification Code image.
* The width parameter indicates the width of the generated image. The height parameter indicates the height of the generated image. The OS parameter indicates the output stream of the page.
*/
Public String getCertPic (int width, int height, OutputStream OS ){
If (width <= 0) width = 60;
If (height <= 0) height = 20;
BufferedImage image = new BufferedImage (width, height,
BufferedImage. TYPE_INT_RGB );
// Obtain the image Context
Graphics g = image. getGraphics ();
// Set the background color
G. setColor (new Color (0 xDCDCDC ));
G. fillRect (0, 0, width, height );
// Draw a border
G. setColor (Color. black );
G. drawRect (0, 0, width-1, height-1 );
// Obtain the randomly generated recognition... the remaining full text>

JAVA verification code generation problems

You must have no image output.
Generally, the possible cause is:
1. There is no information when your verification code image is generated. It is an "empty" image.
2. You forgot to output the image output stream to the page.
3. The page you output is not the same as the page you originally wanted to lose

The best solution: online search for mature code verification code image generation is not very complicated.
Second, check the Code in order.
Finally, give up ~!

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.