欢迎来到258分享网,纯净的网络源码分享基地!

258资源分享网

全部作品
全部作品
网站源码
微信源码
素材特效
源码插件
视频教程
建站学院
热门搜索: 织梦  农业种植  农业  安全设置  官方
258资源分享 > 建站学院 > 微信开发 > java微信小程序参数二维码生成带背景图加字体(无限生成)

推荐下载

HTML5响应式自适应网咯设计

2020-05-12   浏览:736

高端HTML5响应式企业通用网

2020-05-06   浏览:518

html5响应式外贸网站英文版

2020-05-08   浏览:505

HTML5自适应律师工作室类网

2020-04-04   浏览:501

HTML5影视传媒文化公司类网

2020-05-12   浏览:495

java微信小程序参数二维码生成带背景图加字体(无限生成)

发布时间:2020-12-30  

1需求 :1,因为项目需求 ,生成数以万计的二维码      2 ,每个二维码带不同的参数   3,二维码有固定背景图   4 , 往生成图片上写入 字体和编号(动态 )设计技术 : 1,微信接口token ,nginx 缓存  2, ...

 

 

 


需求 : 
1,因为项目需求 ,生成数以万计的二维码   

   2 ,每个二维码带不同的参数

   3,二维码有固定背景图 
   4 , 往生成图片上写入 字体和编号(动态 )

设计技术 :

 1,微信接口token ,nginx 缓存

  2,二维码 图片定义 写字

maven


com.twelvemonkeys.imageio 
imageio-jpeg 
3.0-rc5 

还有rt.jar 在你java安装文件的bin里面

获取token信息

public void GetUrl() throws ClientProtocolException, IOException { HttpGet httpGet = new HttpGet( "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + Configure.getAppID() + "&secret=" + Configure.getSecret() ); HttpClient httpClient = HttpClients.createDefault(); HttpResponse res = httpClient.execute(httpGet); HttpEntity entity = res.getEntity(); String result = EntityUtils.toString(entity, "UTF-8"); JSONObject jsons = JSONObject.fromObject(result); String expires_in = jsons.getString("expires_in"); //缓存 if(Integer.parseInt(expires_in)==7200){ //ok String access_token = jsons.getString("access_token"); Jedis cache = new Jedis("127.0.0.1", 6379); System.out.println("access_token:"+access_token); Pipeline pipeline = cache.pipelined(); pipeline.set("access_token", access_token); pipeline.expire("access_token", 7200); pipeline.sync(); }else{ System.out.println("出错获取token失败!"); } }

  获取二维码 信息图片

public String GetPostUrl(String access_token,String id) throws Exception { //String result = HttpRequest.sendPost("?access_token="+access_token, path); System.out.println(id); String url ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token="; Map<String, Object> map = new HashMap<String, Object>(); map.put("path", "pages/index/index?###="+id);//你二维码中跳向的地址 map.put("width", "430");//图片大小 JSONObject json = JSONObject.fromObject(map); System.out.println(json); String res= HttpClientConnectionManager.httpPostWithJSON(url + access_token, json.toString(),id); System.out.println(res); return null; }

返回图片保存 ,根据 id

public static String httpPostWithJSON(String url, String json,String id) throws Exception { String result = null; DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json"); StringEntity se = new StringEntity(json); se.setContentType("application/json"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "UTF-8")); httpPost.setEntity(se); // httpClient.execute(httpPost); HttpResponse response = httpClient.execute(httpPost); if (response != null) { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { InputStream instreams = resEntity.getContent(); String uploadSysUrl = "D://erweima/"; File saveFile = new File(uploadSysUrl+id+".png"); // 判断这个文件(saveFile)是否存在 if (!saveFile.getParentFile().exists()) { // 如果不存在就创建这个文件夹 saveFile.getParentFile().mkdirs(); } saveToImgByInputStream(instreams, uploadSysUrl, id+".png"); } } httpPost.abort(); return result; } /* @param instreams 二进制流 * @param imgPath 图片的保存路径 * @param imgName 图片的名称 * @return * 1:保存正常 * 0:保存失败 */ public static int saveToImgByInputStream(InputStream instreams,String imgPath,String imgName){ int stateInt = 1; if(instreams != null){ try { File file=new File(imgPath+imgName);//可以是任何图片格式.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) { stateInt = 0; e.printStackTrace(); } finally { try { instreams.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return stateInt; }

然后是图片重合和增加字体

本文标签

:备案管家服务帮您把复杂流程变简单,解决您在进行网站备案过程中遇到的效率低下和成功率不高的问题。个人300/2个域名,企业500/5个域名,qq1650004.