6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Node.jsでQRコードを読み込む

Posted at

QRコードのデコードをNode.jsで行います。

ハッカソン仙台で発生してた質問から記事化してみました。

Creating and Reading QR Codes with Node.js

QRコード(画像) -> URLなどのテキスト

ってイメージです。

準備

モジュールインストール

qrcode-readerjimpを使います。

$ npm i qrcode-reader
$ npm i jimp

コード

app.js
'use strcit';

const QRReader = require('qrcode-reader');
const fs = require('fs');
const jimp = require('jimp');

async function run(IMAGE_PATH) {
  const img = await jimp.read(fs.readFileSync(IMAGE_PATH));
  const qr = new QRReader();

  const value = await new Promise((resolve, reject) => {
    qr.callback = (err, v) => err != null ? reject(err) : resolve(v);
    qr.decode(img.bitmap);
  });

  return value.result;
}

//呼び出し
run('./qr.png').then(res => {
    console.log(res);
})

実行

app.jsと同じ階層にqr.pngを設置して実行します。

$ node app.js
6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?