Use JSP to display random digital verification code pictures in the web page

xiaoxiao2021-03-06  62

code show as below:

<% @ page contenttype = "image / jpeg" import = "java.awt. *, java.awt.image. *, java.util. *, javax.imageio. *"%> <%! Color getrandcolor (int FC) , int BC) {// given random color 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);}%> <% / / Set the page does not cache Response.setHeader ("prgma", "no-cache"); response.setHeader ("cache-control", "no-cache"); Response.SetDateHeader ("Expires", 0);

// Create image in memory int width = 60, height = 20; bufferedimage image = new bufferedimage (width, height, bufferedimage.type_int_rgb);

/ / Get graphics context graphics g = image.getgraphics ();

/ / Generate random 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));

// Sea Border G.SetColor (New Color (255, 255, 255)); g.drawRect (0, 0, Width-1, Height-1);

// Randomly generate 155 interference lines, enabling the authentication code in the image to detect G.SetColor (GetrandColor (160, 200)) in the image; for (int = 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);

// Take the randomly generated authentication code (4 digits) SRAND = ""; 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 call function is the same, it may be because the seed is too close, so only G. DrawString (RAND, 13 * i 6, 16);}

// Deposit the authentication code into sessionSession.SetaTribute ("Rand", SRAND);

// Image Effective g.dispose (); // Output Image to Page Imageio.write (Image, "JPEG", Response.getOutputStream ());%> If you have any questions, you can contact me: WebMaster @ bcxy .com

转载请注明原文地址:https://www.9cbs.com/read-111551.html

New Post(0)