1. Image package com. esoon. EZActor. ezconfig;
Import java. awt. Color;
Import java. awt. Font;
Import java. awt. Graphics;
Import java. awt. image. BufferedImage;
Import java. util. Random;
Public class Image {
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 (120 );
Int yl = random. nextInt (120 );
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
G. setColor (new Color (20 + random. nextInt (110), 20 + random
. NextInt (110), 20 + random. nextInt (110); // the color of the called function is the same. It may be because the seed is too close, so it can only be generated directly.
G. drawString (rand, 13 * I + 6, 16 );
}
// The image takes effect
G. dispose ();
Return image;
}
}
2. The image Servletpackage com. esoon. EZActor. ezconfig is displayed;
Import java. io. IOException;
Import javax. imageio. ImageIO;
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Public class RandomImage extends HttpServlet {
/**
* Constructor of the object.
*/
Public RandomImage (){
Super ();
}
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
// Set the output format
Response. setContentType ("image/jpeg ");
// Set the page not to cache
Response. setHeader ("Pragma", "No-cache ");
Response. setHeader ("Cache-Control", "no-cache ");
Response. setDateHeader ("Expires", 0 );
// Generate an image
Image image = new Image ();
// Output the image to the page
ImageIO. write (image. creatImage (), "JPEG", response. getOutputStream ());
// Print the verification code
System. Out. println (image. srand );
}
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Doget (request, response );
}
}