Jquery. qrcode. js is a jquery plug-in that can generate a matrix QR code QRCode on the client. It can be used to easily generate a two-dimensional barcode on the page. This plug-in can be used independently and is small in size.
How to Use
1. First, add the jquery library file and qrcode plug-in to the page.
The Code is as follows: |
Copy code |
<Script type = "text/javascript" src = "jquery. js"> </script> <Script type = "text/javascript" src = "jquery. qrcode. min. js"> </script> |
2. Add the following code to the page where you need to display the QR code:
The Code is as follows: |
Copy code |
<Div id = "code"> </div> |
3. Call the qrcode plug-in.
Qrcode supports canvas and table for Image Rendering. By default, the canvas mode is used, with the highest efficiency. Of course, the browser must support html5. The direct call is as follows:
The Code is as follows: |
Copy code |
$ ('# Code'). qrcode ("http://www.bKjia. c0m"); // any string |
You can also call the following method:
The Code is as follows: |
Copy code |
$ ("# Code"). qrcode ({ Render: "table", // table Method Width: 200, // width Height: 200, // height text: "www. bKjia. c0m" // any content }); |
In this way, you can directly generate a QR code on the page. You can use the "scan" function on your mobile phone to read the QR code information.
Recognize Chinese
During the experiment, we found that the two-dimensional code could not recognize Chinese content. We found that jquery-qrcode was converted using charCodeAt. And this method will get its Unicode encoding by default, if there is Chinese content, in the generation of the QR code before the string to the UTF-8, and then generate a QR code. You can use the following functions to convert Chinese strings:
The Code is as follows: |
Copy code |
Function toUtf8 (str ){ Var out, I, len, c; Out = ""; Len = str. length; For (I = 0; I <len; I ++ ){ C = str. charCodeAt (I ); If (c> = 0x0001) & (c <= 0x007F )){ Out + = str. charAt (I ); } Else if (c> 0x07FF ){ Out + = String. fromCharCode (0xE0 | (c> 12) & 0x0F )); Out + = String. fromCharCode (0x80 | (c> 6) & 0x3F )); Out + = String. fromCharCode (0x80 | (c> 0) & 0x3F )); } Else { Out + = String. fromCharCode (0xC0 | (c> 6) & 0x1F )); Out + = String. fromCharCode (0x80 | (c> 0) & 0x3F )); } } Return out; } |
Example:
The Code is as follows: |
Copy code |
Var str = toUtf8 ("aison Homestead "); $ ('# Code'). qrcode (str ); |
English is no problem, if it is Chinese, under normal circumstances Unicode is UTF-16 implementation, length 2 bits, and UTF-8 encoding is 3 bits, so the codec of the QR code does not match.
The solution is of course to convert the string into a UTF-8 before the QR code encoding