Saving barcodes

Include necessary files


var barcodeBakeryCommon = require('barcode-bakery-common');
var barcodeBakery1D = require('barcode-bakery-1d');
var Color = barcodeBakeryCommon.Color;
var Drawing = barcodeBakeryCommon.Drawing;
var Font = barcodeBakeryCommon.Font;
var Label = barcodeBakeryCommon.Label;

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.


var colorFront = new Color(0, 0, 0);
var colorBack = new Color(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.


var font = new Font('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.


var code = new barcodeBakery1D.Code128();
code.setScale(2); //Resolution
code.setThickness(30); // Thickness
code.setBackgroundColor(colorFront); // Color of bars
code.setForegroundColor(colorBack); // 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 Drawing 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 Drawing.IMG_FORMAT_PNG or Drawing.IMG_FORMAT_JPEG to have a PNG or a JPG file. Then pass in the callback.


var drawing = new Drawing(barcode, colorBack);
drawing.save("D:\\hello.png", Drawing.IMG_FORMAT_PNG, function () { /* success callback */ });