glbarcode++
 All Classes Namespaces Files Functions Variables Typedefs Macros Pages
Usage

Abstract Barcode Class

The abstract glbarcode::Barcode class is the base class for all glbarcode++ barcode types. This class provides the core interface for these implementations.

Concrete Barcode Classes

The first step in generating a barcode is accomplished by instantiating the appropriate concrete barcode class. These concrete classes are derived from the glbarcode::Barcode base class. glbarcode++ currently includes the following concrete barcode classes:

Class Symbology Type ID Notes
glbarcode::BarcodeCode39 Code39 "code39"
glbarcode::BarcodeCode39Ext Extended Code39 "code39ext" Code39 with extended encoding to support full ASCII data.
glbarcode::BarcodeUpcA UPC-A "upc-a"
glbarcode::BarcodeEan13 EAN-13 "ean-13"
glbarcode::BarcodeOnecode USPS ONECODE "onecode"
glbarcode::BarcodePostnet USPS POSTNET "postnet" Supports all valid USPS data lengths: 5, 8 and 11.
glbarcode::BarcodePostnet5 USPS POSTNET-5 "postnet-5" Only supports data length of 5 (ZIP only)
glbarcode::BarcodePostnet9 USPS POSTNET-9 "postnet-9" Only supports data length of 9 (ZIP+4)
glbarcode::BarcodePostnet11 USPS POSTNET-11 "postnet-11" Only supports data length of 11 (ZIP+4+DC)
glbarcode::BarcodeCepnet CEPNET "cepnet" Brazilian Post barcode. Based on USPS POSTNET.
glbarcode::BarcodeDataMatrix Data Matrix "datamatrix" Data Matrix IEC16022 ECC 200
glbarcode::BarcodeQrcode QR Code "qrcode" Simple front-end to libqrencode.

For example, to instantiate a Code39 barcode object:

The Barcode Factory

Barcodes can also be instantiated using the glbarcode::Factory barcode factory.

A list (TypeIdList) of supported barcode type IDs can be obtained with the getSupportedTypes() method. A type ID is simply a standard string (std::string). For example:

for ( glbarcode::TypeIdList::iterator iter = typeIds.begin(); iter != typeIds.end(); iter++ )
{
std::cerr << *iter << std::endl;
}

Finally, a barcode is instantiated using the createBarcode() method. For example, to instantiate a Code39 barcode object:

where "code39" is the corresponding type ID string from the table in Concrete Barcode Classes. Valid IDs can also be obtained using the getSupportedTypes() method as described above.

Options

The glbarcode::Barcode class currently supports the following options that may be applied to any barcode type:

  • showText is a boolean option to control whether or not text is included in the final barcode rendering. This option is not honored by all barcode types.
  • checksum is a boolean option to control whether or not a checksum or check digit is automatically generated for the barcode. This option is not honored by all barcode types.

Options are set using the appropriate set accessor methods. These set accessors are designed to allow chaining of multiple accessors as in the following example code:

using namespace glbarcode;
...
/* Set options. */
bc.setChecksum(true).setShowText(true);

Options can also be queried using the appropriate get accessors, as in the following example code:

using namespace glbarcode;
...
bool textFlag = bc.showText(); /* Is the showText option set? */

Individual concrete barcode classes may also implement options specific to its barcode type or symbology.

Building Barcodes

A barcode is "built" using the glbarcode::Barcode::build() method:

glbarcode::Barcode::build( std::string data,
double w,
double h );

where

  • data is a string containing the raw barcode data to be encoded
  • w is the requested width of the barcode in points (1 point = 1/72 inch). This parameter is only a suggestion – the actual width of the resulting barcode may differ due to requirements for minimum symbol sizes for the specific symbology standard. Setting w=0 will allow the barcode's height to be autosized based on a nominal symbol size.
  • h is the requested height of the barcode in points (1 point = 1/72 inch). This parameter is only a suggestion – the actual height of the resulting barcode may differ due to requirements for minimum symbol sizes for the specific symbology standard. Setting w=0 will allow the barcode's height to be autosized based on a nominal symbol size.

Rendering Barcodes

Barcodes are rendered using the glbarcode::Barcode::render() method:

where

glbarcode++ currently provides the following renderers:

Class Description
glbarcode::RendererDebug A renderer that simply prints drawing information to stdout.
glbarcode::RendererSvg Scalable Vector Graphics (SVG) renderer. Can render to a given filename or to stdout.
glbarcode::RendererEps Encapsolated Postscript (EPS) renderer. Can render to a given filename or to stdout.

For example, the following will render barcode to an SVG file named "example.svg":

using namespace glbarcode;
Barcode *barcode = ...
barcode->render( RendererSvg().filename( "example.svg" ) );

Putting It All Together

The following simple program will create a Code39 barcode object encoding the data "EXAMPLE-123" and rendering it to an SVG file named "Example.svg":

using namespace glbarcode;
int main( int argc, char **argv )
{
/* Create barcode object. */
Barcode *bc = Factory::createBarcode( "code39" );
/* Set barcode options to show text and generate check digit, and then build */
bc->setChecksum(true).setShowText(true).build( "EXAMPLE-123", 288, 72 );
/* Render barcode. */
bc->render( RendererSvg().setFilename( "Example.svg" ) );
/* Cleanup. */
delete bc;
}

This example will produce the following SVG file:

Example.svg
Output of above example 'Example.svg'