At present, small programs have introduced their own identification code, small program code, circular code looks better than two-dimensional code.
This paper summarizes the interface development of the acquisition of two-dimensional code of the widget. Official address
The main content is extracted from the API document of the Wechat applet. The development of java interface is summarized and developed by oneself.
I. Brief Introduction
The two-dimensional code of any page of the widget can be acquired through the background interface. Scanning the two-dimensional code can directly enter the corresponding page of the widget. At present, Wechat supports two kinds of two-dimensional codes, small program code (left), small program two-dimensional code (right), as follows:
Three Ways of Obtaining Two-Dimensional Code by Wechat Widget Program
Two Kinds of Small Program Code One Kind of Small Program Two-Dimensional Code
1. Official documents have introduced how to obtain two-dimensional codes. Here we mainly introduce how to obtain JAVA.
2. Two-Dimensional Code API Documentation of Wechat Widget Program Official address
3. There are differences in the image styles obtained by small programs. One is the familiar two-dimensional code, the other is the small program code. Specifically, it can be selected according to the needs.
The premise of acquiring small program codes is
The TOKEN of the applet must be obtained
Get access_token See documentation for details.
/** * Get token * @param url * @param grantType * @param appid * @param secret * @return */ public static String getAccessToken(String url,String grantType,String appid,String secret){ String access_token = ""; String tokenUrl = url+"?grant_type="+ grantType+"&appid="+ appid + "&secret="+ secret; Object result = HttpUtils.doGet(tokenUrl); JSONObject jsons = JSONObject.parseObject(result.toString()); String expires_in = jsons.getString("expires_in"); if(BL3Utils.isNotEmpty(expires_in)&&Integer.parseInt(expires_in)==7200){ //ok access_token = jsons.getString("access_token"); }else{ System.out.println("Error acquisition token Failure!"); } return access_token; }
Introduction of parameters:
1.url : https://api.weixin.qq.com/cgi-bin/token
2.grantType: client_credential
3.appid: appid applet, which is automatically generated when the public platform of Wechat registers the applet.
4.secret: the widget secret, which is automatically generated when the widget is registered on the Wechat Public Platform.
5. The URL and grantType parameter official websites are actually introduced.
Two Ways to Get Small Program Codes
At present, there are two interfaces that can generate small code. Developers can choose the appropriate interface according to their needs.
First
- Suitable for business scenarios with fewer codes required
- Yeah, a small program code interface without parameters
- The total number of generated codes is limited to 100,000. Please call carefully.
Interface address:
https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN
(1) Description of POST parameters
parameter | type | Default values | Explain |
---|---|---|---|
path | String | Can't be empty, maximum length 128 bytes | |
width | Int | 430 | Width of Two-Dimensional Code |
auto_color | Bool | false | Automatically configure line color. If the color is still black, it is not recommended to configure the main tone. |
line_color | Object | {"r":"0","g":"0","b":"0"} | When auth_color is false, use R G B to set colors such as {"r":"xxx", "g":"xxx", "b":"xxxx"} |
Note: The small code generated through this interface is permanent, but the number is valid. Please use it carefully. After the user scans the code and enters the applet, he will go directly to the page corresponding to the path.
(2) Request interface testing
Use the http request plug-in postman or RESTClient to request tests.
Request test results to return a picture of a small program code. Unlike the two-dimensional code generated by the Wechat public platform, the small program code directly returns to the file stream, not the url and ticket of the Wechat public platform.
(3) Development of java interface
/** 1. Small Program Code Interface with Finite Number of Parameters 2. @param url 3. @param access_token 4. @param path 5. @param width 6. @return */ public static InputStream getwxacode(String url,String access_token,String path,String width){ url = url + "?access_token=" + access_token; JSONObject jsonParam = new JSONObject(); jsonParam.put("path", path); jsonParam.put("width", Integer.parseInt(width)); jsonParam.put("auto_color", false); Map<String,Object> line_color = new HashMap<>(); line_color.put("r", 0); line_color.put("g", 0); line_color.put("b", 0); jsonParam.put("line_color", line_color); InputStream instreams = HttpUtils.doWXPost(url, jsonParam); if(BL3Utils.isEmpty(instreams)){ System.out.println("Error acquiring two-dimensional code failed!"); } return instreams; }
Description of parameters
1.url : https://api.weixin.qq.com/wxa/getwxacode
2.access_token: The above description (getAccessToken method)
3.path: After the user scans the code and enters the applet, he will go directly to the page corresponding to the path. Generally, the home page address: "pages/index/index" can also take parameters: "pages/index/index?query=1".
4.width: The default width int type for two-dimensional codes is 430
Second
- Suitable for business scenarios with a large number of required codes
- There is no quantitative limit.
- With parameters
Interface address:
https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN
(1) Description of POST parameters
parameter | type | Default values | Explain |
---|---|---|---|
scene | String | Maximum 32 visible characters, only supporting numbers, case English and some special characters: __________________________ | |
page | String | Must be a published applet page, such as "pages/index/index", if you do not fill in this field, the default jump home page | |
width | Int | 430 | Width of Two-Dimensional Code |
auto_color | Bool | false | Automatically configure line color. If the color is still black, it is not recommended to configure the main tone. |
line_color | Object | {"r":"0","g":"0","b":"0"} | When auto_color is false, use R G B to set colors such as {r":" xxx","g":" xxx","b":" xxxx"} |
Note: The small code generated through this interface is permanent and unlimited in number. After the user scans the code and enters the widget, the developer needs to do the processing logic after the value of scene field in the code obtained from the corresponding page. The values of scene fields in the two-dimensional code can be obtained by using the following code. In debugging phase, the user-defined parameter scene=xxxx can be compiled using the conditions of the development tool to simulate. The parameter values of scene in the simulation of the development tool need urlencode. At the same time, it should be noted that the page parameters of this interface can not take any parameters, parameters are processed in scene parameters, remember!!!
// This is the js on the home page.
Page({
onLoad: function(options) {
// Scene in options requires decodeURIComponent to obtain the scene that is passed in when generating two-dimensional code
var scene = decodeURIComponent(options.scene)
}
})
(2) Request interface testing
(3) java interface development
/** * Infinite Number Small Program Code Interface with Parameters * @param url * @param access_token * @param path * @param width * @return */ public static InputStream getwxacodeunlimit(String url,String access_token,String path,String width){ String[] str = path.split("[?]"); path = str[0]; String scene = str[1]; url = url + "?access_token=" + access_token; // Receiving parameter json list JSONObject jsonParam = new JSONObject(); jsonParam.put("scene", scene); jsonParam.put("page", path); jsonParam.put("width", Integer.parseInt(width)); jsonParam.put("auto_color", false); Map<String,Object> line_color = new HashMap<>(); line_color.put("r", 0); line_color.put("g", 0); line_color.put("b", 0); jsonParam.put("line_color", line_color); InputStream instreams = HttpUtils.doWXPost(url, jsonParam); if(BL3Utils.isEmpty(instreams)){ System.out.println("Error acquiring two-dimensional code failed!"); } return instreams; }
Description of parameters
1.url : https://api.weixin.qq.com/wxa/getwxacodeunlimit
2.access_token: The above description (getAccessToken method)
3.path: After the user scans the code and enters the applet, he will go directly to the page corresponding to the path; generally, the home page address "pages/index/index" can also take parameters: "pages/index/index?query=1".
4.width: The default width int type for two-dimensional codes is 430
Be careful:
- The second way to generate small program code is to generate two-dimensional code only when the small program is online.
- The code generated by other methods will have access only after the widget is online, otherwise the widget will be prompted that the widget has not been released.
Obtaining Two-Dimensional Codes for Small Programs
- Suitable for business scenarios with fewer codes required
- The total number of generated codes is limited to 100,000. Please call carefully.
Interface address:
https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN
(1) POST parameter description
parameter | type | Default values | Explain |
---|---|---|---|
path | String | Can't be empty, maximum length 128 bytes | |
width | Int | 430 | Width of Two-Dimensional Code |
Note: The small program 2-D code generated by this interface is permanently valid. The quantity limit is explained at the end of this paper. Please use it carefully. After the user scans the code and enters the applet, he will go directly to the page corresponding to the path.
Examples:
{"path": "pages/index?query=1", "width": 430}
Note: pages/index need to be defined in app.json pages
(2) Request interface testing
/** * Obtaining Two-Dimensional Codes for Small Programs * @param url Official Access to Two-Dimensional Code Address * @param width Default width int type 430 for 2-D codes * @param access_token * @param path * @return */ public static InputStream createwxaqrcode(String url,String access_token,String path,String width){ url = url + "?access_token=" + access_token; JSONObject jsonParam = new JSONObject(); jsonParam.put("path", path); jsonParam.put("width", width); InputStream instreams = doWXPost(url, jsonParam); if(BL3Utils.isEmpty(instreams)){ System.out.println("Error acquiring two-dimensional code failed!"); } return instreams; } /** * request * @param url * @param jsonParam * @return */ public static InputStream doWXPost(String url, JSONObject jsonParam) { InputStream instreams = null; HttpPost httpRequst = new HttpPost(url);// Create the HttpPost object try { StringEntity se = new StringEntity(jsonParam.toString(),"utf-8"); se.setContentType("application/json"); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"UTF-8")); httpRequst.setEntity(se); HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequst); if (httpResponse.getStatusLine().getStatusCode() == 200) { HttpEntity httpEntity = httpResponse.getEntity(); if (httpEntity != null) { instreams = httpEntity.getContent(); } } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return instreams; }
Introduction of parameters:
1. url : https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode
2. access_token: This method is described above (getAccessToken)
3. path: After the user scans the code and enters the applet, he will go directly to the page corresponding to the path; generally, the home page address "pages/index/index" can also take the parameter "pages/index/index?query=1".
4. width: The width int type of the two-dimensional code defaults to 430.
/* @param instreams Binary stream * @param imgPath Storage Path of Pictures * @param fileName Name of picture * @return str Picture Storage Address */ public static String saveToImgByInputStream(InputStream instreams,String imagePath,String fileName){ String str = ""; String path = "QRimage" + getWFileDirName(); String linuxPath = path.replaceAll("//",File.separator); if(instreams != null){ boolean b =uploadImages(instreams,imagePath+File.separator+linuxPath, fileName); if(b){ str =linuxPath+fileName; } } return str; }
Introduction of parameters
1. instreams: described above (createwxaqrcode method)
2. imagePath: Address to save the picture
3. fileName: Image custom name (can be customized such as: 1.jpg, 1.png, etc.).
/** * IO Stream Save Pictures * @param instreams * @param imagePath * @param fileName * @return */ public static boolean uploadImages( InputStream instreams,String imagePath,String fileName) { File f = new File(imagePath); f.setWritable(true, false); boolean flag = false; try { // 1K Data Buffer byte[] bs = new byte[1024]; // Length of data read int len; // Output file stream File file = new File(imagePath,fileName); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); try { // create a new file file.createNewFile(); } catch (IOException e) { System.out.println("An error occurred while creating a new file..."); e.printStackTrace(); } } OutputStream os = new FileOutputStream(imagePath+File.separator+fileName); // Start reading while ((len = instreams.read(bs)) != -1) { os.write(bs, 0, len); } // Close all links. os.close(); instreams.close(); flag = true; } catch (IOException e) { e.printStackTrace(); } return flag; }
The two-dimensional code address can be obtained directly by using the following code
String qrcodeUrl = saveToImgByInputStream(instreams,imagePath,fileName);
III. Explanation
1: Through this interface, only two-dimensional codes of published small programs can be generated.
2: The parametric two-dimensional code of the development version can be generated when the developer tool previews.
3: Interface 1 plus interface 2, the total number of generated codes is limited to 100,000, please call carefully.
4: POST parameters need to be converted to json strings and form submission is not supported.
5: The auto_color line_color parameter is only valid for small code.
Finally, please pay attention to it.
This blog is about
generate
Two Ways of Wechat Small Program Code
And generation
A Way of Two-Dimensional Code for Wechat Small Program
There is a difference between Wechat widget code and Wechat widget two-dimensional code.
Wechat widget code is round. Wechat widget two-dimensional code is square.