zoukankan      html  css  js  c++  java
  • jQuery生成二维码 jquery.qrcode.js

    https://github.com/jeromeetienne/jquery-qrcode

    1.将jquery.qrcode.min.js和jquery添加到您的网页中

    <script src="jquery.min.js"></script>
    <script type="text/javascript" src="jquery.qrcode.min.js"></script>

    2.然后创建一个DOM元素去包含生成qr码。

    <div id="qrcode"></div>

    3.然后你在此容器中的添加qrcode

    qrcode支持canvas和table两种方式进行图片渲染,默认使用canvas方式,效率最高,当然要浏览器支持html5。直接调用如下:

    <script>
        jQuery(function(){
        jQuery('#qrcode').qrcode("http://www.jq22.com");
    })
    </script>

    指定二维码的生成方式:

    可以在调用函数的同时输入想要的二维码生成方式(table/canvas)

    //使用table生成
    jQuery('#qrcode').qrcode({
        render: "table",
       200, //宽度
       height:200, //高度
    text:
    "http://www.jq22.com" }); //使用canvas生成 jQuery('#qrcode').qrcode({ render: "canvas", text: "http://www.jq22.com" });

    指定生成二维码的大小:

    可以在调用函数的同时输入想要生成二维码的宽度和高度即可指定生成的二维码的大小

    //生成100*100(宽度100,高度100)的二维码
    jQuery('#qrcode').qrcode({
        render: "canvas", //也可以替换为table
         100,
        height: 100,
        text: "http://www.jq22.com"
    });

    指定生成二维码的色彩样式:

    可以在调用函数的同时输入想要生成二维码的前景色和背景色即可指定生成的二维码的色彩样式

    //生成前景色为红色背景色为白色的二维码
    jQuery('#qrcode').qrcode({
        render: "canvas", //也可以替换为table
        foreground: "#C00",
        background: "#FFF",
        text: "http://www.jq22.com"
    });

    中文ULR生成方法:

    jQuery("#output").qrcode(encodeURI("http://中文中文"));//使用encodeURI进行转码

    or

    jquery-qrcode是采用charCodeAt()方式进行编码转换的。而这个方法默认会获取它的Unicode编码,如果有中文内容,在生成二维码前就要把字符串转换成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;   
    }

    示例:

    var str = toUtf8("中文!");
    $('#code').qrcode(str);
  • 相关阅读:
    iOS图片拉伸技巧
    Swift和OC混合使用
    【转】动态计算UITableViewCell高度详解
    AutoLayout~Label
    【转】初探 iOS8 中的 Size Class
    Objective-C runtime~
    [转]Objective-C中的类和对象(instance)
    Masonry~
    [转] ios学习--openURL的使用方法
    Autolayout~代码实现
  • 原文地址:https://www.cnblogs.com/cnsdhzzl/p/8398664.html
Copyright © 2011-2022 走看看