26.3. Example script

26.3.1. Example 1 - Setting the shape

The following example shows how to

  • Set the shape to 64x64

  • Use ASCII encoding

  • Adjust the quiet zone

Example 26.4. Datamatrix example, setting quiet zone and ASCII encoding (datamatrix_ex4.php)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php // content="text/plain; charset=utf-8"
require_once('jpgraph/datamatrix/datamatrix.inc.php');
 
$data = 'This is a 64x64 datamatrix symbol';
 
// Create and set parameters for the encoder
$encoder = DatamatrixFactory::Create(DMAT_64x64);
$encoder->SetEncoding(ENCODING_TEXT);
 
// Create the image backend (default)
$backend = DatamatrixBackendFactory::Create($encoder);
$backend->SetModuleWidth(3);
 
// Adjust the Quiet zone
$backend->SetQuietZone(10);
 
// Create the barcode from the given data string and write to output file
try {
    $backend->Stroke($data);
} catch (Exception $e) {
    $errstr = $e->GetMessage();
    echo "Datamatrix error message: $errstr\n";
}
 
?>


Figure 26.10. Datamatrix example, setting quiet zone and ASCII encoding (datamatrix_ex4.php)

Datamatrix example, setting quiet zone and ASCII encoding (datamatrix_ex4.php)


26.3.2. Example 2 - Writing to a file

The following example shows how to generate a barcode and write it to a file.

Example 26.5. Datamatrix example, writing to a file (datamatrix_ex6.php)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php // content="text/plain; charset=utf-8"
require_once('jpgraph/datamatrix/datamatrix.inc.php');
 
$data = 'This is a datamatrix symbol';
 
$outputfile = 'dm_ex6.png';
 
// Create and set parameters for the encoder
$encoder = DatamatrixFactory::Create();
$encoder->SetEncoding(ENCODING_TEXT);
 
// Create the image backend (default)
$backend = DatamatrixBackendFactory::Create($encoder);
$backend->SetModuleWidth(5);
$backend->SetQuietZone(10);
 
// Set other than default colors (one, zero, background)
$backend->SetColor('navy','white');
 
// Create the barcode from the given data string and write to output file
$dir = dirname(__FILE__);
$file = '<span style="font-weight:bold;">"'.$dir.'/'.$outputfile.'"</span>';
try {
    $backend->Stroke($data,$outputfile);
    echo 'Barcode sucessfully written to file: '.$file;
} catch (Exception $e) {
    $errstr = $e->GetMessage();
    $errcode = $e->GetCode();
    echo "Failed writing file: ".$file.'<br>';
    echo "Datamatrix error ($errcode). Message: $errstr\n";
}
 
?>


26.3.3. Example 3 - Creating postscript output

The following example shows hot to generate a postscript output.

When

Example 26.6. Datamatrix example, creating postscript output (datamatrix_ex7.php)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php // content="text/plain; charset=utf-8"
require_once('jpgraph/datamatrix/datamatrix.inc.php');
 
$data = 'A Datamatrix barcode';
 
// Create and set parameters for the encoder
$encoder = DatamatrixFactory::Create();
$encoder->SetEncoding(ENCODING_BASE256);
 
// Create the image backend (default)
$backend = DatamatrixBackendFactory::Create($encoder, BACKEND_PS);
$backend->SetModuleWidth(3);
 
try {
    $ps_txt = $backend->Stroke($data);
    echo '<pre>'.$ps_txt.'</pre>';
} catch (Exception $e) {
    $errstr = $e->GetMessage();
    echo "Datamatrix error message: $errstr\n";
}
 
?>


The resulting output is shown in Figure 26.11 below

Figure 26.11. Datamatrix postscript output

Datamatrix postscript output

26.3.4. Example 4 - Changing background color

The following example shows how to modify the colors in the barcode.

Example 26.7. Datamatrix example, changing colors (datamatrix_ex5.php)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php // content="text/plain; charset=utf-8"
require_once('jpgraph/datamatrix/datamatrix.inc.php');
 
$data = 'This is a 20x20 symbol';
 
// Create and set parameters for the encoder
$encoder = DatamatrixFactory::Create(DMAT_20x20);
$encoder->SetEncoding(ENCODING_TEXT);
 
// Create the image backend (default)
$backend = DatamatrixBackendFactory::Create($encoder);
 
// By default the module width is 2 pixel so we increase it a bit
$backend->SetModuleWidth(4);
 
// Set Quiet zone
$backend->SetQuietZone(10);
 
// Set other than default colors (one, zero, quiet zone/background)
$backend->SetColor('navy','white','lightgray');
 
// Create the barcode from the given data string and write to output file
try {
    $backend->Stroke($data);
} catch (Exception $e) {
    $errstr = $e->GetMessage();
    echo "Datamatrix error message: $errstr\n";
}
 
?>


Figure 26.12. Datamatrix example, changing colors (datamatrix_ex5.php)

Datamatrix example, changing colors (datamatrix_ex5.php)