java生成动态gif格式与png格式的验证码(代码1)

来源:互联网 发布:hash算法的实现原理 编辑:程序博客网 时间:2024/05/01 20:27
Java代码  收藏代码
  1. import java.awt.Color;  
  2. import java.awt.Font;  
  3. import java.io.OutputStream;  
  4. import java.util.Random;  
  5.   
  6. /** 
  7.  * 验证码抽象类,暂时不支持中文 
  8.  * @author wzztestin 
  9.  * 
  10.  */  
  11. public abstract class Captcha {  
  12.     private static final Random random = new Random();  
  13.     // 字体  
  14.     protected Font font = new Font("Verdana", Font.ITALIC | Font.BOLD, 28);   
  15.     // 默认宽度  
  16.     protected int width = 150;   
  17.     //默认高度  
  18.     protected int height = 40;   
  19.     //验证码串  
  20.     private String yanzhenmastr = "";  
  21.     //除数  
  22.     private String onenum = "";  
  23.       
  24.     /** 
  25.      * 生成随机颜色 
  26.      * @param fc 
  27.      * @param bc 
  28.      * @return Color 
  29.      */  
  30.     Color getRandColor(int fc, int bc) {  
  31.         Random random = new Random();  
  32.         if (fc > 255) {  
  33.             fc = 255;  
  34.         }  
  35.         if (bc > 255) {  
  36.             bc = 255;  
  37.         }  
  38.         int r = fc + random.nextInt(bc - fc);  
  39.         int g = fc + random.nextInt(bc - fc);  
  40.         int b = fc + random.nextInt(bc - fc);  
  41.         return new Color(r, g, b);  
  42.     }  
  43.       
  44.     /** 
  45.      * 生成随机字体 
  46.      * @return Font 
  47.      */  
  48.      Font getRandomFont() {   
  49.          String[] fonts = {"Georgia""Verdana""Arial""Tahoma""Time News Roman""Courier New""Arial Black""Quantzite"};   
  50.          int fontIndex = (int)Math.round(Math.random() * (fonts.length - 1));   
  51.          int fontSize = 28;   
  52.          return new Font(fonts[fontIndex], Font.PLAIN, fontSize);   
  53.      }  
  54.       
  55.     public String getOnenum() {  
  56.         return onenum;  
  57.     }  
  58.   
  59.     public String getCzfu() {  
  60.         return czfu;  
  61.     }  
  62.   
  63.     public String getTwonum() {  
  64.         return twonum;  
  65.     }  
  66.     //操作符  
  67.     private String czfu = "";  
  68.     //被除数  
  69.     private String twonum = "";  
  70.     public String getYanzhenmastr() {  
  71.         return yanzhenmastr;  
  72.     }  
  73.   
  74.     protected int alpha = 5;  
  75.   
  76.     public int getAlpha() {  
  77.         return alpha;  
  78.     }  
  79.   
  80.     public void setAlpha(int alpha) {  
  81.         this.alpha = alpha;  
  82.     }  
  83.   
  84.     /** 
  85.      * 生成随机字符数组 
  86.      *  
  87.      * @return 字符数组 
  88.      */  
  89.     protected String getVilidCode() {  
  90.         int numlength =  random.nextInt(2)+1;  
  91.         for (int i = 0; i < numlength; i++) {  
  92.             char rnum = Randoms.getNum();  
  93.             yanzhenmastr = yanzhenmastr + rnum;  
  94.             onenum = onenum + rnum;  
  95.         }  
  96.         czfu = Randoms.getChaoZuoFu()+"";  
  97.         yanzhenmastr = yanzhenmastr + czfu;  
  98.         for (int i = 0; i < numlength; i++) {  
  99.             char rnum = Randoms.getNum();  
  100.             yanzhenmastr = yanzhenmastr + rnum;  
  101.             twonum = twonum + rnum;  
  102.         }  
  103.         yanzhenmastr = yanzhenmastr + "=?";  
  104.         return yanzhenmastr;  
  105.     }  
  106.   
  107.     public Font getFont() {  
  108.         return font;  
  109.     }  
  110.   
  111.     public void setFont(Font font) {  
  112.         this.font = font;  
  113.     }  
  114.   
  115.     public int getWidth() {  
  116.         return width;  
  117.     }  
  118.   
  119.     public void setWidth(int width) {  
  120.         this.width = width;  
  121.     }  
  122.   
  123.     public int getHeight() {  
  124.         return height;  
  125.     }  
  126.   
  127.     public void setHeight(int height) {  
  128.         this.height = height;  
  129.     }  
  130.   
  131.     /** 
  132.      * 给定范围获得随机颜色 
  133.      *  
  134.      * @return Color 随机颜色 
  135.      */  
  136.     protected Color color(int fc, int bc) {  
  137.         if (fc > 255)  
  138.             fc = 255;  
  139.         if (bc > 255)  
  140.             bc = 255;  
  141.         int r = fc + Randoms.num(bc - fc);  
  142.         int g = fc + Randoms.num(bc - fc);  
  143.         int b = fc + Randoms.num(bc - fc);  
  144.         return new Color(r, g, b);  
  145.     }  
  146.   
  147.     /** 
  148.      * 验证码输出,抽象方法,由子类实现 
  149.      *  
  150.      * @param os 
  151.      *            输出流 
  152.      */  
  153.     public abstract void out(OutputStream os);  
  154. }  

 

