欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年04月27日 10:11:46   投稿:lqh  
這篇文章主要介紹了java仿Servlet生成驗(yàn)證碼實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下

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

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

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

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)效果圖

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

相關(guān)文章

  • 淺談Hibernate中的三種數(shù)據(jù)狀態(tài)(臨時(shí)、持久、游離)

    淺談Hibernate中的三種數(shù)據(jù)狀態(tài)(臨時(shí)、持久、游離)

    下面小編就為大家?guī)硪黄獪\談Hibernate中的三種數(shù)據(jù)狀態(tài)(臨時(shí)、持久、游離)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • ZooKeeper官方文檔之Java案例解讀

    ZooKeeper官方文檔之Java案例解讀

    ZooKeeper官方提供了一個(gè)Java監(jiān)聽的例子,本文是我對(duì)該例子的學(xué)習(xí)筆記??梢宰鰹閹椭斫獯死拥馁Y料,有需要的朋友可以借鑒參考下
    2022-01-01
  • spring cloud gateway如何獲取請(qǐng)求的真實(shí)地址

    spring cloud gateway如何獲取請(qǐng)求的真實(shí)地址

    這篇文章主要介紹了spring cloud gateway如何獲取請(qǐng)求的真實(shí)地址問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • 深入淺出學(xué)習(xí)AQS組件

    深入淺出學(xué)習(xí)AQS組件

    AQS ( AbstractQueuedSynchronizer)是一個(gè)用來構(gòu)建鎖和同步器的框架,使用AQS能簡單且高效地構(gòu)造出應(yīng)用廣泛的大量的同步器,下面小編和大家來一起學(xué)習(xí)一下吧
    2019-05-05
  • SpringBoot整合Mybatis簡單實(shí)現(xiàn)增刪改查

    SpringBoot整合Mybatis簡單實(shí)現(xiàn)增刪改查

    這篇文章主要介紹了SpringBoot整合Mybatis簡單實(shí)現(xiàn)增刪改查,文章為圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • SpringBoot+Redis布隆過濾器防惡意流量擊穿緩存

    SpringBoot+Redis布隆過濾器防惡意流量擊穿緩存

    本文主要介紹了SpringBoot+Redis布隆過濾器防惡意流量擊穿緩存,文中根據(jù)實(shí)例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • MyBatis在mapper中傳遞參數(shù)的四種方式

    MyBatis在mapper中傳遞參數(shù)的四種方式

    MyBatis是一個(gè)持久層框架,它提供了一種將數(shù)據(jù)庫操作與Java對(duì)象之間的映射關(guān)系進(jìn)行配置的方式,在MyBatis中,Mapper是用于定義數(shù)據(jù)庫操作的接口,而參數(shù)傳遞則是通過Mapper接口的方法來實(shí)現(xiàn)的,本文給大家介紹了MyBatis在mapper中傳遞參數(shù)的四種方式,需要的朋友可以參考下
    2024-03-03
  • Java集合Iterator迭代的實(shí)現(xiàn)方法

    Java集合Iterator迭代的實(shí)現(xiàn)方法

    這篇文章主要介紹了Java集合Iterator迭代接口的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒家,對(duì)Java 結(jié)合iterator知識(shí)感興趣的朋友一起看看吧
    2016-08-08
  • Spring?Cloud?Alibaba負(fù)載均衡實(shí)現(xiàn)方式

    Spring?Cloud?Alibaba負(fù)載均衡實(shí)現(xiàn)方式

    這篇文章主要為大家介紹了Spring?Cloud?Alibaba負(fù)載均衡實(shí)現(xiàn)方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Jenkins集成SonarQube的方法詳解

    Jenkins集成SonarQube的方法詳解

    這篇文章主要介紹了Jenkins集成SonarQube的方法詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09

最新評(píng)論