<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>@sinのブログ</title>
<link>https://ameblo.jp/white-rabbit-0925/</link>
<atom:link href="https://rssblog.ameba.jp/white-rabbit-0925/rss20.xml" rel="self" type="application/rss+xml" />
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com" />
<description>プログラミングなどのメモをそっと自分用にメモ</description>
<language>ja</language>
<item>
<title>QuaggaJSを使ったバーコードリーダ実装</title>
<description>
<![CDATA[ <p>バーコードリーダーを作ることになったのでその時メモです。</p><p>&nbsp;</p><p>今回はJavascriptのQuaggaJSを使用することにしました。</p><p>&nbsp;</p><h3>フォルダ構成</h3><p>今回のフォルダ構成は以下の通りです。</p><p>/js</p><p>/css</p><p>index.html</p><p>&nbsp;</p><p>js・・・javascriptを保存するディレクトリ</p><p>css ・・・スタイルシートを保存するディレクトリ</p><p>&nbsp;</p><p>&nbsp;</p><h3>事前準備</h3><p>カメラアクセス機能を使用するにはhttpsが必要なので証明証が入ったサーバーなりを用意。</p><p>&nbsp;</p><h3>手順</h3><h4>1. ライブラリのインストール</h4><p><a href="https://serratus.github.io/quaggaJS/">https://serratus.github.io/quaggaJS/</a></p><p>&nbsp;</p><p>npmでインストールできますが、今回はzipファイルをダウンロードしてquagga.min.jsを/jsフォルダに保存します。</p><p>&nbsp;</p><h4>2. htmlファイルの作成</h4><p>とりあえず、最低限のhtmlファイルを作成します。</p><p>photo-areaで指定した部分がバーコードの読み取り部分です。</p><p>&nbsp;</p><p>&lt;html&gt;</p><p>&nbsp; &nbsp; &lt;head&gt;</p><p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&lt;link rel="stylesheet"&nbsp;href="css/test.css" /&gt;</p><p>&nbsp; &nbsp;&nbsp;&lt;/head&gt;</p><p>&nbsp; &nbsp; &lt;body&gt;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &lt;h3&gt;バーコード&lt;/h3&gt;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &lt;div id="photo-area" class="viewport"&gt;&lt;/div&gt;</p><p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&lt;script src="js/jquery-3.4.1.min.js" type="text/javascript"&gt;&lt;/script&gt;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &lt;script src="js/quagga.min.js" type="text/javascript"&gt;&lt;/script&gt;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &lt;script src="js/test.js" type="text/javascript"&gt;&lt;/script&gt;</p><p>&nbsp; &nbsp; &lt;/body&gt;</p><p>&lt;/html&gt;</p><pre class="prettyprint linenums"></pre><h4 class="prettyprint linenums">3. Quaggaの設定を記述する。</h4><p class="prettyprint linenums"><span style="font-weight:bold;">javascriptファイル(test.js)に下記内容を記述する。</span></p><div><span style="font-weight:bold;">今回は</span>i2of5バーコードリーダーです。</div><div>&nbsp;</div><div>バーコードの種類は下記に対応してます。</div><div><strong>EAN</strong>,&nbsp;<strong>CODE 128</strong>,&nbsp;<strong>CODE 39</strong>,&nbsp;<strong>EAN 8</strong>,&nbsp;<strong>UPC-A</strong>,&nbsp;<strong>UPC-C</strong>,&nbsp;<strong>I2of5</strong>,&nbsp;<strong>2of5</strong>,&nbsp;<strong>CODE 93</strong>&nbsp;and&nbsp;<strong>CODABAR</strong></div><div>&nbsp;</div><div>&nbsp;</div><p>$(function () {</p><p>&nbsp; &nbsp;&nbsp;startScanner();</p><p>});</p><pre class="prettyprint linenums">const startScanner = () =&gt; {&nbsp; &nbsp; Quagga.init({&nbsp; &nbsp; &nbsp; &nbsp; inputStream: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: "Live",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: "LiveStream",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; target: document.querySelector('#photo-area'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; constraints: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decodeBarCodeRate: 3,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; successTimeout: 500,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; codeRepetition: true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tryVertical: true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frameRate: 15,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width: 352,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height: 288,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; facingMode: "environment"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; decoder: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; readers: [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "i2of5_reader"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; }, function (err) {&nbsp; &nbsp; &nbsp; &nbsp; if (err) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(err);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; console.log("Initialization finished. Ready to start");&nbsp; &nbsp; &nbsp; &nbsp; Quagga.start();&nbsp; &nbsp; &nbsp; &nbsp; // Set flag to is running&nbsp; &nbsp; &nbsp; &nbsp; _scannerIsRunning = true;&nbsp; &nbsp; });&nbsp; &nbsp; Quagga.onProcessed(function (result) {&nbsp; &nbsp; &nbsp; &nbsp; var drawingCtx = Quagga.canvas.ctx.overlay,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drawingCanvas = Quagga.canvas.dom.overlay;&nbsp; &nbsp; &nbsp; &nbsp; if (result) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (result.boxes) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drawingCtx.clearRect(0, 0, parseInt(drawingCanvas.getAttribute("width")), parseInt(drawingCanvas.getAttribute("height")));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.boxes.filter(function (box) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return box !== result.box;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).forEach(function (box) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Quagga.ImageDebug.drawPath(box, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x: 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y: 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, drawingCtx, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: "green",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lineWidth: 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (result.box) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Quagga.ImageDebug.drawPath(result.box, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x: 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y: 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, drawingCtx, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: "#00F",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lineWidth: 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (result.codeResult &amp;&amp; result.codeResult.code) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Quagga.ImageDebug.drawPath(result.line, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x: 'x',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y: 'y'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, drawingCtx, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: 'red',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lineWidth: 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; //barcode read call back&nbsp; &nbsp; Quagga.onDetected(function (result) {&nbsp; &nbsp; &nbsp; &nbsp; console.log(result.codeResult.code);&nbsp; &nbsp; });}</pre><p>Quagga.onProcessed部分は、バーコードに枠を表示する部分です。</p><p>&nbsp;</p><h4>4.スタイルシートの設定</h4><p>このままだと枠が正しい位置に表示されないのでスタイルシート(test.css)で調整します。</p><pre class="prettyprint linenums">#photo-area.viewport {&nbsp; &nbsp;height: 288px;}#photo-area.viewport canvas, video {&nbsp; &nbsp;float: left;&nbsp; &nbsp;height: 288px;}#photo-area.viewport canvas.drawingBuffer, video.drawingBuffer {&nbsp; &nbsp;margin-left: -352px;}</pre><p>注意点は以下の２つです。</p><ul><li>Quagga.initのheightとviewport canvas, videoのheightを合わせる。</li><li>Quagga.initのwidthの値分、viewport canvas, videoのmargin-leftをマイナス設定する。</li></ul><div><h3>&nbsp;</h3></div><div><h3>バーコードリーダーの感度</h3><p>このままでiphoneなどで実際にバーコードを読もうとすると中々認識してくれません。</p><p>おそらくスマフォのカメラのサイズが1600×1200だとすると、videoのサイズを320x240にしているので画像を縮小した際に画像が劣化して読めないんじゃないかと。</p><p>&nbsp;</p><p>&nbsp;</p><p><span>​</span><a href="https://stat.ameba.jp/user_images/20200720/14/white-rabbit-0925/fd/68/p/o0613022614791656444.png"><img alt="" height="155" src="https://stat.ameba.jp/user_images/20200720/14/white-rabbit-0925/fd/68/p/o0613022614791656444.png" width="420"></a></p><p>&nbsp;</p><p>そこで思いついたのが、video自体は640x480のサイズにして、表示は320x240の中央のみを表示すれば感度が上がるのではないかと。</p><p>&nbsp;</p><p>&nbsp;</p><p><a href="https://stat.ameba.jp/user_images/20200720/14/white-rabbit-0925/15/86/p/o0659021314791656742.png"><img alt="" height="136" src="https://stat.ameba.jp/user_images/20200720/14/white-rabbit-0925/15/86/p/o0659021314791656742.png" width="420"></a></p><p>先ず、javascriptファイル(test.js)のQuaggaの設定を640x480に変更します。</p><pre>const startScanner = () =&gt; {&nbsp; &nbsp; Quagga.init({&nbsp; &nbsp; &nbsp; &nbsp; inputStream: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: "Live",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: "LiveStream",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; target: document.querySelector('#photo-area'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; constraints: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decodeBarCodeRate: 3,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; successTimeout: 500,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; codeRepetition: true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tryVertical: true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frameRate: 15,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width: 640,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height: 480,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; facingMode: "environment"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; },}</pre><p>続いてvideoの中央部分を320x240で表示するようにobject-fit: noneをcssに設定します。</p><pre>#photo-area.viewport {&nbsp; &nbsp;height: 240px;&nbsp;   width:320px;}#photo-area.viewport canvas, video {&nbsp; &nbsp;float: left;&nbsp;   width:320px;&nbsp; &nbsp;height: 240px;}#photo-area.viewport canvas.drawingBuffer, video.drawingBuffer {&nbsp; &nbsp;margin-left: -320px;}</pre><p>なお、object-fitはIE/Edgeが対応していないので対応させる場合は<a href="https://github.com/jonathantneal/fitie" target="_blank">fitie.js</a>なんかで。</p><p>&nbsp;</p><p>このバージョンで確認してみるとサクサクっとバーコードを認識してくれます👍</p></div>
]]>
</description>
<link>https://ameblo.jp/white-rabbit-0925/entry-12579659224.html</link>
<pubDate>Wed, 04 Mar 2020 13:21:57 +0900</pubDate>
</item>
</channel>
</rss>
