// The following code is placed on the JSP server to generate a Random Digital Image verification code.
<% @ Page contenttype = "image/JPEG; charset = gb2312" Language = "Java" %>
<% @ Page import = "Java. AWT. *, java. AWT. image. *, java. util. *, javax. ImageIO. *" %>
<%!
Color getrandcolor (int fc, int BC)
{
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 );
}
%>
<%
Response. setheader ("Pragma", "No-Cache ");
Response. setheader ("cache-control", "No-Cache ");
Response. setdateheader ("expires", 0 );
Int I _width = 60;
Int I _height = 20;
Bufferedimage image = new bufferedimage (I _width, I _height, bufferedimage. type_int_rgb );
Graphics G = image. getgraphics ();
Random random = new random ();
G. setcolor (getrandcolor (200,250 ));
G. fillrect (0, 0, I _width, I _height );
G. setfont (new font ("Times New Roman", Font. Plain, 18 ));
G. setcolor (getrandcolor (160,200 ));
For (INT I = 0; I <155; I ++)
{
Int I _x = random. nextint (I _width );
Int I _y = random. nextint (I _height );
Int I _x1 = random. nextint (12 );
Int I _y1 = random. nextint (12 );
G. drawline (I _x, I _y, I _x + I _x1, I _y + I _y1 );
}
String s_rand = "";
For (INT I = 0; I <4; I ++)
{
String Rand = string. valueof (random. nextint (10 ));
S_rand + = rand;
G. setcolor (new color (20 + random. nextint (110), 20 + random. nextint (110), 20 + random. nextint (110 )));
G. drawstring (RAND, 13 * I + 6, 16 );
}
Session. setattribute ("RAND", s_rand );
G. Dispose ();
ImageIO. Write (image, "Jpeg", response. getoutputstream ());
%>