Java generated picture Verification Code instance Code _java

Source: Internet
Author: User
Tags stringbuffer

About the Java Picture Verification Code article recently updated a lot, to help you master the Java Verification Code generation technology, the following for you to share the Java generated picture verification code The simplest way for everyone's reference.

Now the industry in the customization system will take into account machine registration, now the most effective way is input validation. There are many ways to verify today:

first, the problem verification, in fact, is also a picture validation, in the picture generated problems, and then input the box to enter the answer.

second, the picture verification, the input picture displays the text information.

Third, SMS verification, more complex, users do not like.

Four, there is the latest verification of Baidu Way. generate text on the picture, then a text click box, select the text you see in the validation picture.
Now share the code for the Java Build Verification code, which is a basic code. Can be used directly in learning, and you can add logical validation yourself if you need more complex validation.

@Controller public class Imgverifycode extends HttpServlet {/** * */private static final long serialversion
 
  UID = 1L;
   /** * Verification Code picture width.
 
  * Private int width = 70;
   /** * Verify the height of the code picture.
 
  * Private int height = 30;
 
  /** * Verification code number of characters/private int codecount = 5;
 
  /** * XX/private int xx = 0;
 
  /** * Font Height * * private int fontheight;
 
  /** * Codey * * private int codey; /** * codesequence * * string[] codesequence = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "a", "B", "B", "C",
 
  "C", "D", "D", "E", "E", "F", "F", "G", "G", "Z", "X", "Q", "V"}; /** * Initialize Verify picture properties/public void init () throws Servletexception {//Get initial information/width String strwid from Web.xml
    th =width+ "";
    Height String strheight = height+ "";
 
    Number of characters String Strcodecount = codecount+ ""; Converts the configured information to a numeric try {if (strwidth!= null && strwidth.length ()!= 0) {width = Integer.parseint ( Strwidth);
      } if (strheight!= null && strheight.length ()!= 0) {height = Integer.parseint (strheight); } if (Strcodecount!= null && strcodecount.length ()!= 0) {codecount = Integer.parseint (strcod
      Ecount);
    } catch (NumberFormatException e) {e.printstacktrace (); xx = width/(codecount + 2);   Generate random number of horizontal distance fontheight = height-12;      Generate the number of random numbers height Codey = height-8; Generates a random number of vertical distances} protected String images (httpservletrequest req, HttpServletResponse resp) throws Servletexc
    eption,ioexception {init ();
    Define image buffer BufferedImage buffimg = new BufferedImage (width, height, bufferedimage.type_int_rgb);
 
    Graphics2D gd = Buffimg.creategraphics ();
 
    Create a random number generator class Random Random = new Random ();
    Fill the image with a white gd.setcolor (color.white);
 
    Gd.fillrect (0, 0, width, height);
    To create a font, the size of the font should be based on the height of the picture. Font font = new Font ("Fixedsys", Font.plain, fontHeight);
    Sets the font.
 
    Gd.setfont (font);
    Draw a border.
    Gd.setcolor (Color.Black);
 
    Gd.drawrect (0, 0, width-1, height-1);
    Randomly generated 4 lines of interference, so that the image of the authentication code is not easily detected by other programs.
    Gd.setcolor (Color.Black);
      for (int i = 0; i < 4; i++) {int x = random.nextint (width);
      int y = random.nextint (height);
      int xl = Random.nextint (12);
      int yl = Random.nextint (12);
    Gd.drawline (x, y, X + xl, y + yl);
    The//Randomcode is used to save the randomly generated verification code so that the user can authenticate after logging in.
    StringBuffer Randomcode = new StringBuffer ();
 
    int red = 0, green = 0, blue = 0;
    A validation code that randomly generates CODECOUNT numbers.
      for (int i = 0; i < Codecount i++) {//Get a randomly generated captcha number.
      String Strrand = string.valueof (Codesequence[random.nextint (27)]);
      Produces a random color component to construct a color value, so that the color values of each digit of the output will be different.
      Red = Random.nextint (125);
      Green = Random.nextint (255);
 
      Blue = Random.nextint (200);
      Draws the validation code into the image with a randomly generated color.
      Gd.setcolor (New Color (red, green, blue)); Gd.drawstring (Strrand, (i + 1) *XX, Codey);
      The resulting four random numbers are grouped together.
    Randomcode.append (Strrand);
    ///Save the four-digit CAPTCHA to the session.
    HttpSession session = Req.getsession ();
 
    Session.setattribute ("Validatecode", randomcode.tostring ());
    Disables image caching.
    Resp.setheader ("Pragma", "No-cache");
    Resp.setheader ("Cache-control", "No-cache");
 
    Resp.setdateheader ("Expires", 0);
 
    Resp.setcontenttype ("Image/jpeg");
    Outputs the image to the servlet output stream.
    Servletoutputstream SOS = Resp.getoutputstream ();
    Imageio.write (buffimg, "JPEG", SOS);
    Sos.close ();
  return null; } }

This code is the basic way to generate validation pictures.

The above is the entire content of this article, I hope to help you learn, you can also view the previous articles for in-depth study.

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.