Introduction
This step-by-step tutorial will guide you through how to build a complete application that analyzes a Code 39 barcode from an image and output the value, type, location, and confidence to the terminal. For this tutorial, you can use any text editor (vim, gedit, emacs, etc.).
You may want to try our sample application first before creating your first project.
Steps
-
Create a new file titled BXDemo.js
-
Import the Barcode module:
const bx = require("barcode-js");
-
Create the parameters to use for recognition: the input image and the barcode type to recognize. Change the file name in this example to an image on your local system:
const filePath = "filePath/fileName.bmp"; const params = { type: bx.BarcodeType.ALL };
Specifying only the barcode type or types that you are interested in will speed up the analysis. For example, you can use "code39", ["code39", "code128"], or "all". If you omit the barcode type completely, "all" is assumed.
-
Perform analyze on your barcode image:
const analyzeBarcodes = async (filePath, params) => { try { const results = await bx.analyze(filePath, params); console.log(JSON.stringify(results, ["type", "value", "confidence"], 2)); } catch(err) { console.error(`There was an error processing this image\n${err}`); } } analyzeBarcodes(filePath, params);
-
That’s it! Now go to the console and run your application to see the result output data:
$ node BXDemo