git動態驗證碼

效果圖1:
git動態驗證碼
效果圖2:
git動態驗證碼
效果圖3:
git動態驗證碼
效果圖4:
git動態驗證碼
效果圖5:
git動態驗證碼

代碼片段:


import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

public class GifCaptcha {

    private Font font = new Font("宋體", Font.BOLD, 20); // 字體
    private int width = 160; // 驗證碼顯示長度
    private int height = 40; // 驗證碼顯示高度
    private String word = ""; // 當前的字符串
    private int delay = 100; // 幀延遲 (默認100)
    private int quality = 10;// 量化器取樣間隔 - 默認是10ms
    private int repeat = 0; // 幀循環次數
    private int minColor = 0;// 設置隨機顏色時,最小的取色範圍
    private int maxColor = 255;// 設置隨機顏色時,最大的取色範圍
    private int right = 0; // 設置字符最右邊的相對位置---相對原始位置 ,默認爲0

    /**
     * 空參構造函數
     */
    public GifCaptcha() {
    }

    /**
     * 可以設置驗證碼寬度,高度的構造函數
     * 
     * @param width
     *            -驗證碼寬度
     * @param height
     *            -驗證碼高度
     */
    public GifCaptcha(int width, int height) {
        this.width = width;
        this.height = height;
    }

    /**
     *
     * @param width
     *            -驗證碼寬度
     * @param height
     *            -驗證碼高度
     * @param font
     *            -字體
     */
    public GifCaptcha(int width, int height, Font font) {
        this(width, height);
        this.font = font;
    }

    /**
     * @param width
     *            -驗證碼寬度
     * @param height
     *            -驗證碼高度
     * @param font
     *            -字體
     * @param delay
     *            -幀延遲
     */
    public GifCaptcha(int width, int height, Font font, int delay) {
        this(width, height, font);
        this.delay = delay;
    }

    public Font getFont() {
        return font;
    }

    /**
     * 設置字體
     * 
     * @param font
     */
    public void setFont(Font font) {
        this.font = font;
    }

    public int getWidth() {
        return width;
    }

    /**
     * 設置驗證碼寬度
     * 
     * @param width
     */
    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    /**
     * 設置驗證碼高度
     * 
     * @param height
     */
    public void setHeight(int height) {
        this.height = height;
    }

    public String getWord() {
        return word;
    }

    /**
     * 設置驗證碼字符
     * 
     * @param chars
     */
    public void setWord(String chars) {
        this.word = chars;
    }

    public int getDelay() {
        return delay;
    }

    /**
     * 設置每一幀之間的延遲時間,或改變它的後續幀(適用於最後一幀添加)。
     * 
     * @param delay
     *            單位是毫秒
     */
    public void setDelay(int delay) {
        this.delay = delay;
    }

    public int getQuality() {
        return quality;
    }

    /**
     * 設置圖像的顏色量化(轉換質量 由GIF規範允許的最大256種顏色)。 低的值(最小值= 1)產生更好的顏色,但處理顯著緩慢。
     * 10是默認,併產生良好的顏色而且有以合理的速度。 值更大(大於20)不產生顯著的改善速度
     * 
     * @param quality
     *            大於1
     */
    public void setQuality(int quality) {
        if (quality < 1) {
            quality = 1;
        }
        this.quality = quality;
    }

    public int getRepeat() {
        return repeat;
    }

    /**
     * 設置GIF幀應該播放的次數。 默認是 0; 0意味着無限循環。 必須在添加的第一個圖像之前被調用。
     * 
     * @param repeat
     *            必須大於等於0
     */
    public void setRepeat(int repeat) {
        if (repeat >= 0) {
            this.repeat = repeat;
        }
    }

    public int getRight() {
        return right;
    }

    public void setRight(int right) {
        this.right = right;
    }

    public int getMaxColor() {
        return maxColor;
    }

    public void setMaxColor(int maxColor) {
        this.maxColor = maxColor;
    }

    public int getMinColor() {
        return minColor;
    }

    public void setMinColor(int minColor) {
        this.minColor = minColor;
    }

    /**
     * 給定一個輸出流 ,輸入圖片
     * 
     * @param os
     */
    public void outImage(OutputStream os) {
        try {
            AnimatedGifEncoder gifEncoder = new AnimatedGifEncoder();// gif編碼類
            // 生成字符
            gifEncoder.start(os);
            gifEncoder.setQuality(quality);// 設置量化器取樣間隔
            gifEncoder.setDelay(delay);// 設置幀延遲
            gifEncoder.setRepeat(repeat);// 幀循環次數
            BufferedImage frame;
            char[] rands = randomCaptcha(4);
            Color fontcolor[] = new Color[word.length()];
            for (int i = 0; i < word.length(); i++) {
                fontcolor[i] = Randoms.randomColor(minColor, maxColor, 200);
            }
            for (int i = 0; i < word.length(); i++) {
                frame = graphicsImage(fontcolor, rands, i);
                gifEncoder.addFrame(frame);
                frame.flush();
            }
            gifEncoder.finish();
        } finally {
            try {
                os.close();
            } catch (IOException e) {
                // ignore
            }
        }

    }

    /**
     * 生成驗證碼圖片
     *
     * @param fontcolor
     *            隨機字體顏色
     * @param strs
     *            字符數組
     * @param alpha
     *            透明度使用
     * @return BufferedImage
     */
    private BufferedImage graphicsImage(Color[] fontcolor, char[] strs, int alpha) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = image.createGraphics();
        g2d.setColor(Color.WHITE);// 利用指定顏色填充背景
        g2d.fillRect(0, 0, width, height);
        AlphaComposite ac;
        float y = ((height - 8) >> 1) + (font.getSize() >> 1);// 字符的y座標
        float m = (width - (word.length() * font.getSize())) / word.length();
        float x = m / 10;// 字符的x座標
        g2d.setFont(font);
        for (int i = 0; i < word.length(); i++) {
            ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getPellucidity(alpha, i));
            g2d.setComposite(ac);
            g2d.setColor(fontcolor[i]);
            g2d.drawOval(Randoms.num(width), Randoms.num(height), Randoms.num(5, 30), 5 + Randoms.num(5, 30));// 繪製橢圓邊框
            g2d.drawString(strs[i] + "", x + (font.getSize() + m) * i + right, y);
        }
        Randoms.randomLine(g2d, width, height, 3, 100);
        g2d.dispose();
        return image;
    }

    /**
     * 獲取透明度,從0到1,自動計算步長
     * 
     * @return float 透明度
     */
    protected float getPellucidity(int i, int j) {
        int num = i + j;
        float r = (float) 1 / word.length(), s = (word.length() + 1) * r;
        return num > word.length() ? (num * r - s) : num * r;
    }

    /**
     * 生成隨機驗證碼
     */
    protected char[] randomCaptcha(int count) {
        char[] c = Randoms.randomCaptcha(count);
        word = String.valueOf(c);
        return c;
    }
}

下載地址:
https://download.csdn.net/download/zxcnlmx/10677140

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章