Java代码  收藏代码
  1. import java.io.IOException;  
  2. import java.io.OutputStream;  
  3.   
  4. /** 
  5.  *  
  6.  * @author wzztestin 
  7.  * 图片编码器 
  8.  */  
  9. public class Encoder {  
  10.     private static final int EOF = -1;  
  11.   
  12.     private int imgW, imgH;  
  13.     private byte[] pixAry;  
  14.     private int initCodeSize;  
  15.     private int remaining;  
  16.     private int curPixel;  
  17.       
  18.     /** 
  19.      * GIF Image compression routines 
  20.      */  
  21.   
  22.     static final int BITS = 12;  
  23.   
  24.     static final int HSIZE = 5003;   
  25.   
  26.     int n_bits;   
  27.     int maxbits = BITS;   
  28.     int maxcode;   
  29.     int maxmaxcode = 1 << BITS;   
  30.   
  31.     int[] htab = new int[HSIZE];  
  32.     int[] codetab = new int[HSIZE];  
  33.   
  34.     int hsize = HSIZE;   
  35.   
  36.     int free_ent = 0;   
  37.   
  38.     boolean clear_flg = false;  
  39.   
  40.     int g_init_bits;  
  41.   
  42.     int ClearCode;  
  43.     int EOFCode;  
  44.   
  45.     int cur_accum = 0;  
  46.     int cur_bits = 0;  
  47.   
  48.     int masks[] = { 0x00000x00010x00030x00070x000F0x001F0x003F,  
  49.             0x007F0x00FF0x01FF0x03FF0x07FF0x0FFF0x1FFF0x3FFF,  
  50.             0x7FFF0xFFFF };  
  51.   
  52.     int a_count;  
  53.   
  54.     byte[] accum = new byte[256];  
  55.   
  56.     Encoder(int width, int height, byte[] pixels, int color_depth) {  
  57.         imgW = width;  
  58.         imgH = height;  
  59.         pixAry = pixels;  
  60.         initCodeSize = Math.max(2, color_depth);  
  61.     }  
  62.   
  63.     void char_out(byte c, OutputStream outs) throws IOException {  
  64.         accum[a_count++] = c;  
  65.         if (a_count >= 254)  
  66.             flush_char(outs);  
  67.     }  
  68.   
  69.     void cl_block(OutputStream outs) throws IOException {  
  70.         cl_hash(hsize);  
  71.         free_ent = ClearCode + 2;  
  72.         clear_flg = true;  
  73.   
  74.         output(ClearCode, outs);  
  75.     }  
  76.   
  77.     void cl_hash(int hsize) {  
  78.         for (int i = 0; i < hsize; ++i)  
  79.             htab[i] = -1;  
  80.     }  
  81.   
  82.     void compress(int init_bits, OutputStream outs) throws IOException {  
  83.         int fcode;  
  84.         int i ;  
  85.         int c;  
  86.         int ent;  
  87.         int disp;  
  88.         int hsize_reg;  
  89.         int hshift;  
  90.         g_init_bits = init_bits;  
  91.         clear_flg = false;  
  92.         n_bits = g_init_bits;  
  93.         maxcode = MAXCODE(n_bits);  
  94.         ClearCode = 1 << (init_bits - 1);  
  95.         EOFCode = ClearCode + 1;  
  96.         free_ent = ClearCode + 2;  
  97.         a_count = 0;   
  98.         ent = nextPixel();  
  99.         hshift = 0;  
  100.         for (fcode = hsize; fcode < 65536; fcode *= 2)  
  101.             ++hshift;  
  102.         hshift = 8 - hshift;   
  103.         hsize_reg = hsize;  
  104.         cl_hash(hsize_reg);   
  105.         output(ClearCode, outs);  
  106.         outer_loop: while ((c = nextPixel()) != EOF) {  
  107.             fcode = (c << maxbits) + ent;  
  108.             i = (c << hshift) ^ ent;   
  109.             if (htab[i] == fcode) {  
  110.                 ent = codetab[i];  
  111.                 continue;  
  112.             } else if (htab[i] >= 0)  
  113.             {  
  114.                 disp = hsize_reg - i;  
  115.                 if (i == 0)  
  116.                     disp = 1;  
  117.                 do {  
  118.                     if ((i -= disp) < 0)  
  119.                         i += hsize_reg;  
  120.   
  121.                     if (htab[i] == fcode) {  
  122.                         ent = codetab[i];  
  123.                         continue outer_loop;  
  124.                     }  
  125.                 } while (htab[i] >= 0);  
  126.             }  
  127.             output(ent, outs);  
  128.             ent = c;  
  129.             if (free_ent < maxmaxcode) {  
  130.                 codetab[i] = free_ent++;  
  131.                 htab[i] = fcode;  
  132.             } else  
  133.                 cl_block(outs);  
  134.         }  
  135.         output(ent, outs);  
  136.         output(EOFCode, outs);  
  137.     }  
  138.   
  139.     void encode(OutputStream os) throws IOException {  
  140.         os.write(initCodeSize);   
  141.   
  142.         remaining = imgW * imgH;   
  143.         curPixel = 0;  
  144.   
  145.         compress(initCodeSize + 1, os);   
  146.   
  147.         os.write(0);   
  148.     }  
  149.   
  150.     void flush_char(OutputStream outs) throws IOException {  
  151.         if (a_count > 0) {  
  152.             outs.write(a_count);  
  153.             outs.write(accum, 0, a_count);  
  154.             a_count = 0;  
  155.         }  
  156.     }  
  157.   
  158.     final int MAXCODE(int n_bits) {  
  159.         return (1 << n_bits) - 1;  
  160.     }  
  161.   
  162.     private int nextPixel() {  
  163.         if (remaining == 0)  
  164.             return EOF;  
  165.   
  166.         --remaining;  
  167.   
  168.         byte pix = pixAry[curPixel++];  
  169.   
  170.         return pix & 0xff;  
  171.     }  
  172.   
  173.     void output(int code, OutputStream outs) throws IOException {  
  174.         cur_accum &= masks[cur_bits];  
  175.   
  176.         if (cur_bits > 0)  
  177.             cur_accum |= (code << cur_bits);  
  178.         else  
  179.             cur_accum = code;  
  180.   
  181.         cur_bits += n_bits;  
  182.   
  183.         while (cur_bits >= 8) {  
  184.             char_out((byte) (cur_accum & 0xff), outs);  
  185.             cur_accum >>= 8;  
  186.             cur_bits -= 8;  
  187.         }  
  188.   
  189.         if (free_ent > maxcode || clear_flg) {  
  190.             if (clear_flg) {  
  191.                 maxcode = MAXCODE(n_bits = g_init_bits);  
  192.                 clear_flg = false;  
  193.             } else {  
  194.                 ++n_bits;  
  195.                 if (n_bits == maxbits)  
  196.                     maxcode = maxmaxcode;  
  197.                 else  
  198.                     maxcode = MAXCODE(n_bits);  
  199.             }  
  200.         }  
  201.   
  202.         if (code == EOFCode) {  
  203.             while (cur_bits > 0) {  
  204.                 char_out((byte) (cur_accum & 0xff), outs);  
  205.                 cur_accum >>= 8;  
  206.                 cur_bits -= 8;  
  207.             }  
  208.   
  209.             flush_char(outs);  
  210.         }  
  211.     }  
  212. }  
0 0
原创粉丝点击