溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用Java怎么動態(tài)獲取圖片驗(yàn)證碼

發(fā)布時間:2021-04-06 17:37:40 來源:億速云 閱讀:140 作者:Leah 欄目:編程語言

使用Java怎么動態(tài)獲取圖片驗(yàn)證碼?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

具體如下:

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;
  /**
   * 驗(yàn)證碼字符個數(shù)
   */
  private int codeCount = 5;
  /**
   * 驗(yàn)證碼干擾線數(shù)
   */
  private int lineCount = 150;
  /**
   * 圖片驗(yàn)證碼類型
   */
  private ImgCodeType imgCodeType = DEFAULT_TYPE;
  /**
   * 驗(yàn)證碼
   */
  private String code = null;
  /**
   * 驗(yàn)證碼圖片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)生的驗(yàn)證碼
    StringBuffer randomCode = new StringBuffer();
    // 隨機(jī)產(chǎn)生codeCount個字符的驗(yàn)證碼。
    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ù)字驗(yàn)證碼
   * 
   *
   * @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)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI