微信生成小程序二维码

已经更新版本,下面是不保存本地直接将微信返@[TOC](微信生成小程序二维码 更新喽)

样例博客链接 : https://blog.csdn.net/weixin_37545129/article/details/88699623##

已经更新版本,下面是不保存本地直接将微信返回二维码流转换为二进制,上传云时直接将二进制上传,方法自行百度

直接贴代码

 /**
     * 获取小程序二维码
     * @param imgPath 图片路径
     * @param scene   传递的参数(自定义参数)
     * @param config  配置(appId  和 secret   用于获取token)
     * @return
     */

    private String qrCode(String imgPath, String scene, ApiConfig config) throws IOException {
        try {
            //图片路径
            String filePath0 = "/upload/image/" + DateTimeUtil.getDateTime8String();
            //获取token   (根据小程序 appId  和 secret 请求微信获取token )
            String accessToken = AccessTokenApi.getAvailableAccessToken(config).getAccessToken();
            //获取小程序二维码的接口
            URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");// 提交模式
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
            // 发送请求参数
            JSONObject paramJson = new JSONObject();
            paramJson.put("scene", scene);
            paramJson.put("page", "pages/index/index");			//已发布小程序页面,没有则写pages/index/index
            paramJson.put("width", 430);
            paramJson.put("auto_color", true);
            printWriter.write(paramJson.toString());
            System.out.println(imgPath + "=========图片路径=========================" + filePath0);
            // flush输出流的缓冲
            printWriter.flush();
            //图片名称
            String imgName = System.currentTimeMillis() +".png";
            //开始获取数据
            BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
            OutputStream os = new FileOutputStream(new File("D:\\", imgName));
            int len;
            byte[] arr = new byte[1024];
            while ((len = bis.read(arr)) != -1) {
                os.write(arr, 0, len);
                os.flush();
            }
            os.close();
            return imgPath + filePath0 + imgName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 /**
     * 获取小程序二维码(返回二维码二进制) 将二进制上传至七牛云
     *
     * @param scene 传递的参数(自定义参数)
     * @param appId --- secret  配置(appId  和 secret   用于获取token)
     * @param path  小程序跳转路径
     * @return
     */
    private static byte[] qrCode(String scene, String appId, String secret, String path) throws IOException {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        BufferedInputStream inputStream = null;
        byte[] bus = null;
        try {
            //获取token   (根据小程序 appId  和 secret 请求微信获取token )
            String token = TokenUtil.getToken(appId, secret);
            //获取小程序二维码的接口
            URL url = new URL(APP_URL + token);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");// 提交模式
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
            // 发送请求参数
            JSONObject paramJson = new JSONObject();
            paramJson.put("scene", scene);
            path = path == null ? "pages/index/index" : path;
            paramJson.put("page", path);            //已发布小程序页面,没有则写pages/index/index
            paramJson.put("width", 430);
            paramJson.put("auto_color", true);
            printWriter.write(paramJson.toString());
            // flush输出流的缓冲
            printWriter.flush();
            //开始获取数据
            String fileKey = UUID.randomUUID().toString().replace("-" , "");
            inputStream = new BufferedInputStream(httpURLConnection.getInputStream());//拿到二维码流
            byte[] bytes = new byte[1024];
            int b;
            while ((b = inputStream.read(bytes)) != -1) {
                outStream.write(bytes, 0, b);
            }
            bus = outStream.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            outStream.flush();
            outStream.close();
            inputStream.close();
        }
        return bus;//最后会返回二维码的二进制
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值