jquery plug-in qrcode generates QR code online_jquery
With the development of mobile Internet, QR codes are now more and more widely used. By scanning them, you can browse websites, add friends, etc. It is really much more convenient than entering them manually.
We made a comprehensive evaluation system in the early stage, considering the gradual implementation of mobile. It is inconvenient for users to enter a long list of IP addresses. With the help of QR codes, users can directly enter the system by picking up their mobile phones and scanning.
Based on this application scenario, I went online to research how to implement website QR codes. In summary, there are two types:
1. Use some QR code generating websites or QR code generators to generate QR code images, and then hang them on the website, such as Code Cloud QR-Code (QR code) online generator
Advantages: Development cost is zero, diversified QR codes can be quickly realized;
Disadvantages: Changing the QR code and maintaining it is a bit troublesome
2. Use java or .net code on the backend to generate QR code images, and then reference the images on the website, such as qrcode, zxing, etc.
Advantages: Highly customizable, fast batch generation
Disadvantages: heavyweight implementation, higher development cost for simple applications
3. Instantly generate a QR code (ˇ?ˇ) on the front-end page through JavaScript or other methods, such as jquery-qrcode
Advantages: lightweight implementation, reducing image IO, saving traffic
Disadvantages: Not suitable for generating complex QR codes
Of course, in actual applications, these three implementation methods are not completely isolated. We can also combine applications according to the actual situation of the project to maximize efficiency and save costs.
I didn’t have much time at night so I chose jquery-qrcode to study.
jquery-qrcode
jquery-qrcode is a jquery plug-in that can generate QR codes on the browser side. It is self-contained, less than 4k with minimal compression, and has no image download requests. After introducing this class library, you can easily add QR codes to web pages with just one line of code.
It is hosted on github: https://github.com/jeromeetienne/jquery-qrcode
jquery-qrcode mainly contains two files:
1. qrcode.js: QR code algorithm implementation class
2. jquery.qrcode.js: Use jquery to encapsulate qrcode.js, and implement canvas and table rendering to generate QR codes according to user parameters
After compression, there is only one file jquery.qrcode.min.js.
Code implementation
There are actually very detailed instructions and examples on github, so I won’t go into details here.
However, for the convenience of future use, I will reorganize the code based on everyone’s experience on the Internet.
jquery-qrcode.html code is as follows:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>通过jquery-qrcode生成二维码</title> </head> <body> <!-- 引入百度CDN公共库的压缩版jQuery --> <script src="http://libs.baidu.com/jquery/2.0.3/jquery.min.js"></script> <!--引入压缩版jquery.qrcode.js --> <script src="jquery.qrcode.min.js"></script> <!--未压缩则需要引入两个文件jquery.qrcode.js和qrcode.js --> <!--jquery.qrcode.js:jquery封装渲染类库 --> <!--<script src="jquery.qrcode.js"></script>--> <!--qrcode.js:二维码核心计算类库 --> <!--<script src="qrcode.js"></script>--> <script src="jquery.qrcode.min.js"></script> <!--解决中文乱码问题,引入utf16t8.js --> <script src="utf16to8.js"></script> <div id="qrcodeCanvas"></div> <div id="qrcodeTable"></div> <script> //最简用法,render默认是canvas $('#qrcodeCanvas').qrcode("http://www.jb51.net/"); //完整用法,有默认值的均可省略 $('#qrcodeTable').qrcode({ text: utf16to8("脚本之家:http://www.jb51.net/"),//二维码包含的内容,默认只支持英文内容,中文会乱码,通过utf16to8转码可支持中文 render: "table",//渲染方式可选择canvas或table,默认是canvas,canvas方式还支持右键图片下载 width: 256,//宽度,默认是256 height: 256,//高度,默认是256,建议宽度和高度保持一致,否则不容易被识别 typeNumber: -1,//计算模式,默认是-1 //correctLevel: QRErrorCorrectLevel.H,//纠错等级,默认是QRErrorCorrectLevel.H,但是加上correctLevel这一行后无法渲染出二维码 background: "#ffffff",//背景颜色,默认是白色 foreground: "#000000"//前景颜色,默认是黑色 }); </script> <body> </html>
Based on the official sample test, we will find that the recognized Chinese QR code will be garbled.
According to the explanation from netizens with good intentions:
This is related to the js mechanism. The jquery-qrcode library uses charCodeAt() to perform encoding conversion,
This method will obtain its Unicode encoding by default. Generally, decoders use UTF-8, ISO-8859-1, etc.,
There is no problem in English. If it is Chinese, generally Unicode is implemented in UTF-16, with a length of 2 digits, while UTF-8 encoding is 3 digits, so the encoding and decoding of the QR code will not match.
The solution is of course to convert the string into UTF-8 before encoding the QR code
So we can solve this problem with the help of utf16to8.js. The specific code is as follows:
function utf16to8(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; }
The above is all the content shared with you today. I hope it will be helpful to everyone learning jQuery.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

jQuery is a fast, small, feature-rich JavaScript library widely used in front-end development. Since its release in 2006, jQuery has become one of the tools of choice for many developers, but in practical applications, it also has some advantages and disadvantages. This article will deeply analyze the advantages and disadvantages of jQuery and illustrate it with specific code examples. Advantages: 1. Concise syntax jQuery's syntax design is concise and clear, which can greatly improve the readability and writing efficiency of the code. for example,

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute
