Saving barcodes
Include necessary files
If you are using CommonJS.
const barcodeBakeryCommon = require('@barcode-bakery/barcode-nodejs');
const barcodeBakery1D = require('@barcode-bakery/barcode-nodejs/1d');
const BCGColor = barcodeBakeryCommon.BCGColor;
const BCGDrawing = barcodeBakeryCommon.BCGDrawing;
const BCGFont = barcodeBakeryCommon.BCGFont;
const BCGLabel = barcodeBakeryCommon.BCGLabel;
const createSurface = barcodeBakeryCommon.createSurface;
const save = barcodeBakeryCommon.save;
const 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,
createSurface,
save
} from '@barcode-bakery/barcode-nodejs';
import { BCGcode128 } from '@barcode-bakery/barcode-nodejs/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.
const colorBlack = new BCGColor(0, 0, 0);
const 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
const 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.
const 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.
const drawing = new BCGDrawing(createSurface);
drawing.draw(code);
save(drawing, "D:\\hello.png", BCGDrawing.ImageFormat.Png, function () { /* success callback */ });