barcodeを使ってバーコードを生成する

はじめに

バーコード生成というのができるらしいので、試してみる。

環境

Windows 10 Professional
WSL2 - (Ubuntu22.04 LTS)
GNU barcode 0.99

barcodeのインストール

$ sudo apt-get install barcode
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libpaper-utils libpaper1
The following NEW packages will be installed:
  barcode libpaper-utils libpaper1
0 upgraded, 3 newly installed, 0 to remove and 5 not upgraded.
Need to get 91.0 kB of archives.
After this operation, 371 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu jammy/main amd64 libpaper1 amd64 1.1.28build2 [13.8 kB]
Get:2 http://archive.ubuntu.com/ubuntu jammy/universe amd64 barcode amd64 0.99-5 [68.5 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy/main amd64 libpaper-utils amd64 1.1.28build2 [8674 B]
Fetched 91.0 kB in 2s (59.0 kB/s)
Preconfiguring packages ...
Selecting previously unselected package libpaper1:amd64.
(Reading database ... 24928 files and directories currently installed.)
Preparing to unpack .../libpaper1_1.1.28build2_amd64.deb ...
Unpacking libpaper1:amd64 (1.1.28build2) ...
Selecting previously unselected package barcode.
Preparing to unpack .../barcode_0.99-5_amd64.deb ...
Unpacking barcode (0.99-5) ...
Selecting previously unselected package libpaper-utils.
Preparing to unpack .../libpaper-utils_1.1.28build2_amd64.deb ...
Unpacking libpaper-utils (1.1.28build2) ...
Setting up libpaper1:amd64 (1.1.28build2) ...

