Java implements the verification code function and java implements the verification code
Java Verification Code
General idea of using java code to implement the verification code function:
1. Use the java code to generate a verification code image and save the verification code image to a specified file in the project. The Code is as follows:
Package com. util; import java. awt. color; import java. awt. font; import java. awt. graphics; import java. awt. graphics2D; import java. awt. renderingHints; import java. awt. geom. affineTransform; import java. awt. image. bufferedImage; import java. io. file; import java. io. fileOutputStream; import java. io. IOException; import java. io. outputStream; import java. util. arrays; import java. util. random; import javax. imageio. imageIO; p Ublic class VerifyCodeUtils {// use the Algerian font. If the font is not in the system, the font needs to be installed. The font only displays uppercase letters, with, I removed, o several confusing characters: public static final String VERIFY_CODES = "23456789 ABCDEFGHJKLMNPQRSTUVWXYZ"; private static Random random = new Random (); /*** use the system default character source to generate a verification code ** @ param verifySize * Verification code length * @ return */public static String generateVerifyCode (int verifySize) {return generateVerifyCode (verifySize, VERIFY_CODES);}/*** use the specified source to generate a Verification Code * * @ Param verifySize * Verification code length * @ param sources * Verification Code character source * @ return */public static String generateVerifyCode (int verifySize, String sources) {if (sources = null | sources. length () = 0) {sources = VERIFY_CODES;} int codesLen = sources. length (); Random rand = new Random (System. currentTimeMillis (); StringBuilder verifyCode = new StringBuilder (verifySize); for (int I = 0; I <verifySize; I ++) {verifyCode. Append (sources. charAt (rand. nextInt (codesLen-1);} return verifyCode. toString ();}/*** generate a Random verification code file, return the verification code value ** @ param w * @ param h * @ param outputFile * @ param verifySize * @ return * @ throws IOException */public static String outputVerifyImage (int w, int h, File outputFile, int verifySize) throws IOException {String verifyCode = generateVerifyCode (verifySize); outputImage (w, h, outputFile, verifyCo De); return verifyCode;}/*** outputs the Random verification code image stream, return the verification code value ** @ param w * @ param h * @ param OS * @ param verifySize * @ return * @ throws IOException */public static String outputVerifyImage (int w, int h, OutputStream OS, int verifySize) throws IOException {String verifyCode = generateVerifyCode (verifySize); outputImage (w, h, OS, verifyCode); return verifyCode ;} /*** generate the image file with the specified Verification Code ** @ param w * @ param h * @ param o UtputFile * @ param code * @ throws IOException */public static void outputImage (int w, int h, File outputFile, String code) throws IOException {if (outputFile = null) {return;} File dir = outputFile. getParentFile (); if (! Dir. exists () {dir. mkdirs ();} try {outputFile. createNewFile (); FileOutputStream fos = new FileOutputStream (outputFile); outputImage (w, h, fos, code); fos. close () ;}catch (IOException e) {throw e ;}} /*** output the specified Verification code image stream ** @ param w * @ param h * @ param OS * @ param code * @ throws IOException */public static void outputImage (int w, int h, OutputStream OS, String code) throws IOException {int verifySize = code. Length (); BufferedImage image = new BufferedImage (w, h, BufferedImage. TYPE_INT_RGB); Random rand = new Random (); Graphics2D g2 = image. createGraphics (); g2.setRenderingHint (RenderingHints. KEY_ANTIALIASING, RenderingHints. VALUE_ANTIALIAS_ON); Color [] colors = new Color [5]; Color [] colorSpaces = new Color [] {Color. WHITE, Color. CYAN, Color. GRAY, Color. LIGHT_GRAY, Color. MAGENTA, Color. ORANGE, Color. PINK, Co Lor. YELLOW}; float [] fractions = new float [colors. length]; for (int I = 0; I <colors. length; I ++) {colors [I] = colorSpaces [rand. nextInt (colorSpaces. length)]; fractions [I] = rand. nextFloat ();} Arrays. sort (fractions); g2.setColor (Color. GRAY); // set the border Color g2.fillRect (0, 0, w, h); Color c = getRandColor (200,250); g2.setColor (c); // set the background Color g2.fillRect (0, 2, w, h-4); // draw the interference line Random random = new Random (); g2.setColo R (getRandColor (160,200); // set the line color for (int I = 0; I <20; I ++) {int x = random. nextInt (w-1); int y = random. nextInt (h-1); int xl = random. nextInt (6) + 1; int yl = random. nextInt (12) + 1; g2.drawLine (x, y, x + xl + 40, y + yl + 20);} // Add the noise float yawpRate = 0.05f; // noise rate int area = (int) (yawpRate * w * h); for (int I = 0; I <area; I ++) {int x = random. nextInt (w); int y = random. nextInt (h); int rgb = GetRandomIntColor (); image. setRGB (x, y, rgb);} shear (g2, w, h, c); // distort the image g2.setColor (getRandColor (100,160 )); int fontSize = h-4; Font font = new Font ("Algerian", Font. ITALIC, fontSize); g2.setFont (font); char [] chars = code. toCharArray (); for (int I = 0; I <verifySize; I ++) {AffineTransform affine = new AffineTransform (); affine. setToRotation (Math. PI/4 * rand. nextDouble () * (rand. nextBoolean ()? 1:-1), (w/verifySize) * I + fontSize/2, h/2); g2.setTransform (affine); g2.drawChars (chars, I, 1, (w-10)/verifySize) * I + 5, h/2 + fontSize/2-10);} g2.dispose (); ImageIO. write (image, "jpg", OS );} /*** obtain the Color by numerical value * @ param fc * @ param bc * @ return */private static Color getRandColor (int fc, int bc) {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);}/*** get a random Color * @ return */private static int getRandomIntColor () {int [] rgb = getRandomRgb (); int color = 0; for (int c: rgb) {color = color <8; color = color | c ;} return color;}/*** get a random three-digit color value * @ return */private static int [] getRandomRgb () {int [] rgb = new int [3]; for (int I = 0; I <3; I ++) {rgb [I] = random. nextInt (255);} return rgb ;} /*** distort the image * @ param g * @ param w1 * @ param h1 * @ param color */private static void shear (Graphics g, int w1, int h1, color color) {shearX (g, w1, h1, color); shearY (g, w1, h1, color );} /*** distort image X * @ param g * @ param w1 * @ param h1 * @ param color */private static void shearX (Graphics g, int w1, int h1, Color color) {int period = random. nextInt (2); boolean borderGap = true; int frames = 1; int phase = random. nextInt (2); for (int I = 0; I
2. display the path of the Verification Code image generated in the specified folder in the project on the logon page, and save the string returned by the generated verification code in a string variable, suppose it is stored in variable;
3. When a user logs on, obtain the verification code value entered by the user in the login input box and save it in a string-type variable. Suppose it is saved in variable B;
4. Compare variable A and variable B using the equals () method. If A. equals (B), the verification code entered by the user is correct; otherwise, the entered verification code is incorrect;