Home >Web Front-end >JS Tutorial >Use jquery component qrcode to generate QR code and application guide_jquery
There are some CPU-consuming calculations that can be calculated on the client, such as generating QR codes.
qrcode actually calculates and then uses jquery to achieve graphics rendering and drawing. Supports canvas and table methods to generate the QR code we need.
Specific usage
qrcode is a jquery component and requires at least two js, namely jquery and jquery.qrcode. You can go to https://github.com/jeromeetienne/jquery-qrcode to get the latest code.
On the page, add an empty element (use div here) where the QR code needs to be displayed
When you need to generate a QR code, call the statement to generate it directly.
Detailed parameters
Parameter Default Value Description
render canvas rendering method, supports canvas and table
width None Width
height None Height
text None URL needs to be generated
Such as:
How to solve the problem of Chinese characters in the URL
During the experiment, we found that the QR code with Chinese content could not be recognized. By searching various information, we learned that jquery-qrcode uses the charCodeAt() method for encoding conversion. This method will obtain its Unicode encoding by default. If there is Chinese content, the string must be converted to UTF-8 before generating the QR code, and then the QR code will be generated. You can convert Chinese strings through the following functions:
下载二维码
用前端画出来的二维码,不是一个canvas就是一个table,如何下载呢?我们只需要将canvas的内容画到image上,下载下来即可。
在angular中的简单封装
在angular中使用,可以封装成directive。不过要确保已经引入了之前的两个js文件。
The download method can be encapsulated into a service in Angular.
Friends, you can use qrcode to generate QR codes. It’s really easy to use. I hope you like it.