int width = 97;
int height = 46;
StringBuffer bu=new StringBuffer();
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); //创建BufferedImage类的对象
Graphics g = image.getGraphics(); //创建Graphics类的对象
//Graphics2D g2d=(Graphics2D)g; //通过Graphics类的对象创建一个Graphics2D类的对象
Random random = new Random(); //实例化一个Random对象
Font mFont = new Font("华文宋体", Font.BOLD, 30); //通过Font构造字体
g.setColor(ImageUtils.getRandColor(200, 250)); //改变图形的当前颜色为随机生成的颜色
g.fillRect(0, 0, width , height); //绘制一个填色矩形
//画一条折线
//BasicStroke bs=new BasicStroke(2f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL); //创建一个供画笔选择线条粗细的对象
//g2d.setStroke(bs); //改变线条的粗细
//g.setColor(Color.DARK_GRAY); //设置当前颜色为预定义颜色中的深灰色
//int[] xPoints=new int[3];
//int[] yPoints=new int[3];
//for(int j=0;j<3;j++){
//xPoints[j]=random.nextInt(width - 1);
//yPoints[j]=random.nextInt(height - 1);
//}
//g.drawPolyline(xPoints, yPoints,3);
//生成并输出随机的验证文字
g.setFont(mFont);
int itmp=0;
for(int i=0;i<4;i++){
if(random.nextInt(2)==1){
itmp=random.nextInt(26)+65; //生成A~Z的字母
}else{
itmp=random.nextInt(10)+48; //生成0~9的数字
}
char ctmp=(char)itmp;
bu.append(ctmp);
Color color=new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110));
g.setColor(color);
/****随机缩放文字并将文字旋转指定角度**/
//将文字旋转指定角度
Graphics2D g2d_word=(Graphics2D)g;
AffineTransform trans=new AffineTransform();
trans.rotate(random.nextInt(45)*3.14/180,15*i+10,7);
//缩放文字
float scaleSize=random.nextFloat()+0.8f;
if(scaleSize>1.1f) scaleSize=1f;
trans.scale(scaleSize, scaleSize);
g2d_word.setTransform(trans);
/************************/
g.drawString(String.valueOf(ctmp),13 * i + 23, 29);
}
g.dispose();
req.getSession().setAttribute("verifyCode",bu.toString());
res.setHeader("Pragma", "no-cache");
res.setHeader("Cache-Control", "no-cache");
res.setDateHeader("Expires", 0);
res.setContentType("image/jpeg");
ImageIO.write(image, "jpeg", res.getOutputStream());
/**
* 产生随机颜色
*
* @param num1
* @param num2
* @return Color
*/
public static Color getRandColor(int num1, int num2) {
Random random = new Random();
if (num1 > 255)
num1 = 255;
if (num2 > 255)
num2 = 255;
int r = num1 + random.nextInt(num2 - num1);
int g = num1 + random.nextInt(num2 - num1);
int b = num1 + random.nextInt(num2 - num1);
return new Color(r, g, b);
}