はじめに

最近はQRコード決済、バーコード決済が増えており、どのような仕組みになっているのかが気になった。
まずは、仕組みを知る一歩としてQRコードを生成する方法について調べてみた。
今回は qrencodeというLinuxのパッケージソフトウェアを使用していみたいと思う。

環境

1
2
3
Windows10 Professional
WSL2 - (Ubuntu22.04 LTS)
qrencode version 4.1.1

qrencodeのインストール

1
$ sudo apt-get install qrencode

実行↓

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libqrencode4
The following NEW packages will be installed:
  libqrencode4 qrencode
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 49.2 kB of archives.
After this operation, 133 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu jammy/universe amd64 libqrencode4 amd64 4.1.1-1 [24.0 kB]
Get:2 http://archive.ubuntu.com/ubuntu jammy/universe amd64 qrencode amd64 4.1.1-1 [25.2 kB]
Fetched 49.2 kB in 1s (41.1 kB/s)
Selecting previously unselected package libqrencode4:amd64.
(Reading database ... 24915 files and directories currently installed.)
Preparing to unpack .../libqrencode4_4.1.1-1_amd64.deb ...
Unpacking libqrencode4:amd64 (4.1.1-1) ...
Selecting previously unselected package qrencode.
Preparing to unpack .../qrencode_4.1.1-1_amd64.deb ...
Unpacking qrencode (4.1.1-1) ...
Setting up libqrencode4:amd64 (4.1.1-1) ...
Setting up qrencode (4.1.1-1) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...
/sbin/ldconfig.real: /usr/lib/wsl/lib/libcuda.so.1 is not a symbolic link

使い方

1
qrencode -h
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
horiba@DESKTOP-M2SDB0M:~$ qrencode -h
qrencode version 4.1.1
Copyright (C) 2006-2017 Kentaro Fukuchi
Usage: qrencode [-o FILENAME] [OPTION]... [STRING]
Encode input data in a QR Code and save as a PNG or EPS image.

  -h           display this message.
  --help       display the usage of long options.
  -o FILENAME  write image to FILENAME. If '-' is specified, the result
               will be output to standard output. If -S is given, structured
               symbols are written to FILENAME-01.png, FILENAME-02.png, ...
               (suffix is removed from FILENAME, if specified)
  -r FILENAME  read input data from FILENAME.
  -s NUMBER    specify module size in dots (pixels). (default=3)
  -l {LMQH}    specify error correction level from L (lowest) to H (highest).
               (default=L)
  -v NUMBER    specify the minimum version of the symbol. (default=auto)
  -m NUMBER    specify the width of the margins. (default=4 (2 for Micro))
  -d NUMBER    specify the DPI of the generated PNG. (default=72)
  -t {PNG,PNG32,EPS,SVG,XPM,ANSI,ANSI256,ASCII,ASCIIi,UTF8,UTF8i,ANSIUTF8,ANSIUTF8i,ANSI256UTF8}
               specify the type of the generated image. (default=PNG)
  -S           make structured symbols. Version number must be specified with '-v'.
  -k           assume that the input text contains kanji (shift-jis).
  -c           encode lower-case alphabet characters in 8-bit mode. (default)
  -i           ignore case distinctions and use only upper-case characters.
  -8           encode entire data in 8-bit mode. -k, -c and -i will be ignored.
  -M           encode in a Micro QR Code.
  -V           display the version number and copyrights of the qrencode.
  [STRING]     input data. If it is not specified, data will be taken from
               standard input.
1
man qrencode
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
QRENCODE(1)                                    General Commands Manual                                    QRENCODE(1)

NAME
       qrencode - Encode input data in a QR Code and save as a PNG or EPS image.

SYNOPSIS
       qrencode [-o FILENAME] [OPTION]...  [STRING]

DESCRIPTION
       Libqrencode  is a library for encoding data in a QR Code symbol, a kind of 2D symbology that can be scanned by
       handy terminals such as a mobile phone with CCD. The capacity of QR Code is up to 7000 digits or 4000  charac‐
       ters, and has high robustness.

       Qrencode  is  a utility software using libqrencode to encode string data in a QR Code and save as a PNG or EPS
       image.

OPTIONS
       -h, --help
              display help message.

       -o FILENAME, --output=FILENAME
              write image to FILENAME. If '-' is specified, the result will be output to standard output.  If  -S  is
              given,  structured symbols are written to FILENAME-01.png, FILENAME-02.png, ... (suffix is removed from
              FILENAME, if specified)

       -r FILENAME, --read-from=FILENAME
              read input data from FILENAME.

       -s NUMBER, --size=NUMBER
              specify the size of dot (pixel). (default=3)

       -l {LMQH}, --level={LMQH}
              specify error correction level from L (lowest) to H (highest). (default=L)

       -v NUMBER, --symversion=NUMBER
              specify the minimum version of the symbol. See SYMBOL VERSIONS for more information. (default=auto)

       -m NUMBER, --margin=NUMBER
              specify the width of margin. (default=4)

       -d NUMBER, --dpi=NUMBER
              specify the DPI of the generated PNG. (default=72)

       -t {PNG,PNG32,EPS,SVG,XPM,ANSI,ANSI256,ASCII,ASCIIi,UTF8,UTF8i,ANSIUTF8,ANSIUTF8i,ANSI256UTF8}
       --type={PNG,PNG32,EPS,SVG,XPM,ANSI,ANSI256,ASCII,ASCIIi,UTF8,UTF8i,ANSIUTF8,ANSIUTF8i,ANSI256UTF8}
              specify the type of the generated image. (default=PNG)

       -S, --structured
              make structured symbols. Version number must be specified with '-v'.

       -k, --kanji
              assume that the input text contains kanji (shift-jis).

       -c, --casesensitive
              encode lower-case alphabet characters in 8-bit mode. (default)

       -i, --ignorecase
              ignore case distinctions and use only upper-case characters.

       -8, --8bit
              encode entire data in 8-bit mode. -k, -c and -i will be ignored.

       -M, --micro
              encode in a Micro QR Code. See MICRO QR CODE for more information.

       --rle  enable run-length encoding for SVG.

       --svg-path
              use single path to draw modules for SVG.

       --inline
              only useful for SVG output, generates an SVG without the XML tag.

QRコードの生成

画像を出力する

1
qrencode -o k-bushi.png "https://k-bushi.com"

できた画像↓
k-bushi_qr

端末に出力する

1
qrencode -t ansi "https://k-bushi.com"
output_ansi

SVGとして出力する

1
qrencode -t SVG "https://k-bushi.com"

output_svg ↓そのタグを貼り付けたやつ

参考サイト様

おわりに

QRコードの生成方法について知ることができた。
最近、ギンダコハイボール横丁という東銀座の居酒屋に行ったのだが、その際にQRコードで注文をするシステムだったため、気になってQRコードを生成する方法について調べた。
上記のシステムについて、いつか妄想で仕組みを組み立てて見たいと思う。
QRコード自体は結構前からあるものだったが、コロナによってさらに認知され、普及しているような気もする。