<stránka
xmlns="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/strana"
xmlns:m="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/makro">
<nadpis>Reading barcodes and QR</nadpis>
<perex>recognize barcodes (including QR) in image files or streams</perex>
<m:pořadí-příkladu>05400</m:pořadí-příkladu>
<text xmlns="http://www.w3.org/1999/xhtml">
<p>
Either ubiquitous 1D barcodes (procudct labels, ISBN in form of EAN-13 etc.) or 2D barcodes (QR containing hyperlinks, vCards etc.)
encode numbers, texts or other data into images that can be printed and scanned or photographed later.
Since <m:a href="release-v0.18">v0.18</m:a>, <m:name/> can interact with this technology.
</p>
<p>
The <code>relpipe-in-barcode</code> tool reads image data from the standard input, so we can feed a file containing a barcode into it or we can generate one on-the-fly.
We can also use <code>tee</code> command to store generated image data in a file.
</p>
<m:pre jazyk="shell"><![CDATA[echo -n "Big Fun" | qrencode -o - | tee qr.png | relpipe-in-barcode | relpipe-out-tabular]]></m:pre>
<p>This pipeline generates this relation:</p>
<m:pre jazyk="text"><![CDATA[symbol:
╭──────────────┬───────────────┬────────────────┬─────────────┬─────────────┬─────────────────┬──────────────────╮
│ id (integer) │ type (string) │ value (string) │ x (integer) │ y (integer) │ width (integer) │ height (integer) │
├──────────────┼───────────────┼────────────────┼─────────────┼─────────────┼─────────────────┼──────────────────┤
│ 0 │ QR-Code │ Big Fun │ 12 │ 12 │ 63 │ 63 │
╰──────────────┴───────────────┴────────────────┴─────────────┴─────────────┴─────────────────┴──────────────────╯
Record count: 1]]></m:pre>
<p>
There might be multiple barcodes (symbols) in the image file and we obtain also their sizes and positions.
We may read a <a href="img/barcode-qr-IMG_5758.jpeg">photo</a>:
</p>
<m:pre jazyk="shell"><![CDATA[cat barcode-qr-IMG_5758.jpeg | relpipe-in-barcode | relpipe-out-tabular]]></m:pre>
<p>and get plenty of symbols:</p>
<m:pre jazyk="text" src="examples/barcode-qr-IMG_5758.txt"/>
<p>
There is also (a bit experimental) example of barcode <m:a href="release-v0.15">streamlet</m:a> that is called from <code>relpipe-in-filesystem</code>
and works with several files at once (can even run in parallel, like any other streamlet):
</p>
<m:pre jazyk="shell"><![CDATA[find -print0 | relpipe-in-filesystem --parallel 8 --file name --streamlet barcode-reader | …]]></m:pre>
<p>
When we are generating QR codes and use Unicode characters (e.g. the <code>→</code> arrow):
</p>
<m:pre jazyk="shell"><![CDATA[echo -n "extreme→impression" | qrencode -o - | relpipe-in-barcode | relpipe-out-csv]]></m:pre>
<p>we may get unexpected result:</p>
<m:pre jazyk="text"><![CDATA["id","type","value","x","y","width","height"
"0","QR-Code","extreme竊段mpression","12","12","74","74"]]></m:pre>
<p>
QR codes use the ISO 8859-1 encoding as default and readers usually do some heuristics to guess actual encoding.
Sometimes it works, sometimes not.
In this particular case, the reader accidentally thought that the encoding was SJIS.
So we can <i>fix</i> it by adding <code>| iconv -t SJIS</code> at the end of the pipeline.
</p>
<p>
But this is just a workaround.
We rather want to add UTF-8 BOM or ECI (Extended Channel Interpretations) to make the reader use the same encoding as we did (UTF-8):
</p>
<m:pre jazyk="shell"><![CDATA[echo -ne "\xEF\xBB\xBFextreme→impression" | qrencode -o - | relpipe-in-barcode | relpipe-out-csv]]></m:pre>
<p>and get desired result:</p>
<m:pre jazyk="text"><![CDATA["id","type","value","x","y","width","height"
"0","QR-Code","extreme→impression","12","12","74","74"]]></m:pre>
<p>
Because the <code>relpipe-in-barcode</code> tool and the <code>barcode-reader</code> streamlet produce data in machine readable form,
we can use them not only for manual ah-hoc reading but also in scripts or batch processing –
e.g. extract payment information from invoices or contact information from scanned business cards, catalogize books or read package labels.
</p>
</text>
</stránka>