Java Authenticode generates specific code _java

Source: Internet
Author: User
Tags set background stringbuffer

Examples of this article for you to share the Java validation code generated by the sample code for your reference, the details are as follows

Package Com.gonvan.component.captcha;
Import java.awt.*;
Import Java.awt.image.BufferedImage;
Import java.io.IOException;
Import Java.util.HashMap;
Import Java.util.Map;
 
Import Java.util.Random;
Import Javax.imageio.ImageIO;
Import Javax.servlet.ServletOutputStream;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
 
Import javax.servlet.http.HttpSession;
 /** * Created by Yuerzm on 2016/3/14. * * Public class Captchafactory {private static final char[] Code_sequence = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345678
  9 ". ToCharArray ();
  private static final int default_width = 60;
  private static final int default_height = 20;
  private static final int default_code_len = 4;
  private static final int default_code_x = 13;
  private static final int default_code_y = 16;
  private static final int default_font_size = 18;
  private static final String default_font_family = "Times New Roman"; private static Captchafactory instance = new Captchafactory ();            private int width = default_width;            Defines the picture's width private int height = default_height;           Defines the height private int length = Default_code_len of the picture;            Define the number of validation codes displayed on the picture private int xx = default_code_x;            Define the picture to display the verification code x coordinates private int yy = default_code_y;          Define the picture to display the verification code y-coordinate private int fontsize = Default_font_size;         Defines the font size of the display verification code on the picture private String fontFamily = default_font_family;
  Define the number of validation codes displayed on the picture Private Captchafactory () {} public static captchafactory getinstance () {return instance;  /** * Configuration Width High * * @param w * @param h * @return/public captchafactory configwidthandheight (int
    W, int h) {instance.width = W;
    Instance.height = h;
  return instance; /** * Configuration Coordinates * * @param x * @param y
   * @return */public captchafactory configxy (int x, int y) {instance.xx = x;
    Instance.yy = y;
  return instance;
    /** * Configure font Size * * @param fontsize * @return/public captchafactory configfontsize (int fontsize) {
    Instance.fontsize = fontsize;
  return instance; /** * Configure font * * @param fontFamily * @return/Public captchafactory configfontsize (String fontfamil
    Y) {instance.fontfamily = fontFamily;
  return instance; public void Write (HttpServletRequest request, httpservletresponse response) throws IOException {//Save four-bit number of Authenticode to
    Session.
    Map captcha = Generate ();
    String randomcode = (string) captcha.get ("Captchacode");
 
    BufferedImage buffimg = (bufferedimage) captcha.get ("captchaimg");
    HttpSession session = Request.getsession ();
 
    Session.setattribute ("code", Randomcode);
    Disables image caching.
    Response.setheader ("Pragma", "No-cache"); Response.setheader ("Cache-control", "No-caChe ");
    Response.setdateheader ("Expires", 0);
 
    Response.setcontenttype ("Image/jpeg");
    Outputs the image to the servlet output stream.
    Servletoutputstream outputstream = Response.getoutputstream ();
    Imageio.write (buffimg, "JPEG", outputstream);
  Outputstream.close (); Public map<string, object> generate () throws IOException {//define image buffer BufferedImage buffimg = new B
    Ufferedimage (width, height, bufferedimage.type_int_rgb);
    Graphics gd = Buffimg.getgraphics ();
    Set Background color Gd.setcolor (Getrandcolor (200, 250));
    Gd.fillrect (0, 0, width, height);
    Set the font, the size of the font should be based on the height of the picture.
 
    Gd.setfont (New Font (fontFamily, Font.plain, FontSize));
 
    Create a random number generator class Random Random = new Random ();
    Randomly generated 40 lines of interference, so that the image of the authentication code is not easily detected by other programs.
    Gd.setcolor (Getrandcolor (160, 200));
      for (int i = 0; i < 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;
    Randomly produces a length verification code.
      for (int i = 0; i < length; i++) {//Get the randomly generated CAPTCHA number.
      String code = string.valueof (Code_sequence[random.nextint (36)]);
      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 (110);
      Green = Random.nextint (110);
 
      Blue = Random.nextint (110);
      Draws the validation code into the image with a randomly generated color.
      Gd.setcolor (New Color (red +, green +, Blue + 20));
 
      Gd.drawstring (Code, I * xx + 6, yy);
      The resulting random numbers are grouped together.
    Randomcode.append (code);
    } map<string, object> retval = new hashmap<> ();
    Retval.put ("Captchacode", randomcode.tostring ());
    Retval.put ("Captchaimg", buffimg);
  return retval; /** * A given range of random color * * @param FC * Minimum value * @param BC * Maximum * @return color */privat e 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); }
}

The above is the entire content of this article, I hope to help you learn.

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.