成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專(zhuān)欄INFORMATION COLUMN

圖片驗(yàn)證碼的JAVA工具類(lèi)

SKYZACK / 744人閱讀

摘要:我們平時(shí)開(kāi)發(fā)時(shí)經(jīng)常會(huì)遇到需要圖片驗(yàn)證碼,基礎(chǔ)的驗(yàn)證碼包括了數(shù)字字母甚至可能有漢字。下面我給出一個(gè)簡(jiǎn)單的工具類(lèi)。驗(yàn)證碼生成器圖片的寬度。

我們平時(shí)開(kāi)發(fā)時(shí)經(jīng)常會(huì)遇到需要圖片驗(yàn)證碼,基礎(chǔ)的驗(yàn)證碼包括了數(shù)字、字母、甚至可能有漢字。下面我給出一個(gè)簡(jiǎn)單的工具類(lèi)。

package com..ankang.tony.util;

import java.awt.Color;

import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Random;

import javax.imageio.ImageIO;

/**
 * 驗(yàn)證碼生成器
 */
public class ValidateCode {

    // 圖片的寬度。
    private int width = 160;
    // 圖片的高度。
    private int height = 40;
    // 驗(yàn)證碼字符個(gè)數(shù)
    private int codeCount = 5;
    // 驗(yàn)證碼干擾線數(shù)
    private int lineCount = 150;
    // 驗(yàn)證碼
    private static String code = null;
    // 驗(yàn)證碼圖片Buffer
    private BufferedImage buffImg = null;

    private char[] codeSequence = { "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R",
            "S", "T", "U", "V", "W", "X", "Y", "Z", "2", "3", "4", "5", "6", "7", "8", "9" };

    public ValidateCode() {
        this.createCode();
    }

    /**
     * 
     * @param width
     *            圖片寬
     * @param height
     *            圖片高
     */
    public ValidateCode(int width, int height) {
        this.width = width;
        this.height = height;
        this.createCode();
    }

    /**
     * 
     * @param width
     *            圖片寬
     * @param height
     *            圖片高
     * @param codeCount
     *            字符個(gè)數(shù)
     * @param lineCount
     *            干擾線條數(shù)
     */
    public ValidateCode(int width, int height, int codeCount, int lineCount) {
        this.width = width;
        this.height = height;
        this.codeCount = codeCount;
        this.lineCount = lineCount;
        this.createCode();
    }

    public void createCode() {
        int x = 0, fontHeight = 0, codeY = 0;
        int red = 0, green = 0, blue = 0;

        x = width / (codeCount + 1);// 每個(gè)字符的寬度
        fontHeight = height - 2;// 字體的高度
        codeY = height - 3;

        // 圖像buffer
        buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = buffImg.createGraphics();
        // 生成隨機(jī)數(shù)
        Random random = new Random();
        // 將圖像填充為白色
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, width, height);
        // 創(chuàng)建字體
        ImgFontByte imgFont = new ImgFontByte();
        Font font = imgFont.getFont(fontHeight);
        g.setFont(font);
        for (int i = 0; i < lineCount; i++) {
            int xs = random.nextInt(width);
            int ys = random.nextInt(height);
            int xe = xs + random.nextInt(width / 8);
            int ye = ys + random.nextInt(height / 8);
            red = random.nextInt(255);
            green = random.nextInt(255);
            blue = random.nextInt(255);
            g.setColor(new Color(red, green, blue));
            g.drawLine(xs, ys, xe, ye);
        }
        // randomCode記錄隨機(jī)產(chǎn)生的驗(yàn)證碼
        StringBuffer randomCode = new StringBuffer();
        // 隨機(jī)產(chǎn)生codeCount個(gè)字符的驗(yàn)證碼。
        for (int i = 0; i < codeCount; i++) {
            String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
            // 產(chǎn)生隨機(jī)的顏色值,讓輸出的每個(gè)字符的顏色值都將不同。
            red = random.nextInt(255);
            green = random.nextInt(255);
            blue = random.nextInt(255);
            g.setColor(new Color(red, green, blue));
            g.drawString(strRand, (i + 1) * x, codeY);
            // 將產(chǎn)生的四個(gè)隨機(jī)數(shù)組合在一起。
            randomCode.append(strRand);
        }
        // 將四位數(shù)字的驗(yàn)證碼保存到Session中。
        code = randomCode.toString();
    }

    public void write(String path,String fileName) throws IOException {
        File folder = new File(path);
        if(!folder.exists()){
            folder.mkdirs();
        }
        OutputStream sos = new FileOutputStream(path+fileName);
        this.write(sos);
    }

    public void write(OutputStream sos) throws IOException {
        ImageIO.write(buffImg, "png", sos);
        sos.close();
    }

    public BufferedImage getBuffImg() {
        return buffImg;
    }

    public String getCode() {
        return code;
    }
    
    public static void main(String[] args) {  
        ValidateCode vCode = new ValidateCode(120,40,5,50);  
        try {  
            String path="D:
eportimagecode";  
            System.out.println(vCode.getCode()+" >"+path);  
            vCode.write(path,new Date().getTime()+".png");  
        } catch (IOException e) {  
            e.printStackTrace();
        }  
     }

}

下面這個(gè)類(lèi)主要是用作字體的設(shè)置,大家也可以直接拿過(guò)來(lái)用。

package com.ankang.tony.util;

import java.awt.Font;
import java.io.ByteArrayInputStream;

public class ImgFontByte {
     public Font getFont(int fontHeight){  
            try {  
                Font baseFont = Font.createFont(Font.ITALIC, new ByteArrayInputStream(hex2byte(getFontByteStr()))); 
                return baseFont.deriveFont(Font.PLAIN, fontHeight);  
            } catch (Exception e) {  
                return new Font("Consola",Font.PLAIN, fontHeight);  
            }  
        }  
          
        private  byte[] hex2byte(String str) {   
            if (str == null)  
                return null;  
            str = str.trim();  
            int len = str.length();  
            if (len == 0 || len % 2 == 1)  
                return null;  
            byte[] b = new byte[len / 2];  
            try {  
                for (int i = 0; i < str.length(); i += 2) {  
                    b[i/2] = (byte) Integer.decode("0x" + str.substring(i, i + 2)).intValue();  
                }  
                return b;  
            } catch (Exception e) {  
                return null;  
            }  
        } 
        
     /** 
      * ttf字體文件的十六進(jìn)制字符串 
      * @return 
      */  
     private String getFontByteStr(){ 
         return null;  
     }  
}

點(diǎn)我啦!贈(zèng)送大疆無(wú)人機(jī)

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/64991.html

相關(guān)文章

  • Java虛擬機(jī):Java二進(jìn)制字節(jié)碼的結(jié)構(gòu)、加載

    摘要:這個(gè)龐大的結(jié)構(gòu)主要包含以下幾個(gè)部分魔數(shù)和版本號(hào)基本的信息用于確定二進(jìn)制字節(jié)碼的特征和加載可行特征。 這篇文章的素材來(lái)自周志明的《深入理解Java虛擬機(jī)》。 作為Java開(kāi)發(fā)人員,一定程度了解JVM虛擬機(jī)的的運(yùn)作方式非常重要,本文就一些簡(jiǎn)單的虛擬機(jī)的相關(guān)概念和運(yùn)作機(jī)制展開(kāi)我自己的學(xué)習(xí)過(guò)程,是這個(gè)系列的第二篇。 我們?cè)谖募飳?xiě)入了java的源代碼,源代碼寫(xiě)就后存入磁盤(pán),磁盤(pán)上的源代碼經(jīng)過(guò)j...

    CHENGKANG 評(píng)論0 收藏0
  • ThinkPHP驗(yàn)證碼不顯示的解決方案

    摘要:今天遇到一個(gè)很奇怪的,就是我寫(xiě)了一個(gè)程序本地運(yùn)行正常,但是發(fā)布到甲方的服務(wù)器上出現(xiàn)無(wú)法顯示驗(yàn)證碼的。適用于驗(yàn)證碼圖片在新窗口打開(kāi)的時(shí)候顯示了報(bào)錯(cuò)信息以及一堆亂碼的情況下,如果有報(bào)錯(cuò)信息,請(qǐng)根據(jù)報(bào)錯(cuò)信息進(jìn)行檢查驗(yàn)證碼所調(diào)用的字體是否缺失。 今天遇到一個(gè)很奇怪的BUG,就是我寫(xiě)了一個(gè)PHP程序本地運(yùn)行正常,但是發(fā)布到甲方的服務(wù)器上出現(xiàn)無(wú)法顯示驗(yàn)證碼的BUG。 showImg(https:/...

    Meils 評(píng)論0 收藏0
  • node識(shí)別驗(yàn)證

    摘要:驗(yàn)證碼的識(shí)別成功率跟圖片質(zhì)量關(guān)系密切,一般拿到后的驗(yàn)證碼都得經(jīng)過(guò)灰度化,二值化,去噪,利用就可以很方便的做到。 了解驗(yàn)證碼 什么是驗(yàn)證碼? 所謂驗(yàn)證碼,就是將一串隨機(jī)產(chǎn)生的數(shù)字或符號(hào),生成一幅圖片,圖片里加上一些干擾象素(防止OCR),由用戶(hù)肉眼識(shí)別其中的驗(yàn)證碼信息,輸入表單提交網(wǎng)站驗(yàn)證,驗(yàn)證成功后才能使用某項(xiàng)功能,通俗說(shuō)就是一種區(qū)分用戶(hù)是計(jì)算機(jī)和人的公共全自動(dòng)程序 驗(yàn)證碼的作用 可以...

    levy9527 評(píng)論0 收藏0
  • 15分鐘破解網(wǎng)站驗(yàn)證

    摘要:目前花費(fèi)了兩分鐘。我這地方使用卷積神經(jīng)網(wǎng)絡(luò),。這地方對(duì)卷積神經(jīng)網(wǎng)絡(luò)算法就不做詳細(xì)介紹,感興趣的同學(xué),可以學(xué)習(xí)一下。 概述 ??很多開(kāi)發(fā)者都討厭網(wǎng)站的驗(yàn)證碼,特別是寫(xiě)網(wǎng)絡(luò)爬蟲(chóng)的程序員,而網(wǎng)站之所以設(shè)置驗(yàn)證碼,是為了防止機(jī)器人訪問(wèn)網(wǎng)站,造成不必要的損失?,F(xiàn)在好了,隨著機(jī)器學(xué)習(xí)技術(shù)的發(fā)展,機(jī)器識(shí)別驗(yàn)證碼的問(wèn)題比較好解決了。 樣本采集工具 ??這里我們采用wordpress的Really Si...

    hlcfan 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<