A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 刘兆华 于 2013-5-2 21:01 编辑

例如 web上的注册验证码   思路和需要用到的类麻烦讲述一下   如果有注释源码  贴上来看看吧?

3 个回复

倒序浏览
  1. <%@ page contentType="image/JPEG"
  2. import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"
  3. pageEncoding="GBK"%><%!Color getRandColor(int fc, int bc) {
  4. Random random = new Random();
  5. if (fc > 255)
  6. fc = 255;
  7. if (bc > 255)
  8. bc = 255;
  9. int r = fc + random.nextInt(bc - fc);
  10. int g = fc + random.nextInt(bc - fc);
  11. int b = fc + random.nextInt(bc - fc);
  12. return new Color(r, g, b);
  13. }%><%

  14. response.setHeader("Pragma", "No-cache");
  15. response.setHeader("Cache-Control", "no-cache");
  16. response.setDateHeader("Expires", 0);

  17. int width = 60, height = 20;
  18. BufferedImage image = new BufferedImage(width, height,
  19. BufferedImage.TYPE_INT_RGB);

  20. Graphics g = image.getGraphics();

  21. Random random = new Random();

  22. g.setColor(getRandColor(200, 250));
  23. g.fillRect(0, 0, width, height);

  24. g.setFont(new Font("Times New Roman", Font.PLAIN, 18));

  25. g.setColor(getRandColor(160, 200));
  26. for (int i = 0; i < 100; i++) {
  27. int x = random.nextInt(width);
  28. int y = random.nextInt(height);
  29. int xl = random.nextInt(12);
  30. int yl = random.nextInt(12);
  31. g.drawLine(x, y, x + xl, y + yl);
  32. }

  33. String sRand = "";
  34. for (int i = 0; i < 4; i++) {
  35. String rand = String.valueOf(random.nextInt(10));
  36. sRand += rand;

  37. g.setColor(new Color(20 + random.nextInt(110), 20 + random
  38. .nextInt(110), 20 + random.nextInt(110)));
  39. g.drawString(rand, 13 * i + 6, 16);
  40. }

  41. session.setAttribute("code", sRand);

  42. g.dispose();

  43. ImageIO.write(image, "JPEG", response.getOutputStream());
  44. %>

  45. 在你需要引入的地方:
  46. <img border=0 src="image.jsp">

  47. 就可以了。。。
复制代码
回复 使用道具 举报
ValidateCode.rar (6.93 KB, 下载次数: 5)
回复 使用道具 举报


如果仍有问题,请继续追问,如果问题已解决,请将分类改为已解决,谢谢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马