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.