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

Java實現(xiàn)動態(tài)獲取圖片驗證碼的示例代碼

 更新時間:2019年08月16日 09:57:54   作者:楓止水  
這篇文章主要介紹了Java實現(xiàn)動態(tài)獲取圖片驗證碼的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

本文介紹了Java實現(xiàn)動態(tài)獲取圖片驗證碼的示例代碼,分享給大家,具體如下:

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Random;
 
 
public class ImgAuthCode {
 
  /**
   * 圖片的寬度
   */
  private int width = 330;
  /**
   * 圖片的高度
   */
  private int height = 40;
  /**
   * 驗證碼字符個數(shù)
   */
  private int codeCount = 5;
  /**
   * 驗證碼干擾線數(shù)
   */
  private int lineCount = 150;
  /**
   * 圖片驗證碼類型
   */
  private ImgCodeType imgCodeType = DEFAULT_TYPE;
  /**
   * 驗證碼
   */
  private String code = null;
  /**
   * 驗證碼圖片Buffer
   */
  private BufferedImage buffImg = null;
 
  private static final ImgCodeType DEFAULT_TYPE = ImgCodeType.ENANDNUMBER;
 
  private static char[] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R',
      'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
 
  public ImgAuthCode() {
    this.createCode();
  }
 
  /**
   * @param width 圖片寬
   * @param height 圖片高
   */
  public ImgAuthCode(int width, int height) {
    this.width = width;
    this.height = height;
    this.createCode();
  }
 
  /**
   * @param width   圖片寬
   * @param height  圖片高
   * @param codeCount 字符個數(shù)
   * @param lineCount 干擾線條數(shù)
   */
  public ImgAuthCode(int width, int height, int codeCount, int lineCount,ImgCodeType imgCodeType) {
    this.width = width;
    this.height = height;
    this.codeCount = codeCount;
    this.lineCount = lineCount;
    this.imgCodeType = imgCodeType;
    this.createCode();
  }
 
