Use jquery. qrcode. js to generate a QR code

Source: Internet
Author: User

Use jquery. qrcode. js to generate a QR code

Functions of QR code

1) scan the mobile device to "take away" and read it (that is, generate a QR code for the URL)

2) information can be transmitted (simply generating a string with a QR code)

 

 

Third-party open source Library

 

Qrcode. js implements the core function library of QR code

Jquery. qrcode. js uses jquery to encapsulate the core function library. It is actually used for drawing (canvas and table are supported ).

 

Usage

1) introduce the jquery Library

2) Introduce qrcode. js

3) Introduce jquery. qrcode. js

4) Write

 

Var str = "http://www.baidu.com"; // var str = "Hello World"; // var str = "adding a function for Chinese"; // var str = toUtf8 (str ); // some functions are used to solve Chinese garbled characters $ (# qrcode ). qrcode ({render: canvas, // sets the rendering mode. The value is canvas or table, and is omitted as canvastext: str. // The only required parameter. The width of the QR code is 200, // width, 256 pxheight: 200, // height, 256 pxbackground: # fff, // the background color of the QR code, and # fffforeground: #0f0 // The foreground color of the QR code, omitted as #000

 

Solve Chinese garbled characters

Jquery. qrcode. js adopts charCodeAt () for encoding and conversion. By default, this method obtains its Unicode encoding. Therefore, Chinese characters are garbled. The solution is to put the following function at the end of qrcode. js.

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;  }


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.