微信开发之带参二维码的使用

来源:互联网 发布:佳为软件称软件 编辑:程序博客网 时间:2024/05/21 18:47

就在今年春节期间,有个我不愿提及的人说要做个使用微信扫描二维码的方式实现会议签到的功能,当时还没接触任何的微信开发,也就只说自己不会微信开发。直到今天才直到应该怎么实现。现在很多东西都用php来开发微信,我就用java做了几个项目居然被php的鄙视,二话没说直接把这种好友拉入黑名单。语言本身无优劣之分,没有糟糕的语言只有糟糕的不想学习的懒惰程序员。好了废话不多说进入今天的主题-----------微信带参二维码的使用


一 为什么会有这样的要求

前些年二维码主要是为了扫描之后出现链接,点击之后进行下载各类APP的场合。二维码也就这样流行起来,微信也不甘落后,扫一扫的这种功能就来了。


二  微信带参二维码的分类

微信带参二维码主要分临时二维码、永久二维码两种,下面是微信开发文档的解释:

1、临时二维码,是有过期时间的,最长可以设置为在二维码生成后的7天(即604800秒)后过期,但能够生成较多数量。临时二维码主要用于帐号绑定等不要求二维码永久保存的业务场景2、永久二维码,是无过期时间的,但数量较少(目前为最多10万个)。永久二维码主要用于适用于帐号绑定、用户来源统计等场景。

看过之后对带参二维码应该有了比较全面的认识了,接下来就是重头戏---Java代码


三  使用程序生成并换取二维码

要获取到最终的二维码只需要简单的两步分别是创建二维码ticket、通过ticket换取二维码,接下来就是贴代码了:


1  创建二维码ticket

public static WeixinQRCode createTemporaryQRCode(String token,int expireSeconds,int sceneId){String requestUrl="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN";requestUrl=requestUrl.replace("TOKEN", token);String jsonMsg="{\"expire_seconds\": %d, \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": %d}}}";JSONObject json=CommonUtil.httpsRequest(requestUrl, "POST", String.format(jsonMsg, expireSeconds,sceneId));WeixinQRCode w=null;if(json!=null){try{w=new WeixinQRCode();w.setExpire_seconds(json.getInt("expire_seconds"));w.setTicket(json.getString("ticket"));w.setUrl(json.getString("url"));}catch(Exception e){//e.printStackTrace();int errorCode=json.getInt("errorcode");String errorMsg=json.getString("errmsg");System.out.println(errorCode+":"+errorMsg);}}return w;}
上面的代码是用于临时二维码,下面在贴上永久二维码的代码:

public static WeixinQRCode createPermanentQRCode(String token,int sceneId){String requestUrl="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN";requestUrl=requestUrl.replace("TOKEN", token);String jsonMsg="{\"action_name\": \"QR_LIMIT_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": %d}}}";JSONObject json=CommonUtil.httpsRequest(requestUrl, "POST", String.format(jsonMsg, sceneId));WeixinQRCode w=null;if(json!=null){try{w=new WeixinQRCode();//w.setExpire_seconds(json.getInt("expire_seconds"));w.setTicket(json.getString("ticket"));w.setUrl(json.getString("url"));}catch(Exception e){//e.printStackTrace();int errorCode=json.getInt("errorcode");String errorMsg=json.getString("errmsg");System.out.println(errorCode+":"+errorMsg);}}return w;}

2  通过ticket换取二维码

public static void  getImageQRCode(String ticket,String savePath) throws Exception{String requestUrl="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET";requestUrl=requestUrl.replace("TICKET", URLEncoder.encode(ticket, "UTF-8"));try{    URL url = new URL(requestUrl);              HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();                           httpUrlConn.setDoOutput(true);              httpUrlConn.setDoInput(true);              httpUrlConn.setUseCaches(false);              // 设置请求方式(GET/POST)              httpUrlConn.setRequestMethod("GET");              httpUrlConn.connect();                     // 将返回的输入流转换成字符串              InputStream inputStream = httpUrlConn.getInputStream();              BufferedInputStream bis=new BufferedInputStream(inputStream);            FileOutputStream bos=new FileOutputStream(new File(savePath));            int temp=0;            while ((temp=bis.read())!=-1) {              bos.write(temp);            }              bos.close();              bis.close();              // 释放资源              inputStream.close();              inputStream = null;              httpUrlConn.disconnect();  }catch(Exception e){   e.printStackTrace();}}

下面是WeixinQRCode,直接上代码:

package com.debug.weixin.pojo;public class WeixinQRCode {private String ticket;private int expire_seconds;private String url;public String getTicket() {return ticket;}public void setTicket(String ticket) {this.ticket = ticket;}public int getExpire_seconds() {return expire_seconds;}public void setExpire_seconds(int expireSeconds) {expire_seconds = expireSeconds;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}}

下面是运行效果图:



0 0
原创粉丝点击