jsp制作验证码

本文介绍了一个使用Java生成带有随机数字的验证码图片的方法。该方法包括设置图片的基本属性、绘制背景和边框、添加干扰线及随机数等步骤,确保验证码难以被自动识别。适用于网站登录验证等场景。
RandomPic.Java

---------------------------------
package  et.command.pic;

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

public  class  RandomPic
{
private  final  int  width=60;//背景(图片)的宽
private  final  int  height=20;//背景(图片)的高

private  final  int  interfereLineNum  88;//干扰线个数
private  final  int  interferePointNum  88;//干扰线个数
private  final  int  interfereLineLenMax  12;//干扰线随机长度最大值
private  final  int  authenticationCodeNumber   4;//认证码的位数
//背景色的范围
private  final  int  backgroundColorBegin  =200;
private  final  int  backgroundColorFinal  =250;

private  final  int  interfereColorBegin  =160;
private  final  int  interfereColorFinal  =200;

private  final  int  radomNumberColorBegin  =20;
private  final  int  radomNumberColorFinal  =130;
//字体
private  final  String  fontName="Times  New  Roman";
private  final  int  fontStyle  Font.PLAIN;
private  final  int  fontSize  18;

private  final  int  stringWidth  =13;//string宽度坐标
private  final  int  stringHight  =16;//string高度坐标
private  final  int  stringClearance  =6;//string间距
//rgb范围
private  final  int  rgbValue  255;
//
private  Graphics  graphics;//内存中虚拟的画布

public  String  authenticationCode=new  String();//取随机产生的认证码(4位数字),对外的接口
public  BufferedImage  image;//图片的内存,对外的接口

//认证码由服务器生成
public  RandomPic(){
init();
}
//认证码客户端生成,rn为客户端生成的认证码
public  RandomPic(String  rn){
init();
authenticationCode  rn;
}

private  void  init(){
image  new  BufferedImage(width,  height,  BufferedImage.TYPE_INT_RGB);
graphics  image.getGraphics();
//System.out.println("hello");
}
public  void  drawServer(){
//画基本框架
drawBasic();
//画干扰线或者干扰点
drawInterfereLine();
//drawInterferePoint();
//画随机数
drawRadomNumberServer();
//图象生效
graphics.dispose();
}
public  void  drawClient(){
//画基本框架
drawBasic();
//画干扰线或者干扰点
drawInterfereLine();
//drawInterferePoint();
//画随机数
drawRadomNumberClient();
//图象生效
graphics.dispose();
}
//  给定范围获得随机颜色bc:begin  color  ,fc:finally  color
private  Color  getRandColor(int  fc,int  bc){
Random  random  new  Random();
if(fc>rgbValue)  fc=rgbValue;
if(bc>rgbValue)  bc=rgbValue;
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);
}

//设定图片基本信息
private  void  drawBasic(){
//设定背景色
graphics.setColor(getRandColor(backgroundColorBegin,backgroundColorFinal));
graphics.fillRect(0,  0,  width,  height);
//设定字体
graphics.setFont(new  Font(fontName,fontStyle,fontSize));
//画边框
graphics.setColor(new  Color(rgbValue,rgbValue,rgbValue));
graphics.drawRect(0,0,width-1,height-1);
}
//随机产生interfereLine条干扰线,使图象中的认证码不易被其它程序探测到
private  void  drawInterfereLine(){
Random  random  new  Random();
graphics.setColor(getRandColor(interfereColorBegin,interfereColorFinal));
for  (int  i=0;i<interfereLineNum;i++){
int  random.nextInt(width);
int  random.nextInt(height);
int  xl  random.nextInt(interfereLineLenMax);
int  yl  random.nextInt(interfereLineLenMax);
graphics.drawLine(x,y,x+xl,y+yl);
}
}
//  随机产生interfereLine条干扰点,使图象中的认证码不易被其它程序探测到
@SuppressWarnings("unused")
private  void  drawInterferePoint(){
Random  random  new  Random();
graphics.setColor(getRandColor(interfereColorBegin,interfereColorFinal));
for  (int  i=0;i<interferePointNum;i++){
int  random.nextInt(width);
int  random.nextInt(height);
graphics.drawLine(x,y,x,y);
}
}
//  取随机产生的认证码(4位数字),由服务器端生成
private  void  drawRadomNumberServer(){
Random  random  new  Random();
for  (int  i=0;i<authenticationCodeNumber ;i++){
String  rand=String.valueOf(random.nextInt(10));
authenticationCode+=rand;
graphics.setColor(getRandColor(radomNumberColorBegin,radomNumberColorFinal));
//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
graphics.drawString(rand,stringWidth*i+stringClearance,stringHight);//后两个参数为坐标
}
//System.out.println(authenticationCode);
}
//取随机产生的认证码(4位数字),由客户端生成
//<script>
//document.write("<img  border=0  src=image.jsp?rand="+Math.random()*10000+">");
//</script>
  private  void  drawRadomNumberClient(){
authenticationCode  authenticationCode.substring(0,authenticationCode.indexOf("."));
switch(authenticationCode.length()){
      case  1:  authenticationCode  "000"+authenticationCode;  break;
      case  2:  authenticationCode  "00"+authenticationCode;  break;
      case  3:  authenticationCode  "0"+authenticationCode;  break;
      default:  authenticationCode  authenticationCode.substring(0,4);  break;
}
graphics.setColor(getRandColor(radomNumberColorBegin,radomNumberColorFinal));
graphics.drawString(authenticationCode,stringWidth,stringHight);
//System.out.println("authenticationCode"+authenticationCode);
  }

 

RandomPicClient.jsp
---------------------------------
<%@ page contentType="image/jpeg"
 import="java.awt.image.*,
         javax.imageio.*,
         et.command.pic.RandomPic,
         java.io.*"%>
<%
 response.setHeader("Pragma", "No-cache");
 response.setHeader("Cache-Control", "no-cache");
 response.setDateHeader("Expires", 0);
 double rand Math.random() 10000;
 RandomPic rp new RandomPic(Double.toString(rand));
 rp.drawClient();
 session.setAttribute("rand", rp.authenticationCode);
 BufferedImage image rp.image;
 try 
 {
  ImageIO.write(image, "JPEG", response.getOutputStream());
 catch (IOException e) {
  e.printStackTrace();
 }
%> 

 

index.jsp
---------------------------------
<%@ page language="java"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> 
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
 
<html> 
 <img border='0' src='RandomPicClient.jsp'/>
</html>

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值