JAVA--驗證碼

來源:互聯網
上載者:User

標籤:驗證碼   private   public   伺服器   random   

首先,為什麼需要驗證碼?

    簡單說,一是不使得註冊過度,造成伺服器因為容量問題崩潰;二是為了防止暴力破解。


使用的工具:MyEclipse


源碼:

public class Demo2 {

@Test

public void fun1() throws FileNotFoundException, IOException {

VerifyCode vC = new VerifyCode();

BufferedImage bfdi = vC.getImage();//隨機

System.out.println(vC.getText());//列印圖片文本

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

}

}

------------對於VerifyCode------------------

public class VerifyCode {

private int w = 140;

private int h = 70;

 private Random r = new Random();

private String[] fontNames  = {"宋體","黑體", "華文楷體",  "微軟雅黑", "楷體_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 StringBuilder();

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);

}

}

可視效果:



參考:傳智播客視頻講解

本文出自 “11773640” 部落格,請務必保留此出處http://11783640.blog.51cto.com/11773640/1866925

JAVA--驗證碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.