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

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


一 为什么会有这样的要求

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


二  微信带参二维码的分类

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

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

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


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

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


1  创建二维码ticket

  1. public static WeixinQRCode createTemporaryQRCode(String token,int expireSeconds,int sceneId){  
  2.           
  3.           
  4.         String requestUrl=”https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN”;  
  5.         requestUrl=requestUrl.replace(”TOKEN”, token);  
  6.         String jsonMsg=”{\”expire_seconds\”: %d, \”action_name\”: \”QR_SCENE\”, \”action_info\”: {\”scene\”: {\”scene_id\”: %d}}}”;  
  7.         JSONObject json=CommonUtil.httpsRequest(requestUrl, ”POST”, String.format(jsonMsg, expireSeconds,sceneId));  
  8.           
  9.         WeixinQRCode w=null;  
  10.         if(json!=null){  
  11.             try{  
  12.                 w=new WeixinQRCode();  
  13.                 w.setExpire_seconds(json.getInt(”expire_seconds”));  
  14.                 w.setTicket(json.getString(”ticket”));  
  15.                 w.setUrl(json.getString(”url”));  
  16.             }catch(Exception e){  
  17.                 //e.printStackTrace();  
  18.                 int errorCode=json.getInt(“errorcode”);  
  19.                 String errorMsg=json.getString(”errmsg”);  
  20.                 System.out.println(errorCode+”:”+errorMsg);  
  21.             }  
  22.               
  23.         }  
  24.         return w;  
  25.           
  26. }  
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;

}
上面的代码是用于临时二维码,下面在贴上永久二维码的代码:

  1. public static WeixinQRCode createPermanentQRCode(String token,int sceneId){  
  2.           
  3.           
  4.         String requestUrl=”https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN”;  
  5.         requestUrl=requestUrl.replace(”TOKEN”, token);  
  6.         String jsonMsg=”{\”action_name\”: \”QR_LIMIT_SCENE\”, \”action_info\”: {\”scene\”: {\”scene_id\”: %d}}}”;  
  7.         JSONObject json=CommonUtil.httpsRequest(requestUrl, ”POST”, String.format(jsonMsg, sceneId));  
  8.           
  9.         WeixinQRCode w=null;  
  10.         if(json!=null){  
  11.             try{  
  12.                 w=new WeixinQRCode();  
  13.                 //w.setExpire_seconds(json.getInt(“expire_seconds”));  
  14.                 w.setTicket(json.getString(”ticket”));  
  15.                 w.setUrl(json.getString(”url”));  
  16.             }catch(Exception e){  
  17.                 //e.printStackTrace();  
  18.                 int errorCode=json.getInt(“errorcode”);  
  19.                 String errorMsg=json.getString(”errmsg”);  
  20.                 System.out.println(errorCode+”:”+errorMsg);  
  21.             }  
  22.               
  23.         }  
  24.         return w;  
  25.           
  26. }  
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换取二维码

  1. public static void  getImageQRCode(String ticket,String savePath) throws Exception{  
  2.         String requestUrl=”https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET”;  
  3.         requestUrl=requestUrl.replace(”TICKET”, URLEncoder.encode(ticket, “UTF-8”));  
  4.         try{  
  5.             URL url = new URL(requestUrl);    
  6.             HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();    
  7.              
  8.     
  9.             httpUrlConn.setDoOutput(true);    
  10.             httpUrlConn.setDoInput(true);    
  11.             httpUrlConn.setUseCaches(false);    
  12.             // 设置请求方式(GET/POST)    
  13.             httpUrlConn.setRequestMethod(”GET”);    
  14.             httpUrlConn.connect();    
  15.          
  16.             // 将返回的输入流转换成字符串    
  17.             InputStream inputStream = httpUrlConn.getInputStream();    
  18.             BufferedInputStream bis=new BufferedInputStream(inputStream);  
  19.             FileOutputStream bos=new FileOutputStream(new File(savePath));  
  20.             int temp=0;  
  21.             while ((temp=bis.read())!=-1) {    
  22.                 bos.write(temp);  
  23.             }    
  24.             bos.close();    
  25.             bis.close();    
  26.             // 释放资源    
  27.             inputStream.close();    
  28.             inputStream = null;    
  29.             httpUrlConn.disconnect();    
  30.     }catch(Exception e){  
  31.        e.printStackTrace();  
  32.     }  
  33. }  
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,直接上代码:

  1. package com.debug.weixin.pojo;  
  2.   
  3. public class WeixinQRCode {  
  4.     private String ticket;  
  5.     private int expire_seconds;  
  6.     private String url;  
  7.   
  8.     public String getTicket() {  
  9.         return ticket;  
  10.     }  
  11.   
  12.     public void setTicket(String ticket) {  
  13.         this.ticket = ticket;  
  14.     }  
  15.   
  16.     public int getExpire_seconds() {  
  17.         return expire_seconds;  
  18.     }  
  19.   
  20.     public void setExpire_seconds(int expireSeconds) {  
  21.         expire_seconds = expireSeconds;  
  22.     }  
  23.   
  24.     public String getUrl() {  
  25.         return url;  
  26.     }  
  27.   
  28.     public void setUrl(String url) {  
  29.         this.url = url;  
  30.     }  
  31. }  
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;
    }
}

下面是运行效果图:



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


一 为什么会有这样的要求

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


