隨著移動互聯網普及,催生了二維碼廣泛的應用,生活中隨處可見都是二維碼,而我們在系統開發中,也經常經使用到二維碼,也需要根據不同的實際情況為用戶生成不同的二維碼,追萬網絡技術人員給大家分享一個通過的二維碼生成技術,只需要簡單的調用一個函數即可生成。
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;
}
function showewm(locations,str){//locations顯示位置,str生成的內容
jQuery("#"+locations).qrcode({render:"table",width:"150",height:"150",text:toUtf8(str)/*將內容進行轉碼*/});
}
這兩個函數已做備注,只需要根據實際使用情況來修改即可。
另外還有兩個核心的包含文件,自行下載即可
JQUERY生成二維碼.zip