Chinaunix首页 | 论坛 | 博客
  • 博客访问: 342320
  • 博文数量: 88
  • 博客积分: 1673
  • 博客等级: 上尉
  • 技术积分: 934
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-20 13:51
文章分类

全部博文(88)

文章存档

2016年(1)

2015年(4)

2014年(3)

2013年(7)

2012年(11)

2011年(1)

2009年(61)

我的朋友

分类: Java

2015-12-30 17:32:16


点击(此处)折叠或打开

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%@ page import="java.awt.*,java.awt.image.*,javax.imageio.*"%>
  3. <jsp:directive.page import="java.io.OutputStream"/>
  4. <jsp:directive.page import="com.sun.image.codec.jpeg.JPEGImageEncoder"/>
  5. <jsp:directive.page import="com.sun.image.codec.jpeg.JPEGCodec"/>
  6. <%
  7. /*
  8. 使用方法:在需要显示验证码的html代码中使用
  9. 在需判断session的时候判断session.getAttribute("vcode")
  10. */try {
  11.    int codeLength = 5;//验证码长度
  12.    int mixTimes = 250;//模糊程度参数
  13.    Color bgColor = getRandColor(200, 250);//背景颜色
  14.    Color bfColor = new Color(0, 0, 0);//字体颜色
  15.    boolean ifRandomColor = true;//单个字符是否颜色随机
  16.    boolean ifMixColor = true;//模糊线是否颜色随机

  17.    //设置页面不缓存
  18.    response.setHeader("Pragma", "No-cache");
  19.    response.setHeader("Cache-Control", "no-cache");
  20.    response.setDateHeader("Expires", 0);
  21.    response.setContentType("image/png");
  22.   
  23.    // 在内存中创建图象
  24.    int width = 18 * codeLength + 6, height = 20;
  25.    BufferedImage image = new BufferedImage(width, height,
  26.    BufferedImage.TYPE_INT_RGB);
  27.    // 获取图形上下文
  28.    Graphics g = image.getGraphics();
  29.    // 设定背景色
  30.    g.setColor(bgColor);
  31.    g.fillRect(0, 0, width, height);
  32.    //设定字体
  33.    g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
  34.    //画边框
  35.    g.setColor(new Color(33, 66, 99));
  36.    g.drawRect(0, 0, width - 1, height - 1);
  37.    // 随机产生干扰线,使图象中的认证码不易被其它程序探测到
  38.    g.setColor(getRandColor(160, 200));
  39.    for (int i = 0; i < mixTimes * codeLength / 10; i++) {
  40.     if (ifMixColor) {
  41.      g.setColor(getRandColor(160, 200));
  42.     }
  43.     int x = random.nextInt(width);
  44.     int y = random.nextInt(height);
  45.     int xl = random.nextInt(12);
  46.     int yl = random.nextInt(12);
  47.     g.drawLine(x, y, x + xl, y + yl);
  48.    }
  49.    // 取随机产生的认证码(4位数字)
  50.    String sRand = "";
  51.    for (int i = 0; i < codeLength; i++) {
  52.     String rand = String.valueOf(random.nextInt(10));
  53.     sRand += rand;
  54.     // 将认证码显示到图象中
  55.     if (ifRandomColor)
  56.      g.setColor(getRandColor(20, 110, 0));
  57.     else
  58.      g.setColor(bfColor);
  59.     //调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
  60.     g.drawString(rand, 18 * i + 6, 16);
  61.    }
  62.    // 将认证码存入SESSION
  63.    session.setAttribute("rand", sRand);
  64.    // 图象生效
  65.    g.dispose();
  66.    // 输出图象到页面
  67.    //ImageIO.write(image, "PNG", response.getOutputStream());
  68.   
  69.    OutputStream toClient = response.getOutputStream();
  70.   
  71.         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(toClient);
  72.         encoder.encode(image);

  73.    toClient.close();
  74.         out.clear();

  75.    out = pageContext.pushBody();
  76. } catch (Exception e) {
  77.    e.printStackTrace();
  78. }
  79. %>
  80. <%!//给定范围获得随机颜色
  81. private static Random random = new Random();

  82. private Color getRandColor(int fc, int bc) {
  83.    return getRandColor(fc, bc, fc);
  84. }

  85. private Color getRandColor(int fc, int bc, int interval) {
  86.    if (fc > 255) {
  87.     fc = 255;
  88.    }
  89.    if (bc > 255) {
  90.     bc = 255;
  91.    }
  92.    int r = fc + random.nextInt(bc - interval);
  93.    int g = fc + random.nextInt(bc - interval);
  94.    int b = fc + random.nextInt(bc - interval);
  95.    return new Color(r, g, b);
  96. }%>

阅读(475) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~