使用JAVA生成四位随机验证码图片 (2012-04-05 23:28:47)
标签: 字符串 干扰 验证码 内存中 字母 随机 it 分类: JAVA类代码原创
效果:是第一位和第三位是随机数字,第二位和第四位是随机字母,图片中还有随机干扰线,这些数字,字母,干扰线的颜色均随机
package cn.itcast.web.response;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//生成验证码图片
public class Demo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
int WIDTH = 100;
int HIGHT = 30;
//在内在中创建一幅图片
BufferedImage image = new BufferedImage(WIDTH,HIGHT,BufferedImage.TYPE_INT_RGB);
//取得一个画笔
Graphics g = image.getGraphics();
//设置字符串格式
g.setFont(new Font("黑体",Font.BOLD,24));
//画干扰线
for(int i=0;i<7;i++){
Random r = new Random();
int x1 = r.nextInt(WIDTH);
int x2 = r.nextInt(WIDTH);
int y1 = r.nextInt(HIGHT);
int y2 = r.nextInt(HIGHT);
Color color = new Color(
(new Double(Math.random() * 128)).intValue() + 128,
(new Double(Math.random() * 128)).intValue() + 128,
(new Double(Math.random() * 128)).intValue() + 128);
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
}
//得到一个随机颜色
Color color = new Color(
(new Double(Math.random() * 128)).intValue() + 128,
(new Double(Math.random() * 128)).intValue() + 128,
(new Double(Math.random() * 128)).intValue() + 128);
g.setColor(color);
//画字符串
g.drawString(getVaule(), WIDTH/5, 2*HIGHT/3);
//blog.sina.com.cn/flyzhouw
Color color1 = new Color(
(new Double(Math.random() * 128)).intValue() + 128,
(new Double(Math.random() * 128)).intValue() + 128,
(new Double(Math.random() * 128)).intValue() + 128);
g.setColor(color1);
g.drawString(getChar(), WIDTH/3, 2*HIGHT/3);
Color color2 = new Color(
(new Double(Math.random() * 128)).intValue() + 128,
(new Double(Math.random() * 128)).intValue() + 128,
(new Double(Math.random() * 128)).intValue() + 128);
g.setColor(color2);
g.drawString(getVaule(), WIDTH/2, 2*HIGHT/3);
Color color3 = new Color(
(new Double(Math.random() * 128)).intValue() + 128,
(new Double(Math.random() * 128)).intValue() + 128,
(new Double(Math.random() * 128)).intValue() + 128);
g.setColor(color3);
g.drawString(getChar(), 2*WIDTH/3, 2*HIGHT/3);
//将内存中的图片,输出浏览器
ImageIO.write(image, "JPG", response.getOutputStream());
}
private String getVaule() {
Random r = new Random();
int num = r.nextInt(10);
return num + "";
}
private String getChar()
{
Random random =new Random();
int r = 0 ;
while(true)
{
r = random.nextInt(57) + 65;
if(r>90&&r<97||r==0)continue;
break;
}
char a = (char)r;
return a + "";
}
}
*************************************************
建立用以生成验证码的image.jsp 代码
<%@page contentType="image/jpeg" import="java.awt".*,
java.awt.image.*,java.util.*,javax.image.*" pageEncoding="GB2312" %>
<%! Color getRandColor(int fc,int bc) { //给定范围获取随机颜色
Random random = new Random();
if(fc>255) fc=255;
if(bc>255) bc=255
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
%>
<%//设置页面不缓存
response.setHeader("Pragma","no-cache");
response.setHeader("Cache-cotrol","no-cache");
response.setDateHeader("Expries",0);
//在内存中创建图像
int width=60 ,height=20;
BufferedIamge image= new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//获取图形上下文
Graphics g= image.getGraphics();
//生成随机类
Random random = new Random();
//设定背景色
g.setColor(getRandColor(200,250));
g.fillRect(0,0,width,height);
//设定字体
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
//随机产生155条干扰线,使图像的验证码不易被其他程序探测到
g.setColor(getRandColor(160,200));
for(int i=0;i<155;i++) {
int x=random.nextInt(width);
int y = random.nextInt(height);
int x1=random.nextInt(12);
int y1= random.nextInt(12);
g.drawline(x,y,x+x1,y+y1);
}
//取随机产生的验证码(四位数)
String sRand ="";
for(int i=0;i<4;i++) {
String rand=String.valueOf(random.nextInt(10));
sRand+=rand;
//将验证码显示到图像中
g.getColor(new Color(20+random.nextInt(110));20+random.nextInt(110);20+random.nextInt(110));
//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.drawString(rand,13*i+6,16);
}
//将验证码保存到Session
session.setAttribute("rand",sRand);
//图像生效
g.dispose();
//输出图像到页面
ImageIO.write(image,"JPEG",response.getOutputStream());
out.clear();
out=pageContext.pushBody();
%>