JS generates two-dimensional code and prints

Source: Internet
Author: User

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

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.