24.5. Creating barcodes - quick start
Prev | Chapter 24. Linear Barcodes (One Dimensional Barcodes) | Next

## 24.5. Creating barcodes - quick start

The creation of all linear barcode follows the same chema:

1. Create an instance of the encoder for the chosen symbology

2. Create an instance of the backend for the chosen output format (image or postscript)

3. 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
    <?php
    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

Figure 24.3. Encoding "ABC123" with CODE 39.

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
    <?php
    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

Figure 24.4. Encoding "ABC123" with CODE 128.

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.

1. ENCODING_EAN128

2. ENCODING_EAN13

3. ENCODING_EAN8

4. ENCODING_UPCA

5. ENCODING_UPCE

6. ENCODING_CODE39

7. ENCODING_CODE128

8. ENCODING_CODE25

9. ENCODING_CODEI25

10. ENCODING_CODABAR

11. ENCODING_CODE11

12. ENCODING_BOOKLAND

The usage and typical application of each symbology is discussed in Section 24.9.

Prev | Up | Next
24.4. Features | Home | 24.6. Error handling