javascript验证码文字图片制作

package org.accp.ajax;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
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;

@SuppressWarnings("serial")
public class Action4 extends HttpServlet{
 //定义随机生成的字符数组
 char[] code = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
            'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
            'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
            'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
            'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };  
 //创建随机数生成器
 Random rand = new Random();
 //定义获取指定范围内的随机颜色
 Color getRandColor(int fc, int bc) {
        if (fc > 255) {
            fc = 255;
        }
        if (bc > 255) {
            bc = 255;
        }
        int n = Math.abs(fc - bc);
        int r = fc + rand.nextInt(n);
        int g = fc + rand.nextInt(n);
        int b = fc + rand.nextInt(n);
        return new Color(r, g, b);
    }

 Color getRandColor() {
        return getRandColor(0, 255);
    }
 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  doPost(req, resp);
 }
 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
   // 设置页面,禁止图像缓存
  resp.setHeader("Pragma", "No-cache");
  resp.setHeader("Cache-Control", "no-cache");
  resp.setDateHeader("Expires", 0);

     int width = 80; // 图像宽度
     int height = 20; // 图像高度
     int codeCount = 4; // 验证码长度

     // 创建验证码图像image
     BufferedImage image = new BufferedImage(width, height,
             BufferedImage.TYPE_INT_RGB);
     Graphics2D g = image.createGraphics();

     // 设置验证码背景颜色
     g.setColor(getRandColor(216, 250));
     g.fillRect(0, 0, width, height);

     // 设置验证码字体
     g.setFont(new Font("Times New Roman", Font.PLAIN, height - 2));

     // 设置验证码边框
     //g.setColor(Color.BLACK);
     //g.drawRect(0, 0, width - 1, height - 1);

     // 随机生成160条干扰线
     g.setColor(getRandColor(180, 200));
     for (int i = 0; i < 160; i++) {
         int x = rand.nextInt(width);
         int y = rand.nextInt(height);
         int xl = rand.nextInt(12);
         int yl = rand.nextInt(12);
         g.drawLine(x, y, x + xl, y + yl);
     }

     // 用一个randomCode来保存验证码
     StringBuffer randomCode = new StringBuffer();

     // 随机生成验证码
     for (int i = 0; i < codeCount; i++) {
         String randStr = String.valueOf(code[rand.nextInt(62)]);
         g.setColor(getRandColor());
         g.drawString(randStr, (rand.nextFloat() + 18) * i + 5, (rand
         .nextFloat() + 3.5f) * 4);
         randomCode.append(randStr); // 将随机产生的字符合成一个字符串
     }
        System.out.println(randomCode.toString());
     // 将产生的验证码保存到session中
     req.getSession().setAttribute("validatecode", randomCode.toString());

     // 使图像生效
     g.dispose();

     // 将验证码图像输出到页面
     ImageIO.write(image, "JPEG", resp.getOutputStream());
     resp.getOutputStream().close();

 }
}

 

/****************************************************************************************************/

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme() + "://"
   + request.getServerName() + ":" + request.getServerPort()
   + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <base href="<%=basePath%>">

  <title>My JSP 'usingvalidatecode.jsp' starting page</title>

  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

 </head>

 <body>
  <script type="text/javascript">
 function show(o) {
  // 重新载入验证码
  var timenow = new Date().getTime();
  o.src = "Action4?d=" + timenow;
 }
</script>

  验证码:
  <div>${validatecode}</div>
  <img src="Action4" name="rand" id="rand" />
  <a
   href="javascript:show(document.getElementById('rand'))">看不清?</a>
 </body>
</html>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值