Java--verification Code

Source: Internet
Author: User

first, Why do I need a verification code?

Simply put, one is not to make the registration excessive, causing the server because of the capacity problem crashes, and the second is to prevent violent cracking.


Tools to Use: MyEclipse


Source:

public class Demo2 {

@Test

public void Fun1 () throws filenotfoundexception, IOException {

Verifycode VC = new Verifycode ();

BufferedImage BFDI = vC. GetImage ();//random

System.out.println (vC. GetText ());//print Picture Text

Verifycode.output (bfdi, new FileOutputStream ("b.jpg"));

}

}

------------for verifycode------------------

public class Verifycode {

private int w = 140;

private int h = 70;

private Random r = new Random ();

Private string[ ] fontnames = {"arial", "black body", "chinese italic", "microsoft jas", "italics _gb2312" };

Private String codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";

Private color BgColor = new color (255, 255, 255);

Private String text;

Private Color Randomcolor () {

int red = R.nextint (150);

int green = R.nextint (150);

int blue = R.nextint (150);

return new Color (red, green, blue);

}

Private Font Randomfont () {

int index = R.nextint (fontnames.length);

String FontName = fontnames[index];

int style = R.nextint (4);

int size = R.nextint (5) + 24;

return new Font (fontname, style, size);

}

private void DrawLine (bufferedimage Image) {

int num = 3;

graphics2d g2 = (graphics2d) image.getgraphics ();

for (int i = 0; i < num; i++) {

int x1 = R.nextint (w);

int y1 = R.nextint (h);

int x2 = R.nextint (w);

int y2 = R.nextint (h);

G2.setstroke (new Basicstroke (1.5F));

G2.setcolor (color.blue);

G2.drawline (x1, y1, x2, y2);

}

}

Private Char Randomchar () {

int index = R.nextint (codes.length ());

return Codes.charat (index);

}

Private BufferedImage createimage () {

BufferedImage image = new BufferedImage (w, h, bufferedimage.type_int_rgb);

graphics2d g2 = (graphics2d) image.getgraphics ();

G2.setcolor (this.bgcolor);

G2.fillrect (0, 0, w, h);

return image;

}

Public BufferedImage getImage () {

BufferedImage image = CreateImage ();

graphics2d g2 = (graphics2d) image.getgraphics ();

StringBuilder sb = new Strin gbuilder ();

for (int i = 0; i < 4; i++) {

String s = Randomchar () + "";

Sb.append (s);

float x = i * 1.0F * w/4;

G2.setfont (randomfont ());

G2.setcolor (randomcolor ());

G2.drawstring (s, x, h-5);

}

This.text = sb.tostring ();

DrawLine (image);

Return image;

}

Public String GetText () {

Return text;

}

public static void Output (bufferedimage image, outputstream Out)

Throws IOException {

Imageio.write (image, "JPEG", out);

}

}

Visual Effects:



Reference: Preach Wisdom Podcast Video tutorial

This article is from the "11773640" blog, please be sure to keep this source http://11783640.blog.51cto.com/11773640/1866925

Java--verification Code

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.