java產生圖片驗證碼範例程式碼_java

來源:互聯網
上載者:User

 本文執行個體為大家分享了java圖片驗證碼具體實現代碼,供大家參考,具體內容如下

import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.Random;import javax.imageio.ImageIO;/* * 步驟: * 1.畫框 * 2.畫背景 * 3.畫字元 * 4.畫幹擾線 */public final class ImageUtil {     // 驗證碼字元集  private static final char[] chars = {     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',     'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};  // 字元數量  private static final int SIZE = 4;  // 幹擾線數量  private static final int LINES = 5;  // 寬度  private static final int WIDTH = 80;  // 高度  private static final int HEIGHT = 40;  // 字型大小  private static final int FONT_SIZE = 30;   /**   * 產生隨機驗證碼及圖片   * Object[0]驗證碼字串String   * Object[1]驗證碼圖片  BufferedImage   */  public static Object[] createImage() {    StringBuffer sb = new StringBuffer();    // 1.建立空白圖片    BufferedImage image = new BufferedImage(      WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);    // 2.擷取圖片畫筆    Graphics graphic = image.getGraphics();    // 3.設定畫筆顏色    graphic.setColor(Color.LIGHT_GRAY);    // 4.繪製矩形背景    graphic.fillRect(0, 0, WIDTH, HEIGHT);    // 5.畫隨機字元    Random ran = new Random();    for (int i = 0; i <SIZE; i++) {      // 取隨機字元索引      int n = ran.nextInt(chars.length);      // 設定隨機顏色      graphic.setColor(getRandomColor());      // 設定字型大小      graphic.setFont(new Font(        null, Font.BOLD + Font.ITALIC, FONT_SIZE));      // 畫字元      graphic.drawString(        chars[n] + "", i * WIDTH / SIZE, HEIGHT / 2);      // 記錄字元      sb.append(chars[n]);    }    // 6.畫幹擾線    for (int i = 0; i < LINES; i++) {      // 設定隨機顏色      graphic.setColor(getRandomColor());      // 隨機畫線      graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT),          ran.nextInt(WIDTH), ran.nextInt(HEIGHT));    }    // 7.返回驗證碼和圖片    return new Object[]{sb.toString(), image};  }   /**   * 隨機取色   */  public static Color getRandomColor() {    Random ran = new Random();    Color color = new Color(ran.nextInt(256),         ran.nextInt(256), ran.nextInt(256));    return color;  }     public static void main(String[] args) throws IOException {    Object[] objs = createImage();    BufferedImage image = (BufferedImage) objs[1];    //圖片格式可以自訂,java對png圖片顯示更清楚,輸出路徑可自訂    OutputStream os = new FileOutputStream("d:/x.png");    //注意圖片格式與建立格式匹配    ImageIO.write(image, "png", os);    os.close();  } }

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.