1. 程式人生 > 程式設計 >基於binarywang封裝的微信工具包生成二維碼

基於binarywang封裝的微信工具包生成二維碼

一、引包(微信開發工具包)

<dependency>
  <groupId>com.github.binarywang</groupId>
  <artifactId>weixin-java-miniapp</artifactId>
  <version>3.5.0</version>
</dependency>

<dependency>
  <groupId>com.github.binarywang</groupId>
  <artifactId>weixin-java-common</artifactId>
  <version>3.5.0</version>
</dependency>

工具包程式碼詳細連結:https://github.com/binarywang

二、程式碼

這邊是直接返回base64圖片形式。如果有需要其他請自行處理。

  @ApiOperation("生成二維碼")
  @ApiImplicitParams({
      @ApiImplicitParam(name="codeType",value = "型別",dataType = "String",required = true,paramType = "query"),@ApiImplicitParam(name="parameterValue",value = "引數值",paramType = "query")
  })
  @GetMapping(value = "/createQrCode")
  public String createQrCode(@RequestParam("codeType") String codeType,@RequestParam("parameterValue") String parameterValue) throws HttpProcessException,IOException {
	//呼叫工具包的服務
    WxMaService wxMaService = new WxMaServiceImpl();
    WxMaDefaultConfigImpl wxMaDefaultConfigImpl = new WxMaDefaultConfigImpl();
    wxMaDefaultConfigImpl.setAppid(WxConfig.appid);		//小程式appId
    wxMaDefaultConfigImpl.setSecret(WxConfig.secret);	//小程式secret
    wxMaService.setWxMaConfig(wxMaDefaultConfigImpl);

    // 設定小程式二維碼線條顏色為黑色
    WxMaCodeLineColor lineColor = new WxMaCodeLineColor("0","0","0");
    byte[] qrCodeBytes = null;
    try {
	  //其中codeType以及parameterValue為前端頁面所需要接收的引數。
      qrCodeBytes = wxMaService.getQrcodeService().createWxaCodeBytes("pages/index/index?codeType=" + codeType + "&parameterValue=" + parameterValue,30,false,lineColor,false);
    } catch (WxErrorException e) {
      e.printStackTrace();
    }
    String qrCodeStr= Base64.encodeBase64String(qrCodeBytes);
    return qrCodeStr;
  }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。