[Add to favorites] generate verification Images

Source: Internet
Author: User

Generate verification Image

1. Image Generation class

Public class encryptimage {

Public String srand = "";

Public color getrandcolor (int fc, int BC) {// obtain a random color from a given range
Random random = new random ();
If (FC> 255)
Fc = 255;
If (BC> 255)
BC = 255;
Int r = FC + random. nextint (BC-Fc );
Int G = FC + random. nextint (BC-Fc );
Int B = FC + random. nextint (BC-Fc );
Return new color (R, G, B );
}

Public bufferedimage creatimage (){

// Create an image in memory
Int width = 60, Height = 20;
Bufferedimage image = new bufferedimage (width, height,
Bufferedimage. type_int_rgb );

// Obtain the image Context
Graphics G = image. getgraphics ();

// Generate a random class
Random random = new random ();

// Set the background color
G. setcolor (getrandcolor (200,250 ));
G. fillrect (0, 0, width, height );

// Set the font
G. setfont (new font ("Times New Roman", Font. Plain, 18 ));

// Draw a border
// G. setcolor (new color ());
// G. drawrect (0, 0, width-1, height-1 );
// Generates 155 random interference lines, making the authentication code in the image hard to be detected by other programs.
G. setcolor (getrandcolor (160,200 ));
For (INT I = 0; I <155; I ++ ){
Int x = random. nextint (width );
Int y = random. nextint (height );
Int XL = random. nextint (12 );
Int yl = random. nextint (12 );
G. drawline (X, Y, x + XL, Y + yl );
}

// Obtain the random ID code (4 digits)
// String Rand = request. getparameter ("RAND ");
// Rand = Rand. substring (0, Rand. indexof ("."));

For (INT I = 0; I <4; I ++ ){
String Rand = string. valueof (random. nextint (10 ));
Srand + = rand;
// Display the authentication code to the image
// The Color of the called function is the same. It may be because the seed is too close and can only be directly generated.
G. setcolor (new color (20 + random. nextint (110), 20 + random
. Nextint (110), 20 + random. nextint (110 )));
G. drawstring (RAND, 13 * I + 6, 16 );
}
// The image takes effect
G. Dispose ();
Return image;
}
/**
* @ Return returns the srand.
*/
Public String getsrand (){
Return srand;
}
/**
* @ Param Rand the srand to set.
*/
Public void setsrand (string rand ){
Srand = rand;
}
}

2 servlet output

Public class eimage extends httpservlet {

Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Response. addheader ("Content-Type", "image/JPEG ");
Response. setheader ("Pragma", "No-Cache ");
Response. setheader ("cache-control", "No-Cache ");
Response. setdateheader ("expires", 0 );
Encryptimage Ei = new encryptimage ();
Bufferedimage IMG = ei. creatimage ();
Request. getsession (true). setattribute ("ecode", ei. getsrand ());
ImageIO. Write (IMG, "Jpeg", response. getoutputstream ());
}
}

3. check through the ecode IN THE SESSION

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.