一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達(dá)式|

服務(wù)器之家 - 編程語言 - JAVA教程 - java仿Servlet生成驗(yàn)證碼實(shí)例詳解

java仿Servlet生成驗(yàn)證碼實(shí)例詳解

2020-09-20 13:47Java之家 JAVA教程

這篇文章主要介紹了java仿Servlet生成驗(yàn)證碼實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下

java仿Servlet生成驗(yàn)證碼實(shí)例詳解

實(shí)現(xiàn)原理:使用BufferedImage對象的Graphics來進(jìn)行繪制,然后輸出成一張圖片進(jìn)行保存

實(shí)現(xiàn)代碼及詳解:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class validateCode{
 
  private static Random rand = new Random();
 
  public static void main(String[] args){
    int val1 = rand.nextInt(9);
    int val2 = rand.nextInt(9);
    int val3 = rand.nextInt(9);
    int val4 = rand.nextInt(9);
    String val = val1 + " " + val2 + " " + val3 + " " + val4'
    BufferedImage buf = drawImage(val);
    //將最終的圖片保存到D://cheng.png下
    ImageIO.write(buf,"png",new File("D://cheng.png");
  }
 
  public static BufferedImage drawImage(String code){
    int height = 30;
    int width = 60;
    BufferedImage buf = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
    Graphics2D gs = buf.createGraphics();
    gs.setBackground(Color.black);
    gs.drawRect(0,0,width,height);
 
    //繪制隨機(jī)干擾線
    int total = 100;
    drawRandLine(gs,total);
 
    //繪制驗(yàn)證碼
    Font font = new Font("行楷",Font.BOLD,20);
    gs.setFont(font);
    gs.setColor(getRandColor(155,255));
    gs.drawString(code,5,20);
 
    return buf;
  }
 
  public static void drawRandLine(Graphics2D gs,int total){
 
    for(int i=0; i<total; i++){
      int x1 = rand.nextInt(width);
      int x2 = rand.nextInt(width);
      int y1 = rand.nextInt(height);
      int y2 = rand.nextInt(height);
      //設(shè)置隨機(jī)顏色
      gs.setColor(getRandColor(0,155));
      gs.drawLine(x1,y1,x2,y2);
    
  }
 
  public static Color getRandColor(int from,int to){
 
    int r = from + rand.nextInt(to-from);
    int g = from + rand.nextInt(to-from);
    int b = from + rand.nextInt(to-from);
    return new Color(r,g,b); 
  }

最終實(shí)現(xiàn)效果圖

java仿Servlet生成驗(yàn)證碼實(shí)例詳解

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

原文鏈接:http://blog.csdn.net/qq_27905183/article/details/69945770

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 歪歪私人影院成人毛片 | 青青精品 | 国产福利视频一区二区微拍 | 九九精品99久久久香蕉 | 私人影院免费 | 国产福利免费看 | 日韩伦理在线观看 | 欧美s级人做人爱c视频 | 成 人免费va视频 | 亚洲精品资源 | 小小水蜜桃视频高清在线观看免费 | 日本在线小视频 | 国产美女极品免费视频 | 免费一区二区 | 欧洲美女女同 | 福利视频导航大全 | 国产欧美日韩高清专区ho | 亚洲天堂影院在线观看 | 国产午夜亚洲精品一区网站 | 色字当头| 成人久久久 | 校服下的白嫩小乳尖h1v1 | 日本sss| avav一区| 波多野结衣中文字幕在线 | 秋霞午夜视频 | 俄罗斯烧性春三级k8播放 | 亚洲v成人天堂影视 | 日本中文字幕在线视频 | 免费观看国产视频 | 国产无限 | h黑寡妇一级毛片 | 亚洲男人天堂网站 | 深夜影院深a久久 | 欧美午夜视频一区二区三区 | 成年无限观看onlyfans | 成人免费国产欧美日韩你懂的 | 国产亚洲精品第一综合另类 | 亚洲va国产日韩欧美精品色婷婷 | 欧美大陆日韩一区二区三区 | 日本男男漫画 |