0

I am working on reading multiple barcodes, from uploaded images in node.js. For that I am using javascript-barcode-reader, which needs ImageData as input. to get ImageData of an image, I had to make use of node-canvas (as I have to do it all in Nodejs, not front-end).

bellow code executes without any warning/error. Currently, I am only using a single barcode image, but I am getting code as null.

what is going wrong here, why I am getting null value.

const Canvas = require('canvas');
const javascriptBarcodeReader = require('javascript-barcode-reader');

var filepath = './images/Code-2of5.jpg';
var buf = fs.readFileSync(filepath);
var canvas = new Canvas(640, 480);
var Image = Canvas.Image;
var ctx = canvas.getContext('2d');

fs.readFileSync(filepath, function (err, squid) {
    if (err) throw err;
    let img = new Image;
    img.src = squid;
    img.onload = function () {
        ctx.drawImage(img, 0, 0, 640, 480);
    }
});

let imgData = ctx.getImageData(0, 0, 640, 480);
const code = javascriptBarcodeReader(imgData /* ImageData */ , { 
    barcode: 'code-2of5', // 'code-128'
    type: 'interleaved', //standard/interleaved optional type
});

console.log(code); // output as null

Initially, I am trying out for only single barcode image.

enter image description here

Nikhil Kinkar
  • 761
  • 8
  • 31

1 Answers1

1

there are 2 gotchas here:

  1. invalid usage readFileSync with callback.
  2. assigning img.src before register img.onload handler.

check this working example both sync and promisified version

    const fs = require('fs');
    const Canvas = require('canvas');
    const javascriptBarcodeReader = require('javascript-barcode-reader');

    function readBarCodeSync(imagePath) {
      const canvas = new Canvas(640, 480);
      const Image = Canvas.Image;
      const ctx = canvas.getContext('2d');

      const img = new Image;
      img.onload = function () {
        ctx.drawImage(img, 0, 0, 640, 480);
      }
      img.src = fs.readFileSync(imagePath);

      const imgData = ctx.getImageData(0, 0, 640, 480);
      const code = javascriptBarcodeReader(imgData /* ImageData */ , {
        barcode: 'code-2of5', // 'code-128'
        type: 'interleaved', //standard/interleaved optional type
      });

      return code;
    }

    function readBarCode (imagePath) {
      const canvas = new Canvas(640, 480);
      const Image = Canvas.Image;
      const ctx = canvas.getContext('2d');

      const img = new Image;
      img.onload = function () {
        ctx.drawImage(img, 0, 0, 640, 480);
      }
      return new Promise((resolve,reject)=>{
        fs.readFile(imagePath,(err,buf)=>{
          if(err) return reject(err);
          img.src = buf;
          const imgData = ctx.getImageData(0, 0, 640, 480);
          const code = javascriptBarcodeReader(imgData /* ImageData */ , {
            barcode: 'code-2of5', // 'code-128'
            type: 'interleaved', //standard/interleaved optional type
          });
          resolve(code);
        });
      });
    }

    var imagePath = './images/Code-2of5.jpg';

    console.log('sync version', readBarCodeSync(imagePath))
    readBarCode(imagePath)
    .then(barCode=>{
      console.log('async version', barCode);
    })
    .catch(err=>{
      console.error(err);
    })
Mohammed Essehemy
  • 2,006
  • 1
  • 16
  • 20