1. 程式人生 > >微信生成二維碼認證網頁

微信生成二維碼認證網頁

一.引入相關jar

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>2.1</version>
</dependency>
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>2.1</version>
</dependency>

二.編寫相關介面

	/**
	   * 生成二維碼
	   * @param request
	   * @param content 二維碼的內容
	   * @return
	  * @throws UnsupportedEncodingException 
	   */
	  @RequestMapping("/barCodeUrl")
	  public void barCodeUrl(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		 String preSupId = request.getParameter("preSupId");
		 String userCode = request.getParameter("userCode"); 
		 int width = 300;
	     int height = 300;
	     Hashtable<EncodeHintType, Object> hints= new Hashtable<EncodeHintType, Object>();
	     hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
	     BitMatrix bitMatrix;
	    
	     String wxUrl=smpGroupConfig.getWxUrl();//業務域名
	     String url = URLEncoder.encode(wxUrl+"/smpGroup/supplier/focusOnWeixin.to?preSupId="+preSupId+"&userCode="+userCode,"utf-8");//訪問URL
	     String appid=smpGroupConfig.getCorpId();//服務號appid
	     String content ="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri="+url+"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";  
	     InputStream in = null;
	     OutputStream os = null;
	     try {
    	 	 bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
	    	 BufferedImage barcodeImg = MatrixToImageWriter.toBufferedImage(bitMatrix);
	         try {
	        	  in = this.getImageStream(barcodeImg);
	        	  byte[] data = new byte[2048];
	        	  int count = -1;
	        	  os = response.getOutputStream();
	        	  while((count = in.read(data,0,2048)) != -1)
	        		  os.write(data, 0, count);
	        	  os.flush();
	         } catch (Exception e) {
	        	 e.printStackTrace();
	         }
	     } catch (Exception e) {
	    	 e.printStackTrace();
	     }finally {
	    	 if(in!=null) try {in.close();} catch (Exception e) {}
	    	 if(os!=null) try {os.close();} catch (Exception e) {}
	     }
	  }
	  /**
	   * 從BufferedImage物件中獲取InputStream物件
	   */
	  public InputStream getImageStream(BufferedImage bi) {
	   InputStream is = null;
	   ByteArrayOutputStream bs = new ByteArrayOutputStream(); 
	         ImageOutputStream imOut;  
	         try {
	             imOut = ImageIO.createImageOutputStream(bs);  
	             ImageIO.write(bi, "png",imOut);
	             is= new ByteArrayInputStream(bs.toByteArray());
	         } catch (Exception e) {
	             e.printStackTrace();  
	         }   
	         return is;
	  }
	  
	  /**
	   * 關注微訊號
	   * @param request
	   * @return
	   */
	  @RequestMapping("focusOnWeixin")
	  public ModelAndView focusOnWeixin(HttpServletRequest request,HttpServletResponse response) throws Exception{
		  ModelAndView mav = new ModelAndView("/wechat/smpGroup/focusOnWeixin");
		  String preSupId = request.getParameter("preSupId");
		  String userCode = request.getParameter("userCode");
		  String openId = super.getOpenidByHttp(request, response);
		  System.out.println("---------->>preSupId:"+preSupId);
		  System.out.println("---------->>openId:"+openId);
		  if(StringUtils.isEmpty(openId)){
			  System.out.println("獲取使用者資訊失敗!");
			  mav = new  ModelAndView("/weixin/err");
			  mav.addObject("err", "獲取使用者資訊失敗!");
			  return mav;
		  }
		  String url = smpGroupConfig.getSmpGroupUrl() + "/wechat/Wechat!saveOpenIdToSupplier.action?openId="+openId+"&preSupId="+preSupId+"&userCode="+userCode;
		  String result = super.getAppData(request, url);
		  JSONObject json = JSONObject.fromString(result);
		  mav.addObject("rtn", json.getString("rtn"));
		  return mav;
	  }
	

三.結果

1)掃碼

2)掃描結果