Manual - Code 128
Introduction
Code 128 allows you to encode all the ASCII characters from 0 to 127. Different encoding tables allow you to encode data more efficiently. When a character is not available in the table, a "change table" character is written. You can select the table when you start your code, it is a good choice to choose a table which contains the first characters you want to write.
If you don't specify a specific table, the table is automatically chosen.
Here is a list of the supported table characters:
- Table A : ASCII 0-95 (capital letters, numbers, special characters)
- Table B : ASCII 32-127 (capital letters, lower case, numbers, special characters)
- Table C : Group numbers by 2 from 00 to 99
Code 128 also contains 4 special characters that can't be written directly. These are the characters FNC1, FNC2, FNC3, and FNC4. You can write them by activating the tilde character (~) with the setTilde() method.
You can pass normal text to the method parse() in order to let this method analyze your text and choose the best encoding method. You can also still force the encoding method; below are the possibilities you can pass to the parse() method:
- auto encoding : automatic encoding method
[Code128.CODE128_A, 'TEXT']
: Table A[Code128.CODE128_B, 'text']
: Table B[Code128.CODE128_C, '012345']
: Table C[[Code128.CODE128_C, '012345'], 'auto encoding']
: multiple encoding method
This class inherits the BCGBarcode1D class.
Extended ASCII character support
In order to support extended ASCII characters such as ü (u-umlaut/diaeresis) or ß (eszett), you have to precede the character by an FNC4 then subtract the unicode point of the character by 128.
The FNC4 code acts as a toggle for only a single character. If you plan to encode more than 1 subsequent character, you can use two consecutive FNC4 characters to latch into the extended values. You can repeat the double FNC4 characters to revert back to non-extended values.
For instance, to encode Grüße, you would do the following:
const text = 'Gr~F4' + String.fromCharCode('ü'.charCodeAt(0) - 128) + '~F4'
+ String.fromCharCode('ß'.charCodeAt(0) - 128) + 'e';
or
const text = 'Gr~F4~F4' + String.fromCharCode('ü'.charCodeAt(0) - 128)
+ String.fromCharCode('ß'.charCodeAt(0) - 128) + '~F4e';
Example
Methods
BCGcode128's Methods
-
BCGcode128(
start ) — Specifies by which table the barcode should start -
setStart(
start ) — Specifies by which table the barcode should start - getTilde() — Gets if the behavior for tilde ~ is modified
-
setTilde(
tilde ) — Modifies the use of the tilde character ~
BCGBarcode1D's Methods
- getThickness() — Returns the thickness of the barcode
-
setThickness(
thickness ) — Specifies the thickness of the barcode - getLabel() — Gets the label
-
setLabel(
label ) — Sets the label - getFont() — Gets the text font for the label
-
setFont(
font ) — Sets the text font for the label - getChecksum() — Gets the checksum appended to the barcode
-
setDisplayChecksum(
display ) — Specifies the checksum to be added to the label
BCGBarcode's Methods
-
parse(
text ) — Analyzes atext message to draw afterwards -
draw(
image ) — Draws the barcode on theimage -
getDimension(
width ,height ) — Returns an array containing the required size for the image - getScale() — Gets the scale of the barcode
-
setScale(
scale ) — Sets the scale of the barcode - getForegroundColor() — Gets the color of the bars
-
setForegroundColor(
color ) — Sets the color of the bars - getBackgroundColor() — Gets the color of the spaces
-
setBackgroundColor(
color ) — Sets the color of the spaces -
setColor(
foregroundColor ,backgroundColor ) — Sets the color of the bars and spaces - getOffsetX() — Gets the X offset
-
setOffsetX(
value ) — Sets the X offset - getOffsetY() — Gets the Y offset
-
setOffsetY(
value ) — Sets the Y offset -
addLabel(
label ) — Adds a label to the graphic -
removeLabel(
label ) — Removes a label from the graphic - clearLabels() — Removes the labels from the graphic
Code Example
'use strict';
import { createServer } from 'http';
import {
BCGColor,
BCGDrawing,
BCGFont,
BCGLabel,
createSurface,
toBuffer
} from '@barcode-bakery/barcode-nodejs';
import { BCGcode128 } from '@barcode-bakery/barcode-nodejs/1d';
http.createServer(function (request, response) {
const font = new BCGFont('Arial', 18);
const colorBlack = new BCGColor(0, 0, 0);
const colorWhite = new BCGColor(255, 255, 255);
// Barcode Part
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('a123');
// Drawing Part
const drawing = new BCGDrawing(createSurface);
toBuffer(drawing, BCGDrawing.ImageFormat.Png, function (err, buffer) {
response.writeHead(200, { "Content-Type": "image/png" });
response.end(buffer);
});
}).listen(8124);
Method explanations
-
BCGcode128(
start ) — Specifies by which table the barcode should startDescriptionThe argument can be A, B, C or null. See thesetStart() method for more information.
The default value is null.
This means the table is automatically chosen. -
setStart(
start ) — Specifies by which table the barcode should startDescriptionThe argument can be A, B, C or null. This selects the table the barcode will start.
The default value is null.
This means the table will be automatically chosen when you provide the text.
The tables contain different characters which can be encoded in the barcode.
Check the introduction of this document to obtain more information. -
getTilde()
—
Gets if the behavior for tilde ~ is modified
DescriptionGets if the behavior for tilde ~ is modified.
See setTilde() for more details.Returnsbool -true if activated,false otherwise -
setTilde(
tilde ) — Modifies the use of the tilde character ~DescriptionBy settingtrue in this argument, the tilde characters (ASCII 126 ~) will be processed as special characters. These are the special characters you can write.
- ~~ : Writes a simple tilde
- ~Fx : Writes a FNCx character, with x varying between 1 and 4
The default value is true.
-
getThickness()
—
Returns the thickness of the barcode
DescriptionThe thickness of the barcode in pixels. The value isn't multiplied by the scale.Returns
int - value in pixels -
setThickness(
thickness ) — Specifies the thickness of the barcodeDescriptionThe thickness of the barcode in pixels. This is the vertical size. -
getLabel()
—
Gets the label
DescriptionReturns the real value that will be displayed with the barcode. You have to have called the
parse() method first.Returnsstring - final label -
setLabel(
label ) — Sets the labelDescriptionThe text label will be written below or above the barcode depending on the barcode. You can write the special valueBCGBarcode1D.Label if you would like your text to be chosen automatically. It will be the value passed to theparse() method. -
getFont()
—
Gets the text font for the label
DescriptionGets the text font for the label.Returns
-
setFont(
font ) — Sets the text font for the labelDescriptionThe value of the argument must be an instance of theBCGFontFile class. -
getChecksum()
—
Gets the checksum appended to the barcode
DescriptionReturns the value that will be appended to the barcode. This method must be called after the method
parse() .Returnsint - checksum added orfalse if no checkum is included -
setDisplayChecksum(
display ) — Specifies the checksum to be added to the labelDescriptionSettingtrue will append the checksum to the default label.
The default value is true.
-
parse(
text ) — Analyzes atext message to draw afterwardsDescriptionThe data you pass to thetext argument must be supported by the type of barcode you use.
Check each barcode's introduction section to obtain more information on how to use this method within each symbology. -
draw(
image ) — Draws the barcode on theimage DescriptionThe value of theimage argument must be an image resource. The size of the image can be defined by the value received fromgetDimension() . -
getDimension(
width ,height ) — Returns an array containing the required size for the imageDescriptionReturns an array in which the first index is the image width and the second index is the image height.
The arguments are used to specify the starting point of the drawing. Should be 0 for both.
TheBCGDrawing class uses this method to create the image resource.Returnsarray(int, int) - [0] is the width, [1] is the height -
getScale()
—
Gets the scale of the barcode
DescriptionGets the scale of the barcode. The value is the number of the "smallest" unit in pixel.Returns
int - value in pixels -
setScale(
scale ) — Sets the scale of the barcodeDescriptionThe barcode will bex times bigger. Then a pixel will bex byx for its size. -
getForegroundColor()
—
Gets the color of the bars
DescriptionGets the color of the bars of the barcode.Returns
-
setForegroundColor(
color ) — Sets the color of the bars -
getBackgroundColor()
—
Gets the color of the spaces
DescriptionGets the color of the spaces of the barcode.Returns
-
setBackgroundColor(
color ) — Sets the color of the spaces -
setColor(
foregroundColor ,backgroundColor ) — Sets the color of the bars and spacesDescriptionAn easy and fast method to set the color of the bars and spaces. Check thesetForegroundColor() andsetBackgroundColor() . -
getOffsetX()
—
Gets the X offset
DescriptionGets the X offset of the barcode in pixels. The value isn't multiplied by the scale.Returns
int - value in pixels -
setOffsetX(
value ) — Sets the X offsetDescriptionSpecifies the X offset of the barcode in pixels multiplied by the scale. The required size returned bygetDimension() will be modified accordingly. -
getOffsetY()
—
Gets the Y offset
DescriptionGets the Y offset of the barcode in pixels. The value isn't multiplied by the scale.Returns
int - value in pixels -
setOffsetY(
value ) — Sets the Y offsetDescriptionSpecifies the Y offset of the barcode in pixels multiplied by the scale. The required size returned bygetDimension() will be modified accordingly. -
addLabel(
label ) — Adds a label to the graphicDescriptionAdds aBCGLabel object to the drawing. -
removeLabel(
label ) — Removes a label from the graphicDescriptionRemoves a specificBCGLabel object from the drawing. -
clearLabels()
—
Removes the labels from the graphic
DescriptionClears the
BCGLabel objects from the drawing.