中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Java生成驗(yàn)證碼

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Random;
import javax.imageio.ImageIO;
 
public class ValidationCode {
 
    // 圖形驗(yàn)證碼的字符集合,系統(tǒng)將隨機(jī)從這個(gè)字符串中選擇一些字符作為驗(yàn)證碼
    private static String codeChars = "%#23456789abcdefghkmnpqrstuvwxyzABCDEFGHKLMNPQRSTUVWXYZ";
 
    // 返回一個(gè)隨機(jī)顏色(Color對象)
    private static Color getRandomColor(int minColor, int maxColor) {
        Random random = new Random();
        // 保存minColor最大不會超過255
        if (minColor > 255)
            minColor = 255;
        // 保存minColor最大不會超過255
        if (maxColor > 255)
            maxColor = 255;
        // 獲得紅色的隨機(jī)顏色值
        int red = minColor + random.nextInt(maxColor - minColor);
        // 獲得綠色的隨機(jī)顏色值
        int green = minColor + random.nextInt(maxColor - minColor);
        // 獲得藍(lán)色的隨機(jī)顏色值
        int blue = minColor + random.nextInt(maxColor - minColor);
        return new Color(red, green, blue);
    }
 
    protected static void getValidationCode() throws IOException {
        try {
            // 獲得驗(yàn)證碼集合的長度
            int charsLength = codeChars.length();
            // 設(shè)置圖形驗(yàn)證碼的長和寬(圖形的大。
            int width = 90, height = 30;
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics g = image.getGraphics();// 獲得用于輸出文字的Graphics對象
            Random random = new Random();
            g.setColor(getRandomColor(180, 250));// 隨機(jī)設(shè)置要填充的顏色
            g.fillRect(0, 0, width, height);// 填充圖形背景
            // 設(shè)置初始字體
            g.setFont(new Font("Times New Roman", Font.ITALIC, height));
            g.setColor(getRandomColor(120, 180));// 隨機(jī)設(shè)置字體顏色
            // 用于保存最后隨機(jī)生成的驗(yàn)證碼
            StringBuilder validationCode = new StringBuilder();
            // 驗(yàn)證碼的隨機(jī)字體
            String[] fontNames = { "Times New Roman", "Book antiqua", "Arial" };
            // 隨機(jī)生成3個(gè)到5個(gè)驗(yàn)證碼
            for (int i = 0; i < 3 + random.nextInt(3); i++) {
                // 隨機(jī)設(shè)置當(dāng)前驗(yàn)證碼的字符的字體
                g.setFont(new Font(fontNames[random.nextInt(3)], Font.ITALIC, height));
                // 隨機(jī)獲得當(dāng)前驗(yàn)證碼的字符
                char codeChar = codeChars.charAt(random.nextInt(charsLength));
                validationCode.append(codeChar);
                // 隨機(jī)設(shè)置當(dāng)前驗(yàn)證碼字符的顏色
                g.setColor(getRandomColor(10, 100));
                // 在圖形上輸出驗(yàn)證碼字符,x和y都是隨機(jī)生成的
                g.drawString(String.valueOf(codeChar), 16 * i + random.nextInt(7), height - random.nextInt(6));
            }
            File file = new File("d:\\code.png");  
            ImageIO.write(image, "png", file);  
            System.out.println(validationCode.toString());
            //byte[] data = ((DataBufferByte) image.getData().getDataBuffer()).getData();
            g.dispose();
        } catch (Exception e) {
            e.printStackTrace();  
        }
    }
 
    public static void main(String[] args) throws IOException{
        getValidationCode();
    }
}

標(biāo)簽: isp

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:dom一些常用的操作JS方法介紹

下一篇:javascript操作cookie的代碼