随笔-6  评论-38  文章-40  trackbacks-0

这是一个公用文件
package com.zeroidea.common;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.Random;

public class MakeImageAndVerifyCode {

 private java.lang.String verifyCode;

 private java.awt.image.BufferedImage image;

 /**
  * 构建器
  */
 public MakeImageAndVerifyCode() {
  verifyCode = "";
  image = new BufferedImage(60, 20, BufferedImage.TYPE_INT_RGB);
  makeImageAndVerifyCode();
 }

 /**
  * 设置验证码
  *
  * @param verifyCode
  */
 private void setVerifyCode(java.lang.String verifyCode) {
  this.verifyCode = verifyCode;
 }

 /**
  * 生成器
  */
 private void makeImageAndVerifyCode() {
  Graphics graphics = image.getGraphics();
  graphics.fillRect(0, 0, 60, 20);
  java.lang.String nowChar = "";
  graphics.setFont(new Font("Times New Roman", 1, 12));
  for (int i = 0; i < 4; i++) {
   java.lang.String nowCharTemp = getRandInt(10).toString();
   graphics.setColor(getRandColor(100, 200));
   graphics.drawString(nowCharTemp, 1 + i * 15
     + getRandInt(7).intValue(), 12 + getRandInt(8).intValue());
   nowChar = (new StringBuilder(java.lang.String.valueOf(nowChar)))
     .append(nowCharTemp).toString();
  }

  for (int i = 0; i < 88; i++) {
   graphics.setColor(getRandColor(225, 255));
   java.awt.Point point = getRandPoint(60, 20);
   graphics.drawOval(point.x, point.y, 0, 0);
  }

  graphics.setColor(getRandColor(0, 255));
  graphics.drawRect(0, 0, 59, 19);
  graphics.dispose();
  setVerifyCode(nowChar);
 }

 /**
  * 返回验证代码
  *
  * @return String
  */
 public java.lang.String getVerifyCode() {
  return verifyCode;
 }

 /**
  * 返回验证代码图片
  *
  * @return BufferedImage
  */
 public java.awt.image.BufferedImage getImage() {
  return image;
 }

 /**
  * 生成随机颜色
  *
  * @param fc
  * @param bc
  * @return Color
  */
 private java.awt.Color getRandColor(int fc, int bc) {
  java.util.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);
 }

 /**
  * 生成随机数
  *
  * @param i
  * @return Integer
  */
 private java.lang.Integer getRandInt(int i) {
  java.util.Random random = new Random();
  return java.lang.Integer.valueOf(random.nextInt(i));
 }

 /**
  * 生成随机点
  *
  * @param x
  * @param y
  * @return Point
  */
 private java.awt.Point getRandPoint(int x, int y) {
  java.util.Random random = new Random();
  int x_temp = random.nextInt(x);
  int y_temp = random.nextInt(y);
  java.awt.Point point = new Point(x_temp, y_temp);
  return point;
 }
}


生成验证图片并将验证码写入 Session
/**
 * 验证图片产生
 * 作者: 刘万林
 */

package com.zeroidea.pwms.validate;

import java.awt.image.BufferedImage;
import java.io.IOException;


import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zeroidea.common.MakeImageAndVerifyCode;

public class ValidateImage extends HttpServlet {

 /**
  *
  */
 private static final long serialVersionUID = 1L;

 /**
  * Constructor of the object.
  */
 public ValidateImage() {
  super();
 }

 /**
  * Destruction of the servlet. <br>
  */
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }

 /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  response.setHeader("Pragma", "No-cache");
  response.setHeader("Cache-Control", "no-cache");
  response.setDateHeader("Expires", 0);
  MakeImageAndVerifyCode makeImageAndVerifyCode = new MakeImageAndVerifyCode();
  BufferedImage image = makeImageAndVerifyCode.getImage();
  String verifyCode = makeImageAndVerifyCode.getVerifyCode();
  //System.out.println(verifyCode);
  // 将验证码写入 Session
  request.getSession().setAttribute("verifyCode", verifyCode);
  // 以下语句将图像写入 响应输出流
  ImageIO.write(image, "JPEG", response.getOutputStream());
 }

 /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException if an error occure
  */
 public void init() throws ServletException {
  // Put your code here
 }

}


效果图:logon.JPG

posted on 2006-11-28 20:11 一手的小窝窝 阅读(205) 评论(0)  编辑  收藏 所属分类: JAVA

只有注册用户登录后才能发表评论。


网站导航: