小程序生成二维码
发布于 5 年前 作者 pengming 16251 次浏览 来自 问答

微信小程序如何生成二维码?有经验的同学们,能分享一下吗?需要贴代码

4 回复

 String URL = “二维码接口地址”;
  String meetingId=request.getParameter(“id”);//我自己的参数,不用管
  log.info("------------------createwxaqrcode"+meetingId);
  if(meetingId!=null&&meetingId.trim().length()>0){
   String access_token;
   InputStream is=null;
   OutputStream os=null;
   try {
    access_token = getAccess_token();//获取token,我自己写的方法,把acess_token获取出来就行
    URL = URL + “?access_token=” + access_token;
    Map<String, Object> map = new HashMap<String, Object>();
    map.put(“path”, “pages/index/index?meetingId=”+meetingId);
    map.put(“width”, 375);
    JSONObject json = new JSONObject(map);
    CloseableHttpClient client = HttpClients.createDefault();
    HttpPost post = new HttpPost(URL);
    post.addHeader(HTTP.CONTENT_TYPE, “application/json”);
    StringEntity se;
    se = new StringEntity(json.toString());
    se.setContentType(“application/json”);
    se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, “UTF-8”));
    post.setEntity(se);
    StringEntity s = new StringEntity(json.toString(), “utf-8”);
    s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
      “application/json”));
    post.setEntity(s);
    HttpResponse res = client.execute(post);
    if(res!=null){
     HttpEntity resEntity = res.getEntity();
     if (resEntity != null) {
      // 获取响应输入流
       is = resEntity.getContent();
       response.setHeader(“Content-Type”,“application/x-msdownload”);
       os=response.getOutputStream();
       //os=new FileOutputStream(“D:\\test.png”);//生成图片
       int count = 0;
       byte[] buffer = new byte[1024 * 8];
       while ((count = is.read(buffer)) != -1) {
        os.write(buffer, 0, count);
        os.flush();
       }
       os.close();
       is.close();
     }
    }

我是直接把流发送到小程序端的,直接用image标签的 src属性读取的

你发送的流数据是请求到的那段乱码(二进制数据)吗还是base64编码过的?

请问小程序怎么从后台获取本程序的页面二维码?

 通过微信小程序二维码接口获取了一大串二进制,编码传到后台后,生成的图片打不开,请问是怎么回事?

回到顶部