  public void createCode() {
    int x = 0, fontHeight = 0, codeY = 0;
    x = width / (codeCount + 2);// 每個字符的寬度
    fontHeight = height - 2;// 字體的高度
    codeY = height - 4;
 
    // 圖像buffer
    buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = buffImg.createGraphics();
    // 將圖像填充為白色
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, width, height);
    // 創(chuàng)建字體
//    ImgFontByte imgFont = new ImgFontByte();
//    Font font = imgFont.getFont(fontHeight);
    Font font=new Font("微軟雅黑",Font.PLAIN, fontHeight);
    g.setFont(font);
    drawRandomLine(g);
    // randomCode記錄隨機(jī)產(chǎn)生的驗證碼
    StringBuffer randomCode = new StringBuffer();
    // 隨機(jī)產(chǎn)生codeCount個字符的驗證碼。
    for (int i = 0; i < codeCount; i++) {
      String strRand = getRandomStr(imgCodeType);
      // 產(chǎn)生隨機(jī)的顏色值,讓輸出的每個字符的顏色值都將不同。
      g.setColor(getRandomColor());
      g.drawString(strRand, (i + 1) * x, codeY);
      // 將產(chǎn)生的四個隨機(jī)數(shù)組合在一起。
      randomCode.append(strRand);
    }
    this.code = randomCode.toString();
  }
 
  private void drawRandomLine(Graphics g){
    //干擾線
    for (int i = 0; i < lineCount; i++) {
      int xs = new Random().nextInt(width);
      int ys = new Random().nextInt(height);
      int xe = xs + new Random().nextInt(width / 8);
      int ye = ys + new Random().nextInt(height / 8);
      g.setColor(getRandomColor());
      g.drawLine(xs, ys, xe, ye);
    }
  }
 
  public Color getRandomColor(){
    int red = 0, green = 0, blue = 0;
    Random random = new Random();
    red = random.nextInt(255);
    green = random.nextInt(255);
    blue = random.nextInt(255);
    return new Color(red,green,blue);
  }
 
  public String getRandomStr(ImgCodeType type) {
    switch (type) {
      case ENANDNUMBER:
        return creatENAndNumberCode();
      case HAN:
        return creatHan();
      case EN:
        return creatENCode();
      case NUMBER:
        return creatNumberCode();
      default:
        return creatENAndNumberCode();
    }
  }
 
  public void write(String path) throws IOException {
    OutputStream sos = new FileOutputStream(path);
    this.write(sos);
  }
 
  public void write(OutputStream sos) throws IOException {
    ImageIO.write(buffImg, "png", sos);
    sos.close();
  }
 
  /**
   * 單個漢字
   *
   * @return
   */
  public static String creatHan() {
    String code = "";
    Random random = new Random();
    //一個漢字兩個字節(jié)
    byte[] bytes = new byte[2];
    bytes[0] = Integer.valueOf(176 + Math.abs(random.nextInt(39))).byteValue();
    bytes[1] = Integer.valueOf(161 + Math.abs(random.nextInt(93))).byteValue();
    try {
      code = new String(bytes, "GBK");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    return code;
  }
 
  /**
   * 英文字母加數(shù)字
   *
   * @return
   */
  public static String creatENAndNumberCode() {
    Random random = new Random();
    return String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
  }
 
  /**
   * 英文字母
   * 
   *
   * @return
   */
  public static String creatENCode() {
    StringBuffer code = new StringBuffer();
    Random random = new Random();
    int s = random.nextInt(25) + 65;
    code.append((char) s);
    return code.toString();
  }
 
 
  /**
   * 純數(shù)字驗證碼
   * 
   *
   * @return
   */
  public static String creatNumberCode() {
    StringBuffer code = new StringBuffer();
    Random random = new Random();
    return code.append(random.nextInt(9) + "").toString();
  }
 
 
  public BufferedImage getBuffImg() {
    return buffImg;
  }
 
  public String getCode() {
    return code;
  }
 
  public void setCode(String code) {
    this.code = code;
  }
 
 
  public static void main(String[] args) {
    String randomChineseChar = creatHan();
    String randomEnChar = creatENCode();
    System.out.println("args = [" + randomChineseChar + "]");
    System.out.println("args = [" + randomEnChar + "]");
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java如何用反射將一個對象復(fù)制給另一個對象

    java如何用反射將一個對象復(fù)制給另一個對象

    這篇文章主要介紹了java如何用反射將一個對象復(fù)制給另一個對象問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 解決@Cacheable在同一個類中方法調(diào)用不起作用的問題

    解決@Cacheable在同一個類中方法調(diào)用不起作用的問題

    這篇文章主要介紹了解決@Cacheable在同一個類中方法調(diào)用不起作用的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java Lombok簡介、使用、工作原理、優(yōu)缺點

    Java Lombok簡介、使用、工作原理、優(yōu)缺點

    這篇文章主要介紹了Java Lombok簡介、使用、工作原理、優(yōu)缺點的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Java Lombok,感興趣的朋友可以了解下
    2021-03-03
  • Mybatis plus通用字段自動填充的示例

    Mybatis plus通用字段自動填充的示例

    這篇文章主要介紹了Mybatis plus通用字段自動填充的示例,幫助大家更好的理解和使用Mybatis,感興趣的朋友可以了解下
    2021-01-01
  • 擲6面骰子6000次每個點數(shù)出現(xiàn)的概率

    擲6面骰子6000次每個點數(shù)出現(xiàn)的概率

    今天小編就為大家分享一篇關(guān)于擲6面骰子6000次每個點數(shù)出現(xiàn)的概率,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • Flink開發(fā)IDEA環(huán)境搭建與測試的方法

    Flink開發(fā)IDEA環(huán)境搭建與測試的方法

    這篇文章主要介紹了Flink開發(fā)IDEA環(huán)境搭建與測試的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • mybatis-plus update更新操作的三種方式(小結(jié))

    mybatis-plus update更新操作的三種方式(小結(jié))

    本文主要介紹了mybatis-plus update更新操作的三種方式,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • Maven多模塊之父子關(guān)系的創(chuàng)建

    Maven多模塊之父子關(guān)系的創(chuàng)建

    這篇文章主要介紹了Maven多模塊之父子關(guān)系的創(chuàng)建,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • java使用FastJson解析Json數(shù)據(jù)

    java使用FastJson解析Json數(shù)據(jù)

    本篇文章主要介紹了java使用FastJson解析Json數(shù)據(jù),fastjson 是一個性能極好的用 Java 語言實現(xiàn)的 JSON 解析器和生成器,有興趣的可以了解一下。
    2017-02-02
  • 如何給yml配置文件的密碼加密(SpringBoot)

    如何給yml配置文件的密碼加密(SpringBoot)

    這篇文章主要介紹了如何給yml配置文件的密碼加密(SpringBoot),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10

最新評論