Using Java to generate a letter-verification code _java

Source: Internet
Author: User
Tags int size

This example for you to share the Java generated letter verification code for your reference, the specific content as follows

Import Java.awt.BasicStroke;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics2D;
Import Java.awt.image.BufferedImage;
Import java.io.IOException;
Import Java.io.OutputStream;

Import Java.util.Random;


Import Javax.imageio.ImageIO;
  public class Verifycode {//Picture width high private int w=70;
  private int h=35;
  Private Random r=new Random ();
  Private string[] fontnames={"song Body", "XXFarEastFont-Arial", "bold", "Microsoft Ya Hei", "italics _gb2312"};
  Private String codes= "234567890qwertyuipasdfghjkzxcvbnmQWERTYUIPASDFGHJKZXCVBNM";
  Private color Bgcolor=new color (255,255,255);
  private String text;
    Generate random color private color randomcolor () {int red=r.nextint (150);
    int Green=r.nextint (150);
    int Blue=r.nextint (150);
    return new Color (red, green, blue);
    //Generate random Font private font randomfont () {int index=r.nextint (fontnames.length);
    String Fontname=fontnames[index]; int Style=r.nextint (4);//0 No 1 Bold 2 Italic 3 thick + oblique int size=r.nextint (5) +24; Generate random Font size 24~28 return new font (fontName, style, size);
    }//Interference line private void Drowline (BufferedImage image) {//Generate 4 interference line int num=4;
    Graphics2D bi= (graphics2d) image.getgraphics ();
      for (int i = 0; i < num; i++) {int x1=r.nextint (w);
      int X2=r.nextint (w);
      int Y1=r.nextint (h);
      int Y2=r.nextint (h);
      Bi.setstroke (New Basicstroke (1.5F));
    Bi.setcolor (Color.Blue);//Interference Line Color bi.drawline (x1,y1,x2,y2);
    }///randomly generate a character private char Randomchar () {int index=r.nextint (codes.length ());
  Return Codes.charat (index); }//Picture buffer private BufferedImage createimage () {bufferedimage image=new bufferedimage (W, H, Bufferedimage.type_int_
    BGR);
    Graphics2D bi= (graphics2d) image.getgraphics ();
    Bi.setcolor (This.bgcolor);
    Bi.fillrect (0, 0, W, h);
  return image;
    //Generate Picture Public bufferedimage getImage () {bufferedimage image=createimage ();
    Graphics2D bi= (graphics2d) image.getgraphics ();
    StringBuilder sb=new StringBuilder (); for (int i = 0; I <4;
      i++) {String String=randomchar () + "";
      Sb.append (string);
      Each character occupies a picture of 1/4 wide float X=I*1.0F*W/4;
      Random font format bi.setfont (Randomfont ());
      Bi.setcolor (Randomcolor ());
    Write the words in the appropriate place (h-6 refers to the picture from the bottom 6 height) bi.drawstring (string, x, h-6);
    } this.text=sb.tostring ();
    Drowline (image);
  return image;
  //Returns the resulting font public String GetText () {return text; ///write the picture to the specified location public static void output (BufferedImage image,outputstream out) {try {imageio.write (image, "J
    PG ", out);
    catch (IOException e) {e.printstacktrace ();

 }
  }
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.