Creating config file /etc/papersize with new version
Setting up barcode (0.99-5) ...
Setting up libpaper-utils (1.1.28build2) ...
Processing triggers for install-info (6.8-4build1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...
/sbin/ldconfig.real: /usr/lib/wsl/lib/libcuda.so.1 is not a symbolic link

Processing triggers for man-db (2.10.2-1) ...

バーコードの生成

manで仕様を確認 https://linux.die.net/man/1/barcode

$ barcode -h
GNU barcode 0.99 is a tool to convert text strings to printed bars.

barcode: Options:
   -i <arg>     input file (strings to encode), default is stdin
   -o <arg>     output file, default is stdout
   -b <arg>     string to encode (use input file if missing)
   -e <arg>     encoding type (default is best fit for first string)
   -u <arg>     unit ("mm", "in", ...) used to decode -g, -t, -p
   -g <arg>     geometry on the page: [<wid>x<hei>][+<margin>+<margin>]
   -t <arg>     table geometry: <cols>x<lines>[+<margin>+<margin>]
   -m <arg>     internal margin for each item in a table: <xm>[,<ym>]
   -n           "numeric": avoid printing text along with the bars
   -c           no Checksum character, if the chosen encoding allows it
   -E           print one code as eps file (default: multi-page ps)
   -S           print one code as svg file (default: multi-page ps)
   -P           create PCL output instead of postscript
   -p <arg>     page size (refer to the man page)
   -s           streaming mode (refer to the man page)

Known encodings are (synonyms appear on the same line):
        "ean", "ean13", "ean-13", "ean8", "ean-8"
        "upc", "upc-a", "upc-e"
        "isbn"
        "39", "code39"
        "39ext", "code39ext"
        "128c", "code128c"
        "128b", "code128b"
        "128", "code128"
        "128raw"
        "i25", "interleaved 2 of 5"
        "cbr", "codabar"
        "msi"
        "pls", "plessey"
        "code93", "93"
        "code11", "11"
Mail bug reports and suggestions to <bug-barcode@gnu.org>.
$ barcode -b [文字列] -e [エンコーディング] -o [ファイル名]

でとりあえずは出力できそう。

バーコードはISBNとJANしかわからないので、ISBNで生成してみる。

ファイル指定なし

$ barcode -b "1-56592-292-1" -e ISBN

↓PDFみたいなのが出てくる。 ※psファイルというものらしい

%!PS-Adobe-2.0
%%Creator: "barcode", libbarcode sample frontend
%%DocumentPaperSizes: a4
%%EndComments
%%EndProlog

%%Page: 1 1

% Printing barcode for "1-56592-292-1", scaled  1.00, encoded using "ISBN"
% The space/bar succession is represented by the following widths (space first):
% 911113123121122212314111123111111311221222122311221222221111
[
%  height  xpos   ypos  width       height  xpos   ypos  width
   [75.00  19.50  15.00  0.85]      [75.00  21.50  15.00  0.85]
   [70.00  24.50  20.00  2.85]      [70.00  28.00  20.00  1.85]
   [70.00  32.50  20.00  0.85]      [70.00  35.50  20.00  0.85]
   [70.00  38.00  20.00  1.85]      [70.00  42.00  20.00  1.85]
   [70.00  45.00  20.00  1.85]      [70.00  49.50  20.00  0.85]
   [70.00  54.50  20.00  0.85]      [70.00  56.50  20.00  0.85]
   [70.00  59.00  20.00  1.85]      [70.00  63.50  20.00  0.85]
   [75.00  65.50  15.00  0.85]      [75.00  67.50  15.00  0.85]
   [70.00  70.50  20.00  2.85]      [70.00  73.50  20.00  0.85]
   [70.00  77.00  20.00  1.85]      [70.00  80.00  20.00  1.85]
   [70.00  84.00  20.00  1.85]      [70.00  87.00  20.00  1.85]
   [70.00  91.50  20.00  2.85]      [70.00  94.50  20.00  0.85]
   [70.00  98.00  20.00  1.85]      [70.00 101.00  20.00  1.85]
   [70.00 105.00  20.00  1.85]      [70.00 109.00  20.00  1.85]
   [75.00 111.50  15.00  0.85]      [75.00 113.50  15.00  0.85]

]       { {} forall setlinewidth moveto 0 exch rlineto stroke} bind forall
[
%   char    xpos   ypos fontsize
    [(9)   10.00  10.00 12.00]
    [(7)   22.00  10.00  0.00]
    [(8)   29.00  10.00  0.00]
    [(1)   36.00  10.00  0.00]
    [(5)   43.00  10.00  0.00]
    [(6)   50.00  10.00  0.00]
    [(5)   57.00  10.00  0.00]
    [(9)   69.00  10.00  0.00]
    [(2)   76.00  10.00  0.00]
    [(2)   83.00  10.00  0.00]
    [(9)   90.00  10.00  0.00]
    [(2)   97.00  10.00  0.00]
    [(1)  104.00  10.00  0.00]
]   { {} forall dup 0.00 ne {
        /Helvetica findfont exch scalefont setfont
    } {pop} ifelse
    moveto show} bind forall
% End barcode for "1-56592-292-1"

showpage
%%Trailer

-S で SVG出力ができるらしい↓ https://manpages.ubuntu.com/manpages/focal/man1/barcode.1.html SVG出力

barcode -b "1-56592-292-1" -S -e ISBN -o isbn.svg

結果↓

9781565922921

横幅が狭いので大きくしてみたい…。

barcode -b "1-56592-292-1" -g "400x200" -S -e ISBN -o isbn.svg
9781565922921

いい感じになった…。でかい!

-i オプションはバーコードの読み取りができるのかと思ったが、エンコードする文字列を読み込むみたいな感じなのね。

参考

おわりに

身近なバーコード生成について、実際に試してみました。
最近は、オンラインショッピングが多いので店頭に行く機会が少ないので、バーコードを見る機会が多くないかと思います。
久しぶりにバーコードのこの感じを見れて良かったです。

Hugo で構築されています。
テーマ StackJimmy によって設計されています。