LoginSignup
20
24

More than 5 years have passed since last update.

日本語QRコードは割と簡単に作れる

Last updated at Posted at 2018-02-08

この記事について

QRコードを出力するjQueryプラグイン「jquery.qrcode.js」の活用法を紹介します。

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

日本語の文字列をQRコードに出力する方法をネットで調べると、「無理」とか「Google Chart APIを使う」とかの面倒な方法ばかり出てきて困りましたが、ものの試しにencoding.js で文字コードをShift_JISに変換してみたところ、あっさりと出力できました。

サンプルアプリはこちら
https://okoppe8.github.io/qrcode_japanese/index.html

読み取り時の注意

ほとんどのスマホアプリは日本語QRコードの読み取りに対応していません。
読み取りはQRコード開発元の「デンソーウェーブ 公式QRコードリーダー」を使うと確実です。これ普通に良アプリでオススメです。

[公式QRコードリーダー”Q”デンソーウェーブの無料QRアプリ]

「Google Chart API」があるのでWebサイトで採用する意味は低いですが、社内システムやローカルアプリ(ブラウザだけで動くので)ではなかなか使い道があります。
外部へメール等ができない状況で数百文字の情報をPCからスマホにコピーする用途などに使えます。

QRコードの文字コードはShift_JIS

ウィキペディアにも載ってた。
https://ja.wikipedia.org/wiki/QR%E3%82%B3%E3%83%BC%E3%83%89

unicode全盛の世になっても、Shift_JISはQRコードの仕様の中に生き続けます。

サンプルコード

<!DOCTYPE html>
<html lang="ja">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>qrcode</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
        crossorigin="anonymous">
</head>

<body>
    <div class="container-fluid">
        <h2>日本語QRコード作成 サンプル</h2>
        <div class="row" style="padding:1em">
            <div class="col-sm-6">
                <form>
                    <div>
                        <label>内容を入力して出力ボタンをクリック(全角600字強までOK)</label>
                        <div>
                            <textarea type="text" id="value" style="width:90%;height:15vh"></textarea>
                        </div>
                    </div>
                    <div>
                        <div class="btn-group" >
                            <button class="btn btn-primary" value="100" name="size"></button>
                            <button class="btn btn-primary" value="300" name="size"></button>
                            <button class="btn btn-primary" value="500" name="size"></button>
                        </div>
                    </div>
                </form>
            </div>
            <div id="QRCode" class="col-sm-6" style="color:crimson;padding:1em"></div>
        </div>
        <div style="padding:1em">
            <p>読み取りアプリはこちら(公式QRコードリーダー”Q”デンソーウェーブの無料QRアプリ)</p>
            <a class="btn btn-success" href="https://itunes.apple.com/jp/app/id911719423">iphone</a>
            <a class="btn btn-success" href="https://play.google.com/store/apps/details?id=com.arara.q">android</a>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
        crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
    <script src="js/jquery.qrcode.min.js"></script>
    <script src="js/encoding.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {

            $("button[name='size']").on("click", function (e) {
                e.preventDefault();
                var size = $(this).val();
                var source = Encoding.convert($('#value').val(), 'SJIS')

                try {
                    $('#QRCode').html("").qrcode({
                        width: size,
                        height: size,
                        text: source,
                    });
                } catch (e) {
                    $('#QRCode').html("").append("文字数オーバーです:<br>" + e);
                }
            })
        })
    </script>
</body>

</html>
20
24
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
24