diff --git a/README b/README index b7578cb..2925a05 100644 --- a/README +++ b/README @@ -1,4 +1,7 @@ -This example shows how you can create a QR Code PNG using Javascript and HTML5. +支持中文 + +Demo: +https://chrome.google.com/webstore/detail/sweet-qr-code-maker/hejblflceieoemjadbnlpplaajbneedn Notes: --- diff --git a/qrcode.js b/qrcode.js index 41afbc4..a40853a 100644 --- a/qrcode.js +++ b/qrcode.js @@ -20,7 +20,7 @@ function QR8bitByte(data) { this.mode = QRMode.MODE_8BIT_BYTE; - this.data = data; + this.data = this.toUTF8(data); } QR8bitByte.prototype = { @@ -34,6 +34,26 @@ QR8bitByte.prototype = { // not JIS ... buffer.put(this.data.charCodeAt(i), 8); } + }, + + toUTF8 : function(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; } };