zoukankan      html  css  js  c++  java
  • 数字图片验证码java后台版本

    public BufferedImage createVerifyCode(MiaoshaUser user, long goodsId) {
    		if(user == null || goodsId <=0) {
    			return null;
    		}
    		int width = 80;
    		int height = 32;
    		//create the image
    		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    		Graphics g = image.getGraphics();
    		// set the background color
    		g.setColor(new Color(0xDCDCDC));
    		g.fillRect(0, 0, width, height);
    		// draw the border
    		g.setColor(Color.black);
    		g.drawRect(0, 0, width - 1, height - 1);
    		// create a random instance to generate the codes
    		Random rdm = new Random();
    		// make some confusion
    		for (int i = 0; i < 50; i++) {
    			int x = rdm.nextInt(width);
    			int y = rdm.nextInt(height);
    			g.drawOval(x, y, 0, 0);
    		}
    		// generate a random code
    		String verifyCode = generateVerifyCode(rdm);
    		g.setColor(new Color(0, 100, 0));
    		g.setFont(new Font("Candara", Font.BOLD, 24));
    		g.drawString(verifyCode, 8, 24);
    		g.dispose();
    		//把验证码存到redis中
    		int rnd = calc(verifyCode);
    		redisService.set(MiaoshaKey.getMiaoshaVerifyCode, user.getId()+","+goodsId, rnd);
    		//输出图片	
    		return image;
    	}
    
     
      @RequestMapping(value="/verifyCode", method=RequestMethod.GET)
        @ResponseBody
        public Result<String> getMiaoshaVerifyCod(HttpServletResponse response,MiaoshaUser user,
        		@RequestParam("goodsId")long goodsId) {
        	if(user == null) {
        		return Result.error(CodeMsg.SESSION_ERROR);
        	}
        	try {
        		BufferedImage image  = miaoshaService.createVerifyCode(user, goodsId);
        		OutputStream out = response.getOutputStream();
        		ImageIO.write(image, "JPEG", out);
        		out.flush();
        		out.close();
        		return null;
        	}catch(Exception e) {
        		e.printStackTrace();
        		return Result.error(CodeMsg.MIAOSHA_FAIL);
        	}
        }
    
    
     
    
    
  • 相关阅读:
    41 最大子数组
    4 丑数 Ⅱ-找出第n个丑数
    写在编程初始
    lightoj 1068
    2018-11-8-内置函数(2)
    2018-11-7-内置函数(1)
    2018.11.06 生成器函数进阶&列表推导式&生成器表达式
    python2&python3的区别
    第一次打开Pycharm如何操作?
    关于做题的一些反思
  • 原文地址:https://www.cnblogs.com/chengxiaolong/p/10206332.html
Copyright © 2011-2022 走看看