JAVA 如何生成带参数的微信小程序二维码?

生成后展示在html上最好,保存在电脑上也行
我参考网上的方法,把encode后的数据加上“data:image/jpeg;base64,”放到img。但没有成功,请问该如何生成
JAVA代码:

String accessToken = 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", "id=1");
paramJson.put("page", "/pages/index/index");
printWriter.write(paramJson.toString());
// flush输出流的缓冲
printWriter.flush();
//开始获取数据
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
ByteOutputStream byteOutputStream = new ByteOutputStream();
byte[] bytes = new byte[1024];
int len=0;
while ((len=bis.read(bytes))!=-1){
    byteOutputStream.write(bytes,0,len);
}
String encode = Base64.encode(byteOutputStream.toByteArray());

html:

<img src="{{src}}" style="width: 300px">

js:

axios.get('/qrcode/test',{
    params:{
        code: text
    }
}).then(function (res) {
    var data = res.data.data;
    that.src = "data:image/jpeg;base64,"+data;
});
阅读 3.5k
1 个回答

请贴出核心代码

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题