微信小程序获取二维码,踩过的坑记录一下

微信小程序获取二维码,踩过的坑记录一下

首先申请微信小程序的测试账号测试账号(这个链接藏的比较深)

获取二维码的api文档地址:二维码文档

我这里使用的是方式B,需要传scene这里需要先获取AccessToken,构建参数:

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("scene", inviter.getInviterCode());
        jsonObject.put("page", "");
        jsonObject.put("width", 600);
        jsonObject.put("auto_color", true);
        JSONObject colorJson = (JSONObject) JSON.parse("{\"r\":\"0\",\"g\":\"0\",\"b\":\"0\"}");
        jsonObject.put("line_color", colorJson);
        jsonObject.put("is_hyaline", false);

注意这里的line_color一定得传一个JSON对象,并且里面的rgb三个参数不能少,不然请求返回报错

{"errcode":47001,"errmsg":"data format error hint: [h4fcVA0113b451]"}

接下来就是发送请求,如果请求正常返回的是一张图片流,这里不能直接使用String 接受,否则得到的是乱码,我这里使用的是InputStream接收

public static InputStream postJson(String url, String json) {
        InputStream is = null;
        CloseableHttpClient httpClient = null;
        try {
            httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            StringEntity requestEntity = new StringEntity(json, "utf-8");
            requestEntity.setContentEncoding("UTF-8");
            httpPost.setHeader("Content-type", "application/json");
            httpPost.setEntity(requestEntity);
            HttpResponse response;
            response = httpClient.execute(httpPost);
            int code = response.getStatusLine().getStatusCode();
            if (HttpStatus.OK.value() == code) {
                is = response.getEntity().getContent();
            }
        } catch (Exception e) {
            log.error("", e);
        } finally {
            /*if(httpClient != null) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    log.error("", e);
                }
            }*/
        }
        return is;
    }

这里不要使用spring 的 restTemplate封装,不然拿不到InputStream,或者我这里用的方法不对。

接下来我直接把流返回到前端展示就OK了

private void encodeQrcodeInputStream(InputStream inputStream, HttpServletResponse response) {
        if (inputStream == null) {
            return;
        }

        try {
            OutputStream outputStream = response.getOutputStream();
            //输出二维码图片流
            try {
                IOUtils.copy(inputStream, outputStream);
            } catch (IOException e) {
                logger.error("", e);
            }
        } catch (Exception e1) {
            logger.error("", e1);
        }
    }

这里也可以保存到本地

File file = new File("/Users/apple/test1.png");
        FileOutputStream outputStream = new FileOutputStream(file);
        IOUtils.copy(inputStream, outputStream);
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(outputStream);

这里IOUtils使用的是org.apache.commons.io.IOUtils,很好用的一个工具

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值