Java生成驗證碼(包含gif動畫驗證碼)

1.驗證碼抽象類java

import java.awt.*;數組

import java.io.OutputStream;dom

 import static Randoms.num;ide

import static Randoms.alpha;工具

 * <p>驗證碼抽象類,暫時不支持中文</p>字體

 * @author: wuhongjunui

 * @version:1.0this

public abstract class Captcha編碼

    protected Font font = new Font("Verdana", Font.ITALIC|Font.BOLD, 28);   // 字體spa

    protected int len = 5;  // 驗證碼隨機字符長度

    protected int width = 150;  // 驗證碼顯示跨度

    protected int height = 40;  // 驗證碼顯示高度

    private String chars = null;  // 隨機字符串

     * 生成隨機字符數組

     * @return 字符數組

    protected char[] alphas()

        char[] cs = new char[len];

        for(int i = 0;i<len;i++)

            cs[i] = alpha();

        chars = new String(cs);

        return cs;

    public Font getFont()

        return font;

    public void setFont(Font font)

        this.font = font;

    public int getLen()

        return len;

    public void setLen(int len)

        this.len = len;

    public int getWidth()

        return width;

    public void setWidth(int width)

        this.width = width;

    public int getHeight()

    public void setHeight(int height)  {

        this.height = height;

     * 給定範圍得到隨機顏色

     * @return Color 隨機顏色

     */

    protected Color color(int fc, int bc)

  {http://www.huiyi8.com/gundongdaima/ 滾動代碼

        if (fc > 255)

            fc = 255;

        if (bc > 255)

            bc = 255;

        int r = fc + num(bc - fc);

        int g = fc + num(bc - fc);

        int b = fc + num(bc - fc);

        return new Color(r, g, b);

     * 驗證碼輸出,抽象方法,由子類實現

     * @param os 輸出流

     */

    public abstract void out(OutputStream os);

     * 獲取隨機字符串

     * @return string

    public String text()

        return chars;

2.png格式驗證碼

import javax.p_w_picpathio.ImageIO;

import java.awt.*;

import java.awt.p_w_picpath.BufferedImage;

import java.io.IOException;

import java.io.OutputStream;

import static Randoms.num;

 * <p>png格式驗證碼</p>

 *

 * @author: wuhongjun

 * @version:1.0

public class SpecCaptcha extends Captcha

    public SpecCaptcha()

    public SpecCaptcha(int width, int height)

        this.width = width;

        this.height = height;

    public SpecCaptcha(int width, int height, int len){

        this(width,height);

        this.len = len;

    public SpecCaptcha(int width, int height, int len, Font font){

        this(width,height,len);

        this.font = font;

     * 生成驗證碼

     * @throws java.io.IOException IO異常

     */

    @Override

    public void out(OutputStream out){

        graphicsImage(alphas(), out);

     * 畫隨機碼圖

     * @param strs 文本

     * @param out 輸出流

    private boolean graphicsImage(char[] strs, OutputStream out){

        boolean ok = false;

        try

            BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

            Graphics2D g = (Graphics2D)bi.getGraphics();

            AlphaComposite ac3;

            Color color ;

            int len = strs.length;

            g.setColor(Color.WHITE);

            g.fillRect(0,0,width,height);

            // 隨機畫干擾的蛋蛋

            for(int i=0;i<15;i++){

                color = color(150, 250);

                g.setColor(color);

                g.drawOval(num(width), num(height), 5+num(10), 5+num(10));// 畫蛋蛋,有蛋的生活才精彩

                color = null;

            }

            g.setFont(font);

            int h  = height - ((height - font.getSize()) >>1),

                w = width/len,

                size = w-font.getSize()+1;

            /* 畫字符串 */

            for(int i=0;i<len;i++)

                ac3 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f);// 指定透明度

                g.setComposite(ac3);

                color = new Color(20 + num(110), 20 + num(110), 20 + num(110));// 對每一個字符都用隨機顏色

                g.setColor(color);

                g.drawString(strs[i]+"",(width-(len-i)*w)+size, h-4);

                color = null;

                ac3 = null;

            ImageIO.write(bi, "png", out);

            out.flush();

            ok = true;

        }catch (IOException e){

            ok = false;

        }finally

          Streams.close(out);

        return ok;

3.Gif驗證碼類  

import Streams;

import java.awt.*;

import java.awt.p_w_picpath.BufferedImage;

import java.io.OutputStream;

import static Randoms.num;

/**

 * <p>Gif驗證碼類</p>

 * @author: wuhongjun

 * @version:1.0

public class GifCaptcha extends Captcha

    public GifCaptcha()

    public GifCaptcha(int width,int height){

        this.width = width;

    public GifCaptcha(int width,int height,int len){

        this(width,height);

        this.len = len;

    public GifCaptcha(int width,int height,int len,Font font)

        this(width,height,len);

        this.font = font;

    @Override

    public void out(OutputStream os)

        try

 

            GifEncoder gifEncoder = new GifEncoder();   // gif編碼類,這個利用了洋人寫的編碼類,全部類都在附件中

            //生成字符

            gifEncoder.start(os);

            gifEncoder.setQuality(180);

            gifEncoder.setDelay(100);

            gifEncoder.setRepeat(0);

            BufferedImage frame;

            char[] rands =alphas();

            Color fontcolor[]=new Color[len];

            for(int i=0;i<len;i++)

                fontcolor[i]=new Color(20 + num(110), 20 + num(110), 20 + num(110));

            for(int i=0;i<len;i++)

                frame=graphicsImage(fontcolor, rands, i);

                gifEncoder.addFrame(frame);

                frame.flush();

            gifEncoder.finish();

        }finally

            Streams.close(os);

     * 畫隨機碼圖

     * @param fontcolor 隨機字體顏色

     * @param strs 字符數組

     * @param flag 透明度使用

     * @return BufferedImage

    private BufferedImage graphicsImage(Color[] fontcolor,char[] strs,int flag)

        BufferedImage p_w_picpath = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);

        //或得圖形上下文

        //Graphics2D g2d=p_w_picpath.createGraphics();

        Graphics2D g2d = (Graphics2D)p_w_picpath.getGraphics();

        //利用指定顏色填充背景

        g2d.setColor(Color.WHITE);

        g2d.fillRect(0, 0, width, height);

        AlphaComposite ac3;

        int h  = height - ((height - font.getSize()) >>1) ;

        int w = width/len;

        g2d.setFont(font);

        for(int i=0;i<len;i++)

            ac3 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getAlpha(flag, i));

            g2d.setComposite(ac3);

            g2d.setColor(fontcolor[i]);

            g2d.drawOval(num(width), num(height), 5+num(10), 5+num(10));

            g2d.drawString(strs[i]+"", (width-(len-i)*w)+(w-font.getSize())+1, h-4);

        g2d.dispose();

        return p_w_picpath;

     * 獲取透明度,從0到1,自動計算步長

     * @return float 透明度

     */

    private float getAlpha(int i,int j)

    {

        int num = i+j;

        float r = (float)1/len,s = (len+1) * r;

        return num > len ? (num *r - s) : num * r;

4.Streams IO工具類 

public class Streams

     * 關閉輸入流

     * @param in 輸入流

    public static void close(InputStream in) {

        if (in != null) {

            try {

                in.close();

            } catch (IOException ioex) {

                // ignore

     * 關閉輸出流

     * @param out 輸出流

    public static void close(OutputStream out) {

        if (out != null) {

            try {

                out.flush();

            } catch (IOException ioex) {

                // ignore

            try {

                out.close();

            } catch (IOException ioex) {

                // ignore

5.Randoms 隨機數工具類

* <p>隨機工具類</p>

 * @author: wuhongjun

 * @version:1.0

public class Randoms

    private static final Random RANDOM = new Random();

    //定義驗證碼字符.去除了O和I等容易混淆的字母

    public static final char ALPHA[]={'A','B','C','D','E','F','G','H','G','K','M','N','P','Q','R','S','T','U','V','W','X','Y','Z'

            ,'a','b','c','d','e','f','g','h','i','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z','2','3','4','5','6','7','8','9'};

     * 產生兩個數之間的隨機數

     * @param min 小數

     * @param max 比min大的數

     * @return int 隨機數字

     */

    public static int num(int min, int max)

        return min + RANDOM.nextInt(max - min);

     * 產生0--num的隨機數,不包括num

     * @param num 數字

     * @return int 隨機數字

     */

    public static int num(int num)

        return RANDOM.nextInt(num);

    public static char alpha()

        return ALPHA[num(0, ALPHA.length)];

相關文章
相關標籤/搜索