Home >Web Front-end >JS Tutorial >Use jquery.qrcode.js to generate QR code plug-in

Use jquery.qrcode.js to generate QR code plug-in

高洛峰
高洛峰Original
2017-01-20 14:58:331321browse

QR code applications have penetrated into our lives and work. You only need to "scan" the QR code with your mobile phone to get the corresponding information, which is convenient for us to understand merchants, shop, watch movies, etc. . This article will introduce a jquery-based QR code generation plug-in qrcode. Calling this plug-in on the page can generate the corresponding QR code.

1.qrcode actually uses jQuery to achieve graphics rendering and drawing, supporting canvas (HTML5) and table.

github source code address: https:/ /github.com/jeromeetienne/jquery-qrcode

Parameter description:

render : "canvas",//Set the rendering method

width : 256, / /Set width

height : 256, //Set height

typeNumber : -1, //Calculation mode

correctLevel : QRErrorCorrectLevel.H,//Error correction level

background : "#ffffff", //Background color : "#000000" //Foreground color

2. Usage example:

plug-in Quote:

<script src="../Js/jquery-1.11.3.min.js"></script>
 <script src="../Js/jquery-qrcode-master/jquery.qrcode.min.js"></script>

Simple example 1:

<div id="code"></div>
<script>
 //任意字符串 生成二维码
 //默认使用Canvas画图
 $(&#39;#code&#39;).qrcode(&#39;http://blog.csdn.net/u011127019&#39;);
</script>

Use jquery.qrcode.js to generate QR code plug-inSimple example 2:

<div id="code"></div>
<script>
 //table 模式兼容 IE低版本
 $(&#39;#code&#39;).qrcode({
  render: &#39;table&#39;,
  width: 100,
  height: 100,
  text: &#39;http://blog.csdn.net/u011127019&#39;
 });
</script>

Simple example 3 (Chinese support):


When we tested it, we found that it could not be recognized For the QR code of Chinese content, we learned from various sources 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.

<div id="code"></div>
<script>
 //如果内容中有中文,在生成二维码钱就要把字符串转换成utf-8
 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;
 }
  
 $(&#39;#code&#39;).qrcode({
  text: toUtf8(&#39;我是tianma&#39;),
  width: 150,
  height: 150
 });
  
 //就目前 微信/支付宝等 不识别其他颜色的二维码
 $(&#39;#code&#39;).qrcode({
  text: toUtf8(&#39;我是tianma&#39;),
  width: 150,
  height: 150,
  background: &#39;#f00&#39;,
  foreground: &#39;#0f0&#39;
 });
</script>

Example 4:

//text 属性的值长度不能太长,最大字节数 10208
//text 字符串太长 微信/支付宝等扫一扫无法识别,微博识别内容更多
//微博扫一扫:大约200 字以内,微信扫一扫:大约 160字以内,支付宝扫一扫:大约130字符以内
$(&#39;#code&#39;).qrcode({
 text: toUtf8(&#39;SignalR 是 ASP.NET 团队正在开发的一个 Microsoft .NET Framework 库和 jQuery 插件,可能包括在以后版本的 ASP.NET 平台中。 它提供了一些前景极为光明的功能,而这些功能正是,并且是越来越多的,当前不曾具有的,&#39;),
 width: 150,
 height: 150
});

I hope this article will be helpful to you, use jquery.qrcode. This concludes the introduction to the content of the js-generated QR code plug-in. I hope you will continue to pay attention to our website!

For more articles related to using jquery.qrcode.js to generate QR code plug-ins, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn