Java 获取小程序二维码的几种方式

小程序二维码的适用场景

  1. 适用于需要的码数量较少的业务场景
  2. 总共生成的码数量限制为100,000,请谨慎调用。

获取步骤

接口地址:

https://api.weixin.qq.com/cgi- bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN>

(1)POST 参数说明

参数类型默认值说明
pathString不能为空,最大长度 128 字节
widthInt430二维码的宽度

注意:通过该接口生成的小程序二维码,永久有效,数量限制见文末说明,请谨慎使用。用户扫描该码进入小程序后,将直接进入 path 对应的页面。

示例:

{“path”: “pages/index?query=1”, “width”: 430}

注:pages/index 需要在 app.json 的 pages 中定义

(2)请求接口测试

è¿éåå¾çæè¿°

    /**
     *  获取小程序二维码
     * @param url 官方获取二维码地址
     * @param width 二维码的宽度 int类型 默认 430
     * @param access_token 
     * @param path
     * @return
     */
      public static InputStream createwxaqrcode(String url,String access_token,String path,String width){
          url = url + "?access_token=" + access_token;
          JSONObject jsonParam = new JSONObject();
          jsonParam.put("path", path);
          jsonParam.put("width", width);
          InputStream instreams = doWXPost(url, jsonParam);
         if(BL3Utils.isEmpty(instreams)){
             System.out.println("出错获取二维码失败!");
         }
         return instreams;
     }
    /**
     *  请求
     * @param url 
     * @param jsonParam
     * @return
     */
     public static InputStream doWXPost(String url, JSONObject jsonParam) {
            InputStream instreams = null;
            HttpPost httpRequst = new HttpPost(url);// 创建HttpPost对象
            try {
                StringEntity se = new StringEntity(jsonParam.toString(),"utf-8");
                se.setContentType("application/json");
                se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"UTF-8"));
                httpRequst.setEntity(se);
                HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequst);
                if (httpResponse.getStatusLine().getStatusCode() == 200) {
                    HttpEntity httpEntity = httpResponse.getEntity();
                    if (httpEntity != null) {
                        instreams = httpEntity.getContent();
                    }
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return instreams;
        }

参数介绍:
1. url : https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode
2. access_token:上面有介绍(getAccessToken这个方法)
3. path:用户扫描该码进入小程序后,将直接进入 path 对应的页面;一般是首页地址”pages/index/index” 也可以带上参数
“pages/index/index?query=1”。
4. width:二维码的宽度 int类型 默认 430。

    /* @param instreams 二进制流
     * @param imgPath 图片的保存路径
     * @param fileName 图片的名称
     * @return str 图片保存地址
     */
    public static String saveToImgByInputStream(InputStream instreams,String imagePath,String fileName){
        String str = "";
        String path = "QRimage" + getWFileDirName();
        String linuxPath = path.replaceAll("//",File.separator);
        if(instreams != null){
            boolean b =uploadImages(instreams,imagePath+File.separator+linuxPath, fileName);
            if(b){
                str =linuxPath+fileName;
            }
        }
        return str;
    }

参数介绍

1. instreams: 上面有介绍(createwxaqrcode这个方法)
2. imagePath:保存图片的地址
3. fileName:图片自定义名称(可以自定义 比如:1.jpg、1.png等)。

    /**
     * IO流保存图片
     * @param instreams
     * @param imagePath
     * @param fileName
     * @return
     */
    public static boolean uploadImages( InputStream instreams,String imagePath,String fileName) {
        File f = new File(imagePath);
        f.setWritable(true, false);
        boolean flag = false;
        try {
            // 1K的数据缓冲
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;
            // 输出的文件流
            File file = new File(imagePath,fileName);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
                try {
                    // 创建新文件
                    file.createNewFile();
                } catch (IOException e) {
                    System.out.println("创建新文件时出现了错误。。。");
                    e.printStackTrace();
                }
            }
            OutputStream os = new FileOutputStream(imagePath+File.separator+fileName);
            // 开始读取
            while ((len = instreams.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
            // 完毕,关闭所有链接
            os.close();
            instreams.close();
            flag = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }

直接使用下面的代码就可以获取到二维码地址了

String qrcodeUrl = saveToImgByInputStream(instreams,imagePath,fileName);

接口说明

1:通过该接口,仅能生成已发布的小程序的二维码。
2:可以在开发者工具预览时生成开发版的带参二维码。
3:接口1加上接口2,总共生成的码数量限制为100,000,请谨慎调用。
4 : POST 参数需要转成 json 字符串,不支持 form 表单提交。
5 : auto_color line_color 参数仅对小程序码生效。

最后请大家注意

微信小程序码 和 微信小程序二维码 是有区别的

微信小程序码是圆的,微信小程序二维码是方的

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员徐师兄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值