小程序生成指定页面二维码
小程序生成海报分享传播,需要生成分享页面的二维码,用户扫描这个二维码即可进入分享的这个小程序页面,对于分享者更具有指向性;也可生成“太阳码”参阅太阳码生成
Java后台代码
二维码获取详细接口信息可参照官方文档
接口调用凭证(access_token)可参考官方文档
/*** pagesPath 小程序分享页面路径及页面数据请求参数* pagesPath :"pages/index/showGroup/showGroup?tId=tId",*/public String getInfoGroupOfQRCode(String pagesPath) {// 生成二维码下载到服务器的路径String QRCodePath = "";// 接口调用凭证(access_token)String tToken = "XXX";try {JSONObject json = new JSONObject();String requstPath = "https://api./cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN";String url = requstPath.replace("ACCESS_TOKEN", tToken.gettAccessToken());json.accumulate("path", pagesPath);json.accumulate("width", "180");// 请求微信,获取回执数据HttpResponse response = doPostOfQRCode(url, json.toString());if (response != null) {HttpEntity resEntity = response.getEntity();if (resEntity != null) {InputStream instreams = resEntity.getContent();QRCodePath = saveToImgByInputStream(instreams);}}} catch (IOException e) {e.printStackTrace();}return QRCodePath;}
请求微信
post请求微信获取二维码回执信息
public static HttpResponse doPostOfQRCode(String url, String outStr) throws IOException {DefaultHttpClient client = new DefaultHttpClient();//获取DefaultHttpClient请求HttpPost httpost = new HttpPost(url);//HttpPost将使用Get方式发送请求URLJSONObject jsonObject = null;httpost.setEntity(new StringEntity(outStr, "UTF-8"));//使用setEntity方法,将我们传进来的参数放入请求中HttpResponse response = client.execute(httpost);//使用HttpResponse接收client执行httpost的结果return response;}
微信回执生成图片二维码
根据微信回执的信息生成二维码并保存到服务器
public static String saveToImgByInputStream(InputStream instreams) {JSONObject result = new JSONObject();String filePath = "C:\\uploadfiles";// 文件保存目录String fileName = UUIDUtil.getUUID() + ".jpg";if (instreams != null) {try {File file = new File(filePath, fileName);//可以是任何图片格式.jpg,.png等FileOutputStream fos = new FileOutputStream(file);byte[] b = new byte[1024];int nRead = 0;while ((nRead = instreams.read(b)) != -1) {fos.write(b, 0, nRead);}fos.flush();fos.close();} catch (Exception e) {e.printStackTrace();} finally {}}result.put("qRCodeFilePath", "https://xxx/file/download?fileName="+fileName);return result.toJSONString();}