java小程序生成二维码

背景简介

利用微信官方提供的api,获取accessToken,然后根据accessToken获取小程序的二维码或小程序码。

获取acessToken

微信官方提供了获取acessToke的接口.接口地址说明.

  代码:
      public static String getAccessTokenByAppId(String appId) throws Exception {
    LOG.info("getAccessToken, appId="+appId);
    if(StringUtils.isEmpty(appId)){
        throw new Exception("appId is null");
    }
    Map<String,String> parm=new HashMap<>();
    parm.put("appId",appId);
    String getTokenUrl="http://api.znovpc.com/authorization/oauth2/miniprogram/getAccessToken";
    String resultJson= HttpClientUtils.doHttpsGet(getTokenUrl,parm);
    LOG.info("getaccesstoken result:{}",new Object[]{resultJson});
    WxJsonUtil<String> wxJsonUtil= JSONObject.parseObject(resultJson,WxJsonUtil.class);
    if(!wxJsonUtil.isSuccess()){
        throw new Exception("get token error");
    }
    String access_token=wxJsonUtil.getData();
    return access_token;
}

获取二维码流

通过accessToken获取二维码流,微信官方提供了三个二维码的接口,各有不同,可根据需要选择。接口说明

这里选择接口A。

代码:

    public static InputStream getQRCodeStreamByAccessToken(String accessToken, String path, String width) {
    LOG.info("getQRCodeStreamByAccessToken, accessToken=" + accessToken  + ", path="+path);
    if(StringUtils.isEmpty(accessToken)){
        return null;
    }
    if(accessToken==null){
        LOG.info("getQRcode.ep, accessToken==null");
        return null;
    }

    String url = "https://api.weixin.qq.com/wxa/getwxacode?access_token="+accessToken;
    Map<String,Object> param = new HashMap<>();
    param.put("path", path);
    if(StringUtils.isEmpty(width)){
        //默认大小的430
        width= "430";
    }
    param.put("width", Integer.parseInt(width));
    param.put("auto_color", false);
    Map<String,Object> line_color = new HashMap<>();
    line_color.put("r", 0);
    line_color.put("g", 0);
    line_color.put("b", 0);
    param.put("line_color", line_color);
    String params = JSONObject.toJSONString(param);
    LOG.info("调用生成微信URL接口传参:" + params);

    InputStream is=getQRCodeInputStream(url,params);
    return is;
}
 
   public static InputStream getQRCodeInputStream(String strURL, String params) {
    LOG.info("getQRCodeInputStream, strURL="+strURL+", params="+ params);
    try {
        URL url = new URL(strURL);// 创建连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setUseCaches(false);
        connection.setInstanceFollowRedirects(true);
        connection.setRequestMethod("POST"); // 设置请求方式
        connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
        connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
        connection.connect();
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
        out.append(params);
        out.flush();
        out.close();
        InputStream is = connection.getInputStream();
        return is;

    } catch (IOException e) {
        LOG.error("getQRCodeInputStream() failed",e);
    }
    return null;
}

获取二维码图片文件

得到二维码的流数据后,将其转化为图片文件。
代码:

public  File getFileByInputStream(InputStream is, String filePath, String fileName) {
    LOG.info("getFileByInputStream, filePath="+filePath+", fileName="+ fileName);
    File file=null;
    if(is!= null&& !StringUtils.isEmpty(filePath)&&!StringUtils.isEmpty(fileName)){
        try {
            file=new File(filePath,fileName);//可以是任何图片格式.jpg,.png等
            FileOutputStream fos=new FileOutputStream(file);
            byte[] b = new byte[1024];
            int nRead = 0;
            while ((nRead = is.read(b)) != -1) {
                fos.write(b, 0, nRead);
            }
            fos.flush();
            fos.close();
        } catch (IOException e) {
            LOG.error("getFileByInputStream failed",e);
        }
    }
    return file;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值