无意中在网上看到了这种算式类型的图形验证码,觉得不错。又写了一个练练。
代码如下:
代码如下:
/**
* 随机生成一个加法或乘法的算术表达式,
* 为其绘制一个图形验证码,并返回该表达式的结果。
*/
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;
default: this.result = -1; break;
}
}
/**
* 生成包含算术表达式的图形验证码,并返回表达式结果。
*
* @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(0, 0, this.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, 6, 20);
// 随机生成干扰点。
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);
}
}
* 随机生成一个加法或乘法的算术表达式,
* 为其绘制一个图形验证码,并返回该表达式的结果。
*/
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;
default: this.result = -1; break;
}
}
/**
* 生成包含算术表达式的图形验证码,并返回表达式结果。
*
* @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(0, 0, this.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, 6, 20);
// 随机生成干扰点。
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);
}
}