验证码的变换

复制代码
//验证码
		public String imageCode() throws IOException{
			//获得验证码随机数
			String code = ImageUtil1.getRandomCode(4);
			BufferedImage img = ImageUtil1.createImage(code);
			ValueStackUtil.setSessionAttribute("imgCode", code);
			ImageIO.write(img, "png", ServletActionContext.getResponse().getOutputStream());
			return null;
		}




点击验证码变换


		<tr>
			<td valign="middle" align="right">
				验证码:
				<img id="num" src="<s:url value='/user/user_imageCode'/>" οnclick='this.src=this.src+"?c="+Math.random()'/>
			</td>
			<td valign="middle" align="left">
				<input id="code" type="text" required="true" class="inputgri" name="code" />
			</td>
		</tr>






点击换一张更换验证码
		<script type="text/javascript">
			function fun(){
					var img1 = document.getElementById("num");
					img1.src = "<s:url value='/user/user_imageCode'/>;" + new Date();
	    	  		    } 
		</script>




		<tr>
			<td valign="middle" align="right">
			验证码:
				<img id="num" src="<s:url value='/user/user_imageCode'/>" />
					<a href="javascript:void(0)" οnclick="fun()">换一张</a>
			</td>


			<td valign="middle" align="left">
				<input type="text" class="inputgri" name="code" />
			</td>
		</tr>


	验证码的util方法:
	public class ImageUtil {
	public static String getRandomCode(int n){
		String str = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
		int len = str.length();
		StringBuffer sb = new StringBuffer();
		Random r = new Random();
		for(int i=0;i<n;i++){
			sb.append(str.charAt(r.nextInt(len)));
		}
		return sb.toString();
	}


	 public static BufferedImage createImage(String securityCode){


	        int codeLength = securityCode.length();//验证码长度


	        int fontSize = 20;//字体大小


	        int fontWidth = fontSize+1;


	        //图片宽高


	        int width = codeLength*fontWidth+60;
	        int height = fontSize*2;
	        //图片


	        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);


	        Graphics2D g = image.createGraphics();


	        g.setColor(Color.WHITE);//设置背景色


	        g.fillRect(0, 0, width, height);//填充背景


	        g.setColor(Color.LIGHT_GRAY);//设置边框颜色


	        g.setFont(new Font("Courier New", Font.BOLD, height-2));//边框字体样式


	        g.drawRect(0, 0,0, 0);//绘制边框


	        //绘制噪点


	        Random rand = new Random();


	        g.setColor(Color.LIGHT_GRAY);


	        for (int i = 0; i < codeLength*6; i++) {


	            int x = rand.nextInt(width);


	            int y = rand.nextInt(height);


	            g.drawRect(x, y, 1, 1);//绘制1*1大小的矩形


	        }


	        //绘制验证码


	        int codeY = height-10;


	        g.setColor(new Color(19,148,246));


	        g.setFont(new Font("Georgia", Font.BOLD, fontSize));
	        for(int i=0;i<codeLength;i++){
	        	double deg=new Random().nextDouble()*20;
	        	g.rotate(Math.toRadians(deg), i*16+13,codeY-7.5);
	            g.drawString(String.valueOf(securityCode.charAt(i)), i*16+5, codeY);
	            g.rotate(Math.toRadians(-deg), i*16+13,codeY-7.5);
	        }
	       
	        g.dispose();//关闭资源


	        return image;


	    }
	
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值