二  微信带参二维码的分类

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

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

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


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

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


1  创建二维码ticket

  1. public static WeixinQRCode createTemporaryQRCode(String token,int expireSeconds,int sceneId){  
  2.           
  3.           
  4.         String requestUrl=”https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN”;  
  5.         requestUrl=requestUrl.replace(”TOKEN”, token);  
  6.         String jsonMsg=”{\”expire_seconds\”: %d, \”action_name\”: \”QR_SCENE\”, \”action_info\”: {\”scene\”: {\”scene_id\”: %d}}}”;  
  7.         JSONObject json=CommonUtil.httpsRequest(requestUrl, ”POST”, String.format(jsonMsg, expireSeconds,sceneId));  
  8.           
  9.         WeixinQRCode w=null;  
  10.         if(json!=null){  
  11.             try{  
  12.                 w=new WeixinQRCode();  
  13.                 w.setExpire_seconds(json.getInt(”expire_seconds”));  
  14.                 w.setTicket(json.getString(”ticket”));  
  15.                 w.setUrl(json.getString(”url”));  
  16.             }catch(Exception e){  
  17.                 //e.printStackTrace();  
  18.                 int errorCode=json.getInt(“errorcode”);  
  19.                 String errorMsg=json.getString(”errmsg”);  
  20.                 System.out.println(errorCode+”:”+errorMsg);  
  21.             }  
  22.               
  23.         }  
  24.         return w;  
  25.           
  26. }  
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;

}
上面的代码是用于临时二维码,下面在贴上永久二维码的代码:

  1. public static WeixinQRCode createPermanentQRCode(String token,int sceneId){  
  2.           
  3.           
  4.         String requestUrl=”https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN”;  
  5.         requestUrl=requestUrl.replace(”TOKEN”, token);  
  6.         String jsonMsg=”{\”action_name\”: \”QR_LIMIT_SCENE\”, \”action_info\”: {\”scene\”: {\”scene_id\”: %d}}}”;  
  7.         JSONObject json=CommonUtil.httpsRequest(requestUrl, ”POST”, String.format(jsonMsg, sceneId));  
  8.           
  9.         WeixinQRCode w=null;  
  10.         if(json!=null){  
  11.             try{  
  12.                 w=new WeixinQRCode();  
  13.                 //w.setExpire_seconds(json.getInt(“expire_seconds”));  
  14.                 w.setTicket(json.getString(”ticket”));  
  15.                 w.setUrl(json.getString(”url”));  
  16.             }catch(Exception e){  
  17.                 //e.printStackTrace();  
  18.                 int errorCode=json.getInt(“errorcode”);  
  19.                 String errorMsg=json.getString(”errmsg”);  
  20.                 System.out.println(errorCode+”:”+errorMsg);  
  21.             }  
  22.               
  23.         }  
  24.         return w;  
  25.           
  26. }  
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换取二维码

  1. public static void  getImageQRCode(String ticket,String savePath) throws Exception{  
  2.         String requestUrl=”https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET”;  
  3.         requestUrl=requestUrl.replace(”TICKET”, URLEncoder.encode(ticket, “UTF-8”));  
  4.         try{  
  5.             URL url = new URL(requestUrl);    
  6.             HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();    
  7.              
  8.     
  9.             httpUrlConn.setDoOutput(true);    
  10.             httpUrlConn.setDoInput(true);    
  11.             httpUrlConn.setUseCaches(false);    
  12.             // 设置请求方式(GET/POST)    
  13.             httpUrlConn.setRequestMethod(”GET”);    
  14.             httpUrlConn.connect();    
  15.          
  16.             // 将返回的输入流转换成字符串    
  17.             InputStream inputStream = httpUrlConn.getInputStream();    
  18.             BufferedInputStream bis=new BufferedInputStream(inputStream);  
  19.             FileOutputStream bos=new FileOutputStream(new File(savePath));  
  20.             int temp=0;  
  21.             while ((temp=bis.read())!=-1) {    
  22.                 bos.write(temp);  
  23.             }    
  24.             bos.close();    
  25.             bis.close();    
  26.             // 释放资源    
  27.             inputStream.close();    
  28.             inputStream = null;    
  29.             httpUrlConn.disconnect();    
  30.     }catch(Exception e){  
  31.        e.printStackTrace();  
  32.     }  
  33. }  
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,直接上代码:

  1. package com.debug.weixin.pojo;  
  2.   
  3. public class WeixinQRCode {  
  4.     private String ticket;  
  5.     private int expire_seconds;  
  6.     private String url;  
  7.   
  8.     public String getTicket() {  
  9.         return ticket;  
  10.     }  
  11.   
  12.     public void setTicket(String ticket) {  
  13.         this.ticket = ticket;  
  14.     }  
  15.   
  16.     public int getExpire_seconds() {  
  17.         return expire_seconds;  
  18.     }  
  19.   
  20.     public void setExpire_seconds(int expireSeconds) {  
  21.         expire_seconds = expireSeconds;  
  22.     }  
  23.   
  24.     public String getUrl() {  
  25.         return url;  
  26.     }  
  27.   
  28.     public void setUrl(String url) {  
  29.         this.url = url;  
  30.     }  
  31. }  
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;
    }
}

下面是运行效果图:



猜你喜欢

转载自blog.csdn.net/shuxing520/article/details/79484530
今日推荐