Java Implementation Simple Verification code (to be enhanced)

Source: Internet
Author: User
Tags getcolor set background

Package com.xxx;

Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics2D;
Import Java.awt.image.BufferedImage;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;

Import Javax.imageio.ImageIO;

Import Org.junit.Test;

public class Identifyingcode {

Private final int WIDTH = 70;
Private final int HEIGHT = 30;
Private final byte[] Background_color = new byte[]{127, 127, 127};
Private final String Font_style = "Microsoft Jas Black";
Private final char[] Idetifying_code = new char[]{' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d '};
Private final int code_length= 4;
Private final int font_size = 17;

Generate a verification code map
Public BufferedImage Createidentifyingcode () {
StringBuilder sb = new StringBuilder ();

BufferedImage image = New BufferedImage (WIDTH, HEIGHT, BUFFEREDIMAGE.TYPE_3BYTE_BGR);
Get brushes
Graphics2D g = image.creategraphics ();
Get the color of the previous brush
Color color = G.getcolor ();

Draw a picture border
This.drawimagecheek (g);
This.drawcamouflageline (g, 8);
Set Background color
G.setbackground (New Color (background_color[0]+128, background_color[1]+128, background_color[2]+128));
Set font size and style
G.setfont (New Font (Font_style, Font.plain, (int) (this). Font_size)) {
Private static final long serialversionuid = 1L;});

Adding validation characters to the diagram
for (int i=0; i<code_length; i++) {
Set Brush color
G.setcolor (This.randomcolor ());
Get random characters
Char charcode = idetifying_code[this.randomnumber (0, idetifying_code.length-1)];
Sb.append (CharCode);
Write characters to a picture
System.out.println (SB);
G.rotate (This.randomnumber (45, 135));
g.DrawString ("" +charcode, width/5* (i+1) -5, (this. font_size) +this.randomnumber (0, this. FONT_SIZE/2));

}

try {
Imageio.write (image, "JPEG", New FileOutputStream ("D:/11.jpeg"));
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return image;
}

/**
* Generate a color according to the random three colors
*
* @return Color
*/
Public Color Randomcolor () {
int min = 0;
int max = 255;
int red = randomnumber (min, max);
int green = randomnumber (min, max);
int yellow = randomnumber (min, max);
return new Color (red, green, yellow);
}

public void Drawcamouflageline (graphics2d g, int quantity) {
Color Oldcolor = G.getcolor ();
for (int i=0; i<quantity; i++) {
G.setcolor (This.randomcolor ());
G.drawline (This.randomnumber (0, this. WIDTH), This.randomnumber (0, this. HEIGHT), This.randomnumber (0, this. WIDTH), This.randomnumber (0, this. HEIGHT));
}
G.setcolor (Oldcolor);
}

/**
* Draw inside border of picture
*
* @param g brushes
*/
public void Drawimagecheek (graphics2d g) {
Color Oldcolor = G.getcolor ();
G.setcolor (New Color (255, 255, 255));
G.drawline (0, 0, this. WIDTH-2, 0);
G.drawline (0, 0, 0, this. HEIGHT-2);
G.drawline (0, this. HEIGHT-2, this. WIDTH-2, this. HEIGHT-2);
G.drawline (this. WIDTH-2, 0, this. WIDTH-2, this. HEIGHT-2);
G.setcolor (Oldcolor);
}

public int randomnumber (int min, int max) {
return (int) ((Math.random () * (max-min+1)) +min);

}

public static void Main (string[] args) {
New Identifyingcode (). Createidentifyingcode ();
}
}

One of the issues to be solved: the background is not set correctly

To be optimized: validation codes in the verification diagram need to be rotated to increase program recognition difficulty

This is the effect of the present

Java Implementation Simple Verification code (to be enhanced)

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.