In order to save the memory space in the project development, and generate the two-dimensional code by JS dynamically, the image is not saved in the project, and the picture path does not need to be saved in the database.
This article mainly introduces the web to generate two-dimensional code, of course, ios,android can also generate two-dimensional code by QRCode, and then introduce ...
1. Introduction of JS
<script type= "Text/javascript" src= "Jquery.js" ></script>
<script type= "Text/javascript" src= "Jquery.qrcode.min.js" ></script>
(Note: Download jquery.qrcode.min.js in Https://github.com/jeromeetienne/jquery-qrcode)
2.html page reference
<!--Startprint//Easy to find the start point when printing partially
<div id= "codehtml" >
<div id= "Code" ></div>
</div>
<!--EndPrint//For easy partial printing, find end point--
(Note: The reason why <div id= "code" ></div> external plus a control, is each generation of two-dimensional code, the original two-dimensional code based on a new part of the two-dimensional code, you can delete the test)
3. Custom JS generation QR code
$ ("#codeHtml"). html (' <div id= ' Code ></div> ');
$ ("#code"). QRCode ({
Render: ' Canva ',
width:200,//width
height:200,//height
Text:toutf8 (name)//any content: Chinese garbled, need to convert the code
});
4. Chinese garbled handling
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 >> b) & 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;
}
5. Two-dimensional code printing
function stamp () {
bdhtml = Window.document.body.innerHTML;
Sprnstr = "<!--startprint-->";
Eprnstr = "<!--endprint-->";
prnhtml = Bdhtml.substr (Bdhtml.indexof (SPRNSTR) + 17);
prnhtml = prnhtml.substring (0, Prnhtml.indexof (eprnstr));
var newwindow=window.open ("Print two-dimensional code", "_blank");
NewWindow.document.write (prnhtml);
NewWindow.document.close ();
SetTimeout (function () {
Newwindow.print ();
Newwindow.close ();
}, 100);
}
JS generates two-dimensional code and prints