Java generated picture Verification code sample Code _java

Source: Internet
Author: User

  This example for you to share the Java Image Verification code to achieve the specific code for your reference, the specific content as follows

Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics;
Import Java.awt.image.BufferedImage;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.OutputStream;
Import Java.util.Random;
Import Javax.imageio.ImageIO; * * Steps: * 1. Picture frame * 2. Draw background * 3. Reprint * 4. Draw the disturbing line */public final class Imageutil {//Authentication code character set private static final ch Ar[] chars = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J
  ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z '};
  Character number private static final int SIZE = 4;
  Number of jamming lines private static final int LINES = 5;
  Width private static final int width = 80;
  Highly private static final int height = 40;
 
  Font size private static final int font_size = 30; /** * Generate random verification codes and pictures * Object[0] Authenticode strings String * object[1] Verify code picture bufferedimage/public static object[] Createima GE () {stringbuffer sb = new StringbuffER ();
    1. Create a blank picture bufferedimage image = New BufferedImage (WIDTH, HEIGHT, Bufferedimage.type_int_rgb);
    2. Get picture brush Graphics graphic = image.getgraphics ();
    3. Set the brush color Graphic.setcolor (color.light_gray);
    4. Draw rectangular Background graphic.fillrect (0, 0, WIDTH, HEIGHT);
    5. Draw random characters Random ran = new Random ();
      for (int i = 0; I <SIZE; i++) {//Fetch random character index int n = ran.nextint (chars.length);
      Set the random color Graphic.setcolor (Getrandomcolor ());
      Sets the font size graphic.setfont (new font (NULL, Font.Bold + font.italic, font_size));
      Reprint character Graphic.drawstring (Chars[n] + "", I * width/size, HEIGHT/2);
    Record character Sb.append (Chars[n]);
      //6. Draw interference line for (int i = 0; i < LINES i++) {//Set random color Graphic.setcolor (Getrandomcolor ()); Random Draw Line Graphic.drawline (Ran.nextint (width), ran.nextint (height), ran.nextint (width), ran.nextint (height))
    ;
}//7. Return Verification Code and picture    return new object[]{sb.tostring (), image};
    /** * Randomly take color/public static color Getrandomcolor () {Random ran = new Random ();
    Color color = new Color (ran.nextint (256), Ran.nextint (256), Ran.nextint (256));
  return color;
    public static void Main (string[] args) throws IOException {object[] Objs = CreateImage ();
    BufferedImage image = (bufferedimage) objs[1];
    Picture format can be customized, Java to PNG picture display more clearly, the output path can be customized outputstream OS = new FileOutputStream ("D:/x.png");
    Note that the picture format matches imageio.write (image, "PNG", OS) to create a format;
  Os.close (); }
 
}

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.

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.