Raspberry Pi & Node-REDでQRコードを読み取ってみる



この記事は [Node-RED Advent Calendar 2019 ](https://qiita.com/advent-calendar/2019/node-red)14日目の記事です。
# はじめに
QRコードをカメラで読み取り、QRコードを認識させて見ようと試してみました。
カメラの映像とQRコードを表示させたいためにnode-red-contrib-image-toolsを使用してみました。

# 利用するデバイス
- Raspberry Pi 3 (2018-11-13-raspbian-stretch をインストール）
- USB接続のWebカメラ　[BUFFALO BSWHD06M](https://www.buffalo.jp/product/detail/bswhd06mbk.html)

# 使用したノード
- node-red 1.0.3 
- node-red-contrib-image-tools 0.2.5
- node-red-contrib-usbcamera 0.0.6

# Node-REDの設定
<img width="983" alt="スクリーンショット 2019-12-19 18.30.20.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/171737/c2c33f7b-43c7-efe3-e02b-41956286fb81.png">

# 使用したノードはここにありました。

<img width="170" alt="スクリーンショット 2019-12-19 18.29.25.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/171737/81e36fd6-1120-208d-a28b-61c76210038e.png">

## Image ノードは
　送られた画像を設定によって変換してくれるノードです。
## Viewerノードは
  ノードレッドエディター上で画像を表示してくれるスグレモノです。これは便利ですよ。
## 2d Barcode Decoderノードは
　QRコードおよびData Matrixバーコードをデコードするノードです。

# こんな感じになりました。

![video-1576749104.2019-12-19 19_01_20.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/171737/21969779-6dff-62af-9ac4-293fdb05bafd.gif)


# ただし。。。。読み取れず。。。
<img width="249" alt="スクリーンショット 2019-12-19 18.51.52.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/171737/2ccbcda5-d483-bfbb-8919-d4c45198c362.png">

## こんなエラーが発生しました。
<img width="280" alt="スクリーンショット 2019-12-19 18.52.28.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/171737/f35bea5a-aa07-b7bb-8b05-791641c27c08.png">


## ただし、2D Data Matrix barcodeは、うまく検知する。どういうこと？

# フローのソースコードです。
```json-doc

[{"id":"1e33a9ca.e7b606","type":"tab","label":"フロー 1","disabled":false,"info":""},{"id":"e0743022.b16ed","type":"inject","z":"1e33a9ca.e7b606","name":"","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":300,"wires":[["29aa9988.1d0146"]]},{"id":"29aa9988.1d0146","type":"usbcamera","z":"1e33a9ca.e7b606","filemode":"1","filename":"image01.jpg","filedefpath":"1","filepath":"","fileformat":"jpeg","resolution":"1","name":"","x":350,"y":300,"wires":[["77bc3fb8.51eff","3a3883c0.a7586c"]]},{"id":"998072e0.dc7db","type":"image viewer","z":"1e33a9ca.e7b606","name":"","width":160,"data":"payload","dataType":"msg","x":390,"y":460,"wires":[["438025e.e077cdc"]]},{"id":"c0e4d0d3.2f6a3","type":"debug","z":"1e33a9ca.e7b606","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":810,"y":460,"wires":[]},{"id":"77bc3fb8.51eff","type":"image viewer","z":"1e33a9ca.e7b606","name":"","width":160,"data":"payload","dataType":"msg","x":550,"y":300,"wires":[[]]},{"id":"3a3883c0.a7586c","type":"jimp-image","z":"1e33a9ca.e7b606","name":"","data":"payload","dataType":"msg","ret":"img","parameter1":"0x00000000","parameter1Type":"msg","parameter2":"1","parameter2Type":"msg","parameter3":"","parameter3Type":"msg","parameter4":"","parameter4Type":"msg","parameter5":"","parameter5Type":"msg","parameter6":"","parameter6Type":"msg","parameter7":"","parameter7Type":"msg","parameter8":"","parameter8Type":"msg","parameterCount":0,"jimpFunction":"none","selectedJimpFunction":{"name":"none","fn":"none","description":"Just loads the image.","parameters":[]},"x":210,"y":460,"wires":[["998072e0.dc7db"]]},{"id":"31b685cc.72597a","type":"catch","z":"1e33a9ca.e7b606","name":"","scope":null,"uncaught":false,"x":590,"y":580,"wires":[["18df2c41.877c94"]]},{"id":"18df2c41.877c94","type":"debug","z":"1e33a9ca.e7b606","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":770,"y":580,"wires":[]},{"id":"438025e.e077cdc","type":"2d Barcode Decoder","z":"1e33a9ca.e7b606","name":"","data":"payload","dataType":"msg","specification":"{\"QR\": true, \"dataMatrix\": false, \"tryHarder\": true}","specificationType":"json","x":600,"y":460,"wires":[["c0e4d0d3.2f6a3"]]},{"id":"3ba2b7ef.d2b618","type":"comment","z":"1e33a9ca.e7b606","name":"decode QR","info":"","x":130,"y":240,"wires":[]},{"id":"5de17b3.1c98384","type":"comment","z":"1e33a9ca.e7b606","name":"Catch any errors","info":"","x":600,"y":540,"wires":[]}]
```
# 終わりに
node-red-contrib-image-toolsのサンプルにもQRコードを読み取るフローはありましたが同じ状況なので環境のせいかもしれません。
もし、よろしければ試して頂いて動くようでしたら一報を頂ければうれしいです。