图形验证码

来源:互联网 发布:mac qq聊天记录文件夹 编辑:程序博客网 时间:2024/05/12 18:46
        图形验证码是现在很多服务程序为防止恶意程序而在注册或者登陆的时候增加的一种安全验证措施,用图像显示某些字符串,并添加了一定的干扰图像,保证人眼可以识别而程序无法识别。
        今天在书上看到实现图形验证码的程序,觉得只是简单实现,即不完善,写法也不规范,于是自己重写了一遍,作为锻炼,刚好贴出来,也顺便作为开 blog 第一贴。如有不妥之处,欢迎指点。

代码清单如下:
/**
 * 此类描述一个图形校验码。
 * 
 * 图形校验码通常用于注册或登录页面。
 
*/

package greenis

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;

/**
 * 
@author Greeenis
 * 
 * 
@version 1.0
 *
 * 2007/09/18
 
*/

public class ValidateCode 
{
    
// 默认图形宽度。
    private static int WIDTH = 80;
    
    
// 默认图形高度。
    private static int HEIGHT = 20;
    
    
// 默认验证码长度。
    private static int LENGTH = 4;
    
    
private char[] charMap;
    
private int width;
    
private int height;
    
private int codeLength;
    
    
/**
     * 构造方法
     * 
     * 接收四个参数,分别描述图形宽度、长度、校验码长度和校验码字符范围。
     * 构造一个用户自定义的图形验证码。
     * 
     * 
@param width
     * 
@param height
     * 
@param codeLength
     * 
@param charArrayString
     
*/

    
public ValidateCode(int width, int height, int codeLength, String charArrayString)
    
{
        
this.setWidth(width);
        
this.setHeight(height);
        
this.setCodeLength(codeLength);
        
this.setCharMap(charArrayString);
    }

    
    
/**
     * 构造方法
     * 
     * 接收一个参数:描述验证码码字符范围。
     * 构造一个图形宽度为80像素,高度为20像素,验证码长度为4位的图形验证码。
     * 
     * 
@param charArrayString
     
*/

    
public ValidateCode(String charArrayString)
    
{
        
this(000, charArrayString);
    }

    
    
/**
     * 构造方法
     * 
     * 构造一个图形宽度为80像素,高度为20像素,验证码长度为4为,
     * 验证码范围为“a~z”和“0~9”之间的图形验证码。
     
*/

    
public ValidateCode()
    
{
        
this(000"");
    }

    
    
public char[] getCharMap() {
        
return charMap;
    }


    
public void setCharMap(String charArrayString) {
        
if(charArrayString == "")
        
{
            charArrayString 
= "abcdefghigkLmnopqrstuvwxyz1234567890";
            charMap 
= charArrayString.toCharArray();
        }

        
else
        
{
            charMap 
= charArrayString.toCharArray();
        }

    }


    
public int getCodeLength() {
        
return codeLength;
    }


    
public void setCodeLength(int codeLength) {
        
if(codeLength <= 0)
        
{
            
this.codeLength = LENGTH;
        }

        
else
        
{
            
this.codeLength = codeLength;
        }

    }


    
public int getHeight() {
        
return height;
    }


    
public void setHeight(int height) {
        
if(height <= 0)
        
{
            
this.height = HEIGHT;
        }

        
else
        
{
            
this.height = height;
        }

    }


    
public int getWidth() {
        
return width;
    }


    
public void setWidth(int width) {
        
if(width <= 0)
        
{
            
this.width = WIDTH;            
        }

        
else
        
{
            
this.width = width;
        }

    }

    
    
/**
     * 指定输出流,生成一个图形验证码并返回相应的验证码字符串。
     * 
     * 
@param output
     * 
@return validationCodeString
     
*/

    
public String getValidateCode(OutputStream output)
    
{
        
// 验证码字符串。
        String validateCodeString = "";
        
        
// 图形验证码。
        BufferedImage image = 
            
new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
        Graphics graphics 
= image.getGraphics();
        
        
// 设置背景色。
        graphics.setColor(new Color(0xDCDCDC));
        graphics.fillRect(
00this.width, this.height);
        
        
// 设置边框。
        graphics.setColor(Color.BLACK);
        graphics.drawRect(
00this.width-1this.height-1);
        
        
// 随机生成 codeLength 位验证码
        for(int index = 0; index < this.codeLength; index ++)
        
{
            Random random 
= new Random();
            validateCodeString 
+= this.charMap[random.nextInt(charMap.length - 1)];
        }

        graphics.setColor(Color.BLACK);
        graphics.setFont(
new Font("Atlantic Inline", Font.PLAIN, (this.height * 9 / 10)));
        
for(int index = 0; index < (this.codeLength); index ++)
        
{
            String subValidateCodeString 
= "";
            subValidateCodeString 
= validateCodeString.substring(index, index + 1);
            
int baseX = (int)(this.width / this.codeLength * index + 5);
            
int baseY = (int)(this.height * 9 / 10);
            graphics.drawString(subValidateCodeString, baseX, baseY);
        }

        
        
// 根据图形面积不同,按照 1:50 的比例,随机生成个椭圆干扰点。
        for(int index = 0; index < ((int)(this.width * this.height / 50)); index ++)
        
{
            Random random 
= new Random();
            
int x = random.nextInt(this.width);
            
int y = random.nextInt(this.height);
            
int ovalWidth = random.nextInt(4);
            
int ovalHeight = random.nextInt(4);
            graphics.drawOval(x, y, ovalWidth, ovalHeight);
        }

        
        
// 释放验证码图形。
        graphics.dispose();
        
try {
            ImageIO.write(image, 
"JPEG", output);
        }
 catch (IOException e) {
            e.printStackTrace();
            
return "";
        }

        
        
return validateCodeString;
    }

}