另一个图形验证码(算式类型的)

        无意中在网上看到了这种算式类型的图形验证码,觉得不错。又写了一个练练。
代码如下:
/**
 * 随机生成一个加法或乘法的算术表达式,
 * 为其绘制一个图形验证码,并返回该表达式的结果。
 
*/

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 Greenis
 * @date 2007-10-8
 * 
@version 1.0
 
*/

public   class  ArithmeticValidateCode
{    
    
// 数字集合。
    private int[] number = {0,1,2,3,4,5,6,7,8,9};
    
// 运算符集合。
    private char[] operator = {'+','×'};
    
// 图片宽度。
    private int width = 50;
    
// 图片高度。
    private int height = 20;
    
// 算术表达式。
    private String expression;
    
// 计算结果。
    private int result;
    
    
/**
     * 生成算数表达式和其对应的结果。
     
*/

    
public ArithmeticValidateCode()
    
{
        
int num1 = this.number[(int) (number.length * Math.random())];
        
int num2 = this.number[(int) (number.length * Math.random())];
        
char oper = this.operator[(int) (operator.length * Math.random())];
        
this.expression = Integer.toString(num1) + Character.toString(oper) + Integer.toString(num2) + "=";
        
        
switch(oper)
        
{
        
case '+' : this.result = num1 + num2; break;
        
case '×' : this.result = num1 * num2; break;
        
defaultthis.result = -1break;
        }

    }

    
    
/**
     * 生成包含算术表达式的图形验证码,并返回表达式结果。
     * 
     * 
@param os
     * 
@return result
     
*/

    
public String getValidateCode(OutputStream os)
    
{
        
// 创建图像。
        BufferedImage image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
        Graphics graphics 
= image.getGraphics();
        
        
// 设置图像背景色。
        graphics.setColor(Color.WHITE);
        graphics.fillRect(
00this.width, this.height);
        
        
// 设置图像边框。
        
//graphics.setColor(Color.BLACK);
        
//graphics.drawRect(0, 0, this.width - 1, this.height - 1);
        
        
// 在图像中生成字符串。
        graphics.setColor(Color.BLACK);
        graphics.setFont(
new Font("Atlantic Inline", Font.PLAIN, 20));
        graphics.drawString(
this.expression, 620);
        
        
// 随机生成干扰点。
        Random random = new Random();
        
for(int i = 0; i < 15; i ++)
        
{
            
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", os);
        }
 catch (IOException e) {
            e.printStackTrace();
            
this.result = -1;
        }

        
        
//System.out.println(this.expression);
        
//System.out.println(this.result);
        return Integer.toString(this.result);
    }

}



      
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值