大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 Java驗證碼工具類的代碼

Java驗證碼工具類的代碼

更新時間:2021-11-11 09:30:54 來源:動力節點 瀏覽796次

驗證碼代碼工具代碼如下:

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
/**
 * @title 驗證碼工具類
 * @author boat
 */
public class ImageUtil {
    private static int width = 90;// 定義圖片的width
    private static int height = 30;// 定義圖片的height
    //5個驗證碼一組
   /* private static int codeCount = 5;// 定義圖片上顯示驗證碼的個數
    private static int xx = 13;*/
    //4個驗證碼一組
    private static int codeCount = 4;// 定義圖片上顯示驗證碼的個數
    private static int xx = 15;
    private static int codeY = 23;
    private static int fontHeight = 22;
    private static char[] codeSequence = {'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','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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    /**
     * 生成一個map集合
     * code為生成的驗證碼
     * codePic為生成的驗證碼BufferedImage對象
     *
     * @return
     */
    public static Map<String, Object> generateCodeAndPic() {
        // 定義圖像buffer
        BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // Graphics2D gd = buffImg.createGraphics();
        // Graphics2D gd = (Graphics2D) buffImg.getGraphics();
        Graphics gd = buffImg.getGraphics();
        // 創建一個隨機數生成器類
        Random random = new Random();
        // 將圖像填充為白色
        gd.setColor(Color.WHITE);
        gd.fillRect(0, 0, width, height);
        // 創建字體,字體的大小應該根據圖片的高度來定。
        Font font = new Font("Arial", Font.BOLD, fontHeight);
        // 設置字體。
        gd.setFont(font);
        // 畫邊框。
        //gd.setColor(Color.LIGHT_GRAY);
        gd.setColor(getRandomColor());
        gd.drawRect(0, 0, width - 1, height - 1);
        // 隨機產生40條干擾線,使圖象中的認證碼不易被其它程序探測到。
        gd.setColor(getRandomColor());
        for (int i = 0; i < 30; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            gd.drawLine(x, y, x + xl, y + yl);
        }
        // randomCode用于保存隨機產生的驗證碼,以便用戶登錄后進行驗證。
        StringBuffer randomCode = new StringBuffer();
        // 隨機產生codeCount數字的驗證碼。
        for (int i = 0; i < codeCount; i++) {
            // 得到隨機產生的驗證碼數字。
            String code = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
            // 用隨機產生的顏色將驗證碼繪制到圖像中。
            gd.setColor(getRandomColor());
            gd.drawString(code, (i + 1) * xx, codeY);
            // 將產生的四個隨機數組合在一起。
            randomCode.append(code);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        //存放驗證碼
        map.put("code", randomCode);
        //存放生成的驗證碼BufferedImage對象
        map.put("codePic", buffImg);
        return map;
    }
    /**
     * 隨機取色
     */
    public static Color getRandomColor() {
        Random ran = new Random();
        int red = ran.nextInt(256);
        int green = ran.nextInt(256);
        int blue = ran.nextInt(256);
        Color color = new Color(red,green,blue);
        return color;
    }
    // 獲取驗證碼
    public static Map<String,Object> getCheckCodePic(){
        return ImageUtil.generateCodeAndPic();
    }
    public static void main(String[] args) throws Exception {
        //創建文件輸出流對象
        OutputStream out = new FileOutputStream("d://"+System.currentTimeMillis()+".jpg");
        Map<String,Object> map = ImageUtil.generateCodeAndPic();
        ImageIO.write((RenderedImage) map.get("codePic"), "jpeg", out);
        System.out.println("驗證碼的值為:"+map.get("code"));
    }
}

以上就是關于“Java驗證碼工具類的代碼”的介紹,如果您想了解更多相關知識,不妨來關注一下動力節點的Java開發工具,里面的知識更加豐富。

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 伊人色美文情网址 | 四虎影院免费在线 | 亚洲国产成人综合 | 在线cao| 久久狠色噜噜狠狠狠狠97 | 欧美精品亚洲人成在线观看 | 日本精品a在线 | 色综合综合网 | 国产欧美另类久久精品91 | 99久久精品免费看国产一区二区 | 亚洲日本aⅴ片在线观看香蕉 | 久久久久久综合一区中文字幕 | 手机看片国产永久1204 | 久久免费高清 | 亚洲视频免费在线 | 久久宗合色 | 欧美高清国产 | 99久久做夜夜爱天天做精品 | 国产精品一区二区资源 | 黄频网站在线观看视频 | 天天拍夜夜添久久精品中文 | 国产成人一区二区三中文 | 午夜精品影院 | 91国内精品在线 | 成人永久福利在线观看不卡 | 精品久久久久久亚洲精品 | 操操操综合网 | 四虎国产精品免费久久麻豆 | 久久这里只有 | 天天爽天天摸 | 久久亚洲伊人中字综合精品 | 九九九国产在线 | 日本b站一卡二不卡 | 色狠狠色综合久久8狠狠色 色狠狠婷婷97 | 操熟美女又肥又嫩的骚屁股 | 伊人久久国产 | 国产一级特黄高清在线大片 | 亚州一级毛片在线 | 在线中文字幕亚洲 | 四虎国产精品4hu永久 | 亚洲国产日韩欧美一区二区三区 |