Gif动图验证码,Jpg静图验证码

依赖包:103915_DV1b_2507499.png

captcha1.0.jar 包是我自己打的maven中找不到的。

下载地址:https://pan.baidu.com/s/1o8Dr2nK

使用方式:

动态图:

	/**
	 * @Title:获取验证码图片(Gif版本)
	 * @Description:返回图片流到response中
	 * @author 张颖辉
	 * @date 2017年1月4日上午10:10:30
	 * @param request
	 * @param response
	 */
	@RequestMapping(value="getGifVerifyCode2Stream",method=RequestMethod.GET)
	public void getGifVerifyCode2Stream(Integer w,Integer h,HttpServletResponse response,HttpServletRequest request){
		try {
			response.setHeader("Pragma", "No-cache");  
	        response.setHeader("Cache-Control", "no-cache");  
	        response.setDateHeader("Expires", 0);  
	        response.setContentType("image/gif");  
	        /**
	         * gif格式动画验证码
	         * 宽,高,位数。
	         */
	        
	        // 生成图片
	        w=w==null?200:w;
			h=h==null?80:h;
	        Captcha captcha = new GifCaptcha(w,h,4);
	        //输出
	        captcha.out(response.getOutputStream());
	        HttpSession session = request.getSession(true);  
	        //存入Session
	        session.setAttribute("pic_verifyCode",captcha.text().toLowerCase());  
		} catch (Exception e) {
			//LoggerUtils.fmtError(getClass(),e, "获取验证码异常:%s",e.getMessage());
			logger.error("生成验证码动态图片失败", e);
		}
	}

效果:114015_zVz8_2507499.gif

静态图:

/**
	 * @Title:获取验证码图片(jpg版本)
	 * @Description:返回图片流到response中
	 * @author 张颖辉
	 * @date 2017年1月4日上午10:10:30
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "getJpgVerifyCode2Stream", method = RequestMethod.GET)
	public void getJpgVerifyCode2Stream(Integer w, Integer h, HttpServletResponse response, HttpServletRequest request) {
		try {
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			response.setContentType("image/jpg");
			// 生成图片
			w = w == null ? 200 : w;
			h = h == null ? 80 : h;
			Captcha captcha = new SpecCaptcha(w, h, 4);
			// 输出
			captcha.out(response.getOutputStream());
			// 存入Session
			HttpSession session = request.getSession(true);
			session.setAttribute("pic_verifyCode", captcha.text().toLowerCase());
		} catch (Exception e) {
			logger.error("生成验证码图片(白底)失败", e);
		}
	}

效果:04113734_Kzp8.png

参考:http://www.sojson.com/blog/71.html

转载于:https://my.oschina.net/iyinghui/blog/1601564

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值