The creation of all linear barcode follows the same chema:
Create an instance of the encoder for the chosen symbology
Create an instance of the backend for the chosen output format (image or postscript)
Encode the data and generate the barcode
Normally only three lines of code is needed to create a basic barcode.
For example, the following code will create a barcode encoded as an image representing the data string "ABC123" using symbology "CODE 39".
1 2 3 4 5 6 7 | require_once('jpgraph_barcode.php'); $symbology = BarcodeFactory::Create (ENCODING_CODE39 ); $barcode = BackendFactory ::Create(BACKEND_IMAGE, $symbology); $barcode ->Stroke('ABC123'); |
The generated barcode is shown in
As can be seen from the code above the basic interface to the library makes use of two abstract factories which creates the appropriate encoder and output backend. This design makes the addition of new output formats and new symbologies transparent for the end user of the library.
If instead we wanted to encode the data string using symbology "CODE 128" instead, it would only be necessary to modify the first line in the above code so instead it would become.
1 2 3 4 5 6 7 | require_once('jpgraph_barcode.php'); $symbology = BarcodeFactory::Create (ENCODING_CODE128 ); $barcode = BackendFactory ::Create(BACKEND_IMAGE, $symbology); $barcode ->Stroke('ABC123'); |
the result of this script is shown in Figure 24.4
As can be seen in the examples above both the backend and the symbology is specified by means of a symbolic constant. The following list shows the symbolic constants available to specify the different supported symbologies.
ENCODING_EAN128
ENCODING_EAN13
ENCODING_EAN8
ENCODING_UPCA
ENCODING_UPCE
ENCODING_CODE39
ENCODING_CODE128
ENCODING_CODE25
ENCODING_CODEI25
ENCODING_CODABAR
ENCODING_CODE11
ENCODING_BOOKLAND
The usage and typical application of each symbology is discussed in Section 24.9.