Saving barcodes

Include necessary files


let barcodeBakeryCommon = require('@barcode-bakery/barcode-common');
let barcodeBakery1D = require('@barcode-bakery/barcode-1d');
let BCGColor = barcodeBakeryCommon.BCGColor;
let BCGDrawing = barcodeBakeryCommon.BCGDrawing;
let BCGFont = barcodeBakeryCommon.BCGFont;
let BCGLabel = barcodeBakeryCommon.BCGLabel;
let BCGcode128 = barcodeBakery1D.BCGcode128;

Import files

If you are using module resolution with ES6, you can import the files with the import keyword instead of require:


import { BCGColor, BCGDrawing, BCGFont, BCGLabel } from '@barcode-bakery/barcode-common';
import { BCGcode128 } from '@barcode-bakery/barcode-1d';

Generating colors

Before we start generating barcodes, we have to decide which colors we want to use to display our barcode.
Generally, we use black and white bars.


let colorBlack = new BCGColor(0, 0, 0);
let colorWhite = new BCGColor(255, 255, 255);

Font for Label

We will now create a font for writing a label under the barcode. If you don't wish to have a text label, ignore this step.
The first argument is the font name as you would use it in a CSS font-family property. The second argument is the size as you would write it in CSS.


let font = new BCGFont('Arial', '18px');

Creating the barcode

Now, we will create the barcode. There is no parameter for the class constructor; you must call the provided methods to modify the barcode properties (see the manual). At the end of the code, you must call the function parse() in order to analyze the code you want to draw.


let code = new BCGCode128();
code.setScale(2); //Resolution
code.setThickness(30); // Thickness
code.setForegroundColor(colorBlack); // Color of bars
code.setBackgroundColor(colorWhite); // Color of spaces
code.setFont(font); // Font
code.parse("HELLO"); // Text

Saving the barcode to a file

We need to put the barcode on a drawing surface. We will use the BCGDrawing class.
The first argument is the barcode. The last argument is the background color.
To finish, call the save method with the first argument being the file name, followed by the BCGDrawing.ImageFormat.Png or BCGDrawing.ImageFormat.Jpeg to have a PNG or a JPG file. Then pass in the callback.


let drawing = new BCGDrawing(code, colorWhite);
drawing.save("D:\\hello.png", BCGDrawing.ImageFormat.Png, function () { /* success callback */ });