QRCode for JavaScript – QRコードを生成するには
QRCode for JavaScriptは、Kazuhiko Araseさんが公開されているQRコードを生成するためのライブラリです。
http://www.d-project.com/ からダウンロードすることができ、
ライセンスは、「MIT license」となっています。
<html> <head> <title>QRコード生成サンプル</title> <script type="text/javascript" src="./qrcode.js"></script> <script type="text/javascript"> <!-- function createQRCode(id) { var text = document.getElementById(id).value; if (text == "") { alert("変換する文字列を指定してください。"); document.getElementById(id).focus(); return false; } //QRコードを生成する //・バージョン(型番):4 //・誤り訂正レベル :30%が復元可能 var qr = new QRCode(4, QRErrorCorrectLevel.H); qr.addData(text); qr.make(); document.write("<html>\n"); document.write("<head>\n"); document.write("<title>QRコード生成サンプル</title>\n"); //スタイルシートを設定する document.write("<style type='text/css'>\n"); document.write("table\n"); document.write("{\n"); document.write(" border-width: 0px;\n"); document.write(" border-style: none;\n"); document.write(" border-color: #0000ff;\n"); document.write(" border-collapse: collapse;\n"); document.write("}\n"); document.write(".white\n"); document.write("{\n"); document.write(" border-width: 0px;\n"); document.write(" border-style: none;\n"); document.write(" border-color: #0000ff;\n"); document.write(" border-collapse: collapse;\n"); document.write(" padding: 0;\n"); document.write(" margin: 0;\n"); document.write(" width: 2px;\n"); document.write(" height: 2px;\n"); document.write(" background-color: #000000;\n"); document.write("}\n"); document.write(".black\n"); document.write("{\n"); document.write(" border-width: 0px;\n"); document.write(" border-style: none;\n"); document.write(" border-color: #0000ff;\n"); document.write(" border-collapse: collapse;\n"); document.write(" padding: 0;\n"); document.write(" margin: 0;\n"); document.write(" width: 2px;\n"); document.write(" height: 2px;\n"); document.write(" background-color: #ffffff;\n"); document.write("}\n"); document.write("</style>\n"); document.write("</head>\n"); //QRコードを描画する document.write("<table>\n"); for (var i = 0; i < qr.getModuleCount(); i++) { document.write("<tr>\n"); for (var j = 0; j < qr.getModuleCount(); j++) { if (qr.isDark(i, j)) { document.write("<td class='white' />\n"); } else { document.write("<td class='black' />\n"); } } document.write("</tr>\n"); } document.write("</table>\n"); document.write("</html>\n"); } //--> </script> </head> <body onload="document.getElementById('text').focus();"> <form> <input type="text" id="text" size="30" /> <input type="button" id="create" value="生成" onclick="createQRCode('text');" /> </form> </body> </html>
QRコードに変換する文字列を指定して、生成ボタンを押下すると、
QRコードの規格では、バージョン(型番)が1~40まで定義されていますが、
QRCode for Javaと同様に、QRCode for JavaScriptでは、
バージョン(型番)が1~10までしか対応していないようですし、
まだ日本語にも対応していないようです。
関連記事
コメント 0