国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - java仿Servlet生成驗證碼實例詳解

java仿Servlet生成驗證碼實例詳解

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

這篇文章主要介紹了java仿Servlet生成驗證碼實例詳解的相關資料,需要的朋友可以參考下

java仿Servlet生成驗證碼實例詳解

實現原理:使用BufferedImage對象的Graphics來進行繪制,然后輸出成一張圖片進行保存

實現代碼及詳解:

?
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);
 
    //繪制隨機干擾線
    int total = 100;
    drawRandLine(gs,total);
 
    //繪制驗證碼
    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);
      //設置隨機顏色
      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); 
  }

最終實現效果圖

java仿Servlet生成驗證碼實例詳解

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产免费av网站 | 亚洲一区二区av | 国产成人高清精品免费5388 | 国产免费视频在线 | 人人澡人人透人人爽 | 吴梦梦到粉丝家实战华中在线观看 | 亚洲精品国产一区 | 福利在线看 | 日本中文字幕免费 | 亚洲视频在线观看 | 91久久综合亚洲鲁鲁五月天 | 日韩欧美h | 久久99精品久久久久久国产越南 | 欧美午夜精品久久久久久人妖 | 九九九久久国产免费 | 精品无码久久久久国产 | 免费一级在线视频 | 国产成人综合一区二区三区 | 欧美精品影院 | 欧美精品一区在线 | 91精品久久久久久久久久 | 樱桃小丸子在线观看 | 一级片国产 | 欧美视频在线观看不卡 | 国产情侣91| 国产亚洲精品久久久久动 | 91亚洲成人 | 日韩av一区二区在线观看 | 黄色在线免费观看 | 国产精品久久久久久久久久久新郎 | 日韩成人在线网站 | 亚洲第一福利视频 | 久久丝袜诱惑 | 亚洲第一成年人网站 | 嫩草精品| 91久久精品日日躁夜夜躁国产 | 日韩三级视频 | 亚洲成人一区二区 | 亚洲欧美一区二区三区四区 | 亚洲成人一区二区三区 | 中文字幕av一区二区 |