java 生成微信小程序二维码

  • 简单粗暴,直接开干,获取 access_token
public String getAccessToken() {
    try {
        StringBuilder url = new StringBuilder("https://api.weixin.qq.com/cgi-bin/token?");
        //固定参数
        url.append("grant_type=").append("client_credential");
        //微信小程序的appId和secret
        url.append("&appid=").append(appId);
        url.append("&secret=").append(appSecret);

        HttpClient client = HttpClientBuilder.create().build();//构建一个Client
        HttpGet get = new HttpGet(url.toString());    //构建一个GET请求
        HttpResponse response = client.execute(get);//提交GET请求
        HttpEntity he = response.getEntity();//拿到返回的HttpResponse的"实体"

        String content = EntityUtils.toString(he);
        JSONObject obj = JSONObject.parseObject(content);
        return obj.getString("access_token");
    } catch (IOException e) {
        e.printStackTrace();
        return "";
    }
}
  • 生成二维码图片,调用此方法
/**
 * 二维码图片生成
 */
public String getQrCode(String codeName, String shopId) {
    InputStream inputStream = null;
    try {
        Map<String, Object> map = new HashMap<>();
        //参数,一个可以直接放入参数例如:1 多个例如:id=1&name=2&...,注意,参数长度不能大于32位
        map.put("scene", shopId);
        map.put("path", "pages/warrant/warrant"); //扫码后进入小程序的页面位置
        map.put("width", 430);//不是必须,需要的宽度,默认430x430,最小280最大1280
        map.put("is_hyaline", true);//是否透明底色
        map.put("auto_color", false);//是否线条变色,auto_color=false 生效
        map.put("line_color", JSONUtil.toBean("{'r':255,'g':0,'b':0}", Object.class));

        String body = JSON.toJSONString(map);//将map集合转换成字符串
        StringEntity entity = new StringEntity(body);
        entity.setContentType("image/jpeg");//设置图片类型

        String token = getAccessToken();//获取access_token

        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + token);
        httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
        httpPost.setEntity(entity);
        HttpResponse httpResponse = httpClient.execute(httpPost);
        inputStream = httpResponse.getEntity().getContent();
        //文件名加后缀,文件后缀要一致
        String name = codeName + ".jpg";
        downLoad(inputStream, name);
        return osspath;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
  • 下载到本地
/**
 * 生成在本地
 * @param name
 * @param input
 * @throws IOException
 */
public static void writeToLocal(String name, InputStream input) throws IOException {
    int index;
    byte[] bytes = new byte[1024];
    FileOutputStream downloadFile = new FileOutputStream("H:\\photo\\" + name);
    while ((index = input.read(bytes)) != -1) {
        downloadFile.write(bytes, 0, index);
        downloadFile.flush();
    }
    input.close();
    downloadFile.close();
}
  • 生成的结果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值