求java语言随机生成图片验证码的程序代码,有详细注释最好,本人初学者,谢谢各位的帮忙!!!
3个回答
展开全部
楼上正解
/**
* 图片验证码
*
* @author HuangYong
*
*/
public class CheckCode extends HttpServlet {
private static final long serialVersionUID = 1532787236715859525L;
/**
* 验证图片宽
*/
private static final int WIDTH = 80;
/**
* 验证图片高度
*/
private static final int HEIGHT = 20;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获得图片
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
// 获取画笔
Graphics g = image.getGraphics();
// 设置边框
setBounds(g);
// // 设置背景
// setBackGround(g);
// 干扰线
drawRandomLine(g);
// 验证码
drawRandomString((Graphics2D) g, request);
// 发送给页面
response.setContentType("image/jpeg");
response.setDateHeader("expries", -1);
response.setHeader("cache-control", "no-cache");
response.setHeader("progma", "no-cache");
ImageIO.write(image, "jpg", response.getOutputStream());
}
/**
* 边框
*
* @param g
*/
private void setBounds(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.WHITE);
g.fillRect(1, 1, WIDTH - 2, HEIGHT - 2);
}
/**
* 背景
*
* @param g
*/
// private void setBackGround(Graphics g) {
//
// g.setColor(Color.WHITE);
// g.drawRect(1, 1, WIDTH-2, HEIGHT-2);
//
// }
/**
* 干扰线
*
* @param g
*/
private void drawRandomLine(Graphics g) {
g.setColor(Color.GREEN);
int x1, y1;
int x2, y2;
Random r = new Random();
for (int i = 0; i < 5; i++) {
// 起始xy
x1 = r.nextInt(WIDTH);
y1 = r.nextInt(HEIGHT);
// 结束xy
x2 = r.nextInt(WIDTH);
y2 = r.nextInt(HEIGHT);
g.drawLine(x1, y1, x2, y2);
}
}
/**
* 验证码
*
* @param g
*/
private void drawRandomString(Graphics2D g, HttpServletRequest request) {
g.setColor(Color.RED);
g.setFont(new Font("宋体", Font.BOLD, 20));
String base = "qwertyupasdfghjkzxcvbnm23456789QWERTYUIOPASDFGHJKLZXCVBNM";
StringBuilder sb = new StringBuilder();
char ch = '\u0000';
int degree = 0;
Random r = new Random();
int startX = 8;
for (int i = 0; i < 4; i++) {
ch = base.charAt(r.nextInt(base.length()));
// 设置旋转, ±20°
degree = r.nextInt() % 20;
g.rotate(degree * Math.PI / 180, startX, 14);
g.drawString(ch + "", startX, 16);
// 取消旋转
g.rotate(-degree * Math.PI / 180, startX, 14);
startX += 18;
sb.append(ch);
}
request.getSession().setAttribute("checkCode", sb.toString().toLowerCase());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
希望能帮助你
展开全部
//设置页面无缓存
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
int width=60, height = 20;
//构造BufferedImage
BufferedImage 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.BOLD, 18));
g.setColor(getRandColor(160, 200));
//随机画线
for (int i = 0; i < 150; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(20);
int yl = random.nextInt(20);
g.drawLine(x, y, x + xl, y + yl);
}
//随机生成验证码,然后画在画板上
String sRand = "";
for (int i = 0; i < 4; i++) {
String rand = String.valueOf(random.nextInt(10));
sRand += rand;
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
g.drawString(rand, 13 * i + 6, 16);
}
// 将认证码存入SESSION
session.setAttribute("validateCode", sRand);
g.dispose();
//将BufferedImage写入到响应流中
ImageIO.write(image, "JPEG", response.getOutputStream());
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-03-01
展开全部
李克平【看来可怕【
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询