Java code verification tools

 

PIN code means the code as follows:

 

javax.imageio.ImageIO Import; 
Import in java.awt *;.
Import java.awt.image.BufferedImage;
Import java.awt.image.RenderedImage;
Import java.io.FileOutputStream;
Import a java.io.OutputStream;
Import Classes in java.util .HashMap;
Import a java.util.Map;
Import java.util.Random;

/ **
* @title codes tools
* @author Boat
* /
public class ImageUtil {

Private static int width = 90; // definition of the image width
private static int height = 30; // definition of the image height

// code verifiers. 5 a set of
/ * private static int codeCount = 5 ; the number of codes displayed on the image // definition of
private static int xx = 13; * /

// four codes set
private static int codeCount = 4; // number of codes defined on the image display
private static int xx = 15;

private static int codeY = 23;
private static int fontHeight = 22;

private static char[] codeSequence = {'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','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', '0', '1 ',' 2 ',' 3 ',' 4 ',' 5 ',' 6 ',' 7 ',' 8 ',' 9 '}; // definition of the image buffer public static the Map <String, Object> generateCodeAndPic () { * / * @return * * codePic of codes generated BufferedImage object * to generate a verification code code * generate a map set

/ *








= New new buffImg the BufferedImage the BufferedImage (width, height, BufferedImage.TYPE_INT_RGB);
// buffImg.createGraphics the Graphics2D Gd = ();
// the Graphics2D Gd = (the Graphics2D) buffImg.getGraphics ();
Graphics buffImg.getGraphics Gd = ();

/ / class to create a random number generator
the random random new new = the random ();

// fill the image white
gd.setColor (Color.WHITE);
gd.fillRect (0, 0, width, height);

// create font, the font size should be set according to the height of the picture.
New new font font = font ( "Arial", Font.BOLD, FontHeight);

// set the font.
gd.setFont (font);

// draw the border.
//gd.setColor(Color.LIGHT_GRAY);
gd.setColor (getRandomColor ());
gd.drawRect (0, 0, width -. 1, height -. 1);

// randomly generated interference line 40, the authentication code so that the image is not easy to be detected other programs.
gd.setColor (getRandomColor ());

for (int I = 0; I <30; I ++) {
int X = random.nextInt (width);
int random.nextInt Y = (height);
int random.nextInt XL = ( 12 is);
int = YL random.nextInt (12 is);
gd.drawLine (X, Y, X + XL, YL + Y);
}

// randomCode for holding randomly generated codes to authenticate the user upon login.
= New new randomCode the StringBuffer the StringBuffer ();

// number codeCount randomly generated codes.
for (int I = 0; I <codeCount; I ++) {

// get the digital codes generated at random.
String.valueOf code = String (codeSequence [random.nextInt (codeSequence.length)]);

// color code will be randomly generated into the image drawing.
gd.setColor (getRandomColor ());

gd.drawString (code, (I +. 1) * XX,, codeY);

// four combined random number generated.
randomCode.append (code);
}
the Map <String, Object> = new new the HashMap Map <String, Object> ();
// store codes
map.put ( "code", randomCode);
// put the generated codes BufferedImage Object
map.put ( "codePic", buffImg);
return Map;
}

/ **
* color randomly
* /
public static color getRandomColor () {
the random new new RAN = the random ();
int red = ran.nextInt(256);
int green = ran.nextInt(256);
int blue = ran.nextInt(256);
Color color = new Color(red,green,blue);
return color;
}

// 获取验证码
public static Map<String,Object> getCheckCodePic(){
return ImageUtil.generateCodeAndPic();
}

public static void main(String[] args) throws Exception {
//创建文件输出流对象
OutputStream out = new FileOutputStream("d://"+System.currentTimeMillis()+".jpg");
Map<String,Object> map = ImageUtil.generateCodeAndPic();
ImageIO.write((RenderedImage) map.get("codePic"), "jpeg", out);
System.out.println ( "Code verification values:" + as map.get ( "code"));
}

}


Copy the code directly copied into the project using!


Guess you like

Origin www.cnblogs.com/M87-A/p/12586742.html