PageRenderTime 161ms CodeModel.GetById 98ms app.highlight 52ms RepoModel.GetById 1ms app.codeStats 1ms

/jpgraph/docs/chunkhtml/ch25s02.html

https://code.google.com/
HTML | 477 lines | 458 code | 19 blank | 0 comment | 0 complexity | 7cf78bcac1987bce8bab1024a9c7e1aa MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>25.2.�Creating barcodes</title><link rel="stylesheet" href="manual.css" type="text/css"><meta name="generator" content="DocBook XSL-NS Stylesheets V1.74.0"><link rel="home" href="index.html" title="JpGraph Manual"><link rel="up" href="ch25.html" title="Chapter�25.�Datamatrix (2D-Barcode)"><link rel="prev" href="ch25.html" title="Chapter�25.�Datamatrix (2D-Barcode)"><link rel="next" href="ch25s03.html" title="25.3.�Example script"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">25.2.�Creating barcodes</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch25.html">Prev</a></td><th width="60%" align="center">Chapter�25.�Datamatrix (2D-Barcode)</th><td width="20%" align="right"><a accesskey="n" href="ch25s03.html">Next</a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2614278"></a>25.2.�Creating barcodes</h2></div></div></div><p>In order to use datamatrix barcodes the module
  2                "<code class="filename">datamatrix/datamatrix.inc.php</code>" must first be included.</p><p>Usage of Datamatrix barcodes follows a similar schema as for the linear and PDF417
  3            barcodes with concepts of an encoder and backend. The principle of the overall
  4            encodation process is shown in <a class="xref" href="ch25s02.html#fig.datamatrix-encodation-principle" title="Figure�25.4.�Datamatrix encodation principle">Figure�25.4</a></p><div class="figure"><a name="fig.datamatrix-encodation-principle"></a><p class="title"><b>Figure�25.4.�Datamatrix encodation principle</b></p><div class="figure-contents"><div class="mediaobject"><img src="images/datamatrix-encodation-principle.png" alt="Datamatrix encodation principle"></div></div></div><br class="figure-break"><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2614329"></a>25.2.1.�Getting started</h3></div></div></div><p>Assuming that the library is installed where it can be found by PHP it is now very
  5                simple to create a basic Data Matrix symbol using just default values. We first show
  6                the complete first example and then discuss it. In the following scripts we will
  7                assume that the include path has the core JpGraph directory in its path.</p><p>All barcode creation follows the following basic three steps :</p><p>
  8                </p><div class="orderedlist"><ol type="1"><li><p>Create an instance of the encoder with the chosen datamatrix layout as
  9                            an instance of <code class="code">class Datamatrix</code></p></li><li><p>Create an instance of a suitable backend for the chosen output format
 10                            (image or postscript) by calling the
 11                                <code class="code">DatamatrixBackenFactory::Create()</code></p></li><li><p>Encode data and send it back to the browser or to a file with a call
 12                            to the backend <code class="code">Backend::Stroke()</code> method.</p></li></ol></div><p>
 13            </p><p>The following script shows how to create the simplest possible barcode (in PNG
 14                format) representing the data string "<code class="code">The first datamatrix</code>" encoded
 15                using all default values. The resulting barcode is shown in <a class="xref" href="ch25s02.html#fig.datamatrix_ex00" title="Figure�25.5.�The simplest possible datamatrix script (datamatrix_ex00.php)">Figure�25.5</a></p><p>
 16                </p><div class="example"><a name="example.datamatrix_ex00"></a><p class="title"><b>Example�25.1.�The simplest possible datamatrix script (<code class="filename">datamatrix_ex00.php</code>) </b></p><div class="example-contents"><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
 172
 183
 194
 205
 216
 227
 238
 249
 25</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags">&lt;?php</span><span class="hl-code"> </span><span class="hl-comment">//</span><span class="hl-comment"> content=&quot;text/plain; charset=utf-8&quot;</span><span class="hl-comment"></span><span class="hl-code">
 26</span><span class="hl-reserved">require_once</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">jpgraph/datamatrix/datamatrix.inc.php</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
 27 
 28</span><span class="hl-var">$data</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">The first datamatrix</span><span class="hl-quotes">'</span><span class="hl-code">;
 29 
 30</span><span class="hl-var">$encoder</span><span class="hl-code"> = </span><span class="hl-identifier">DatamatrixFactory</span><span class="hl-code">::</span><span class="hl-identifier">Create</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
 31</span><span class="hl-var">$backend</span><span class="hl-code"> = </span><span class="hl-identifier">DatamatrixBackendFactory</span><span class="hl-code">::</span><span class="hl-identifier">Create</span><span class="hl-brackets">(</span><span class="hl-var">$encoder</span><span class="hl-brackets">)</span><span class="hl-code">;
 32</span><span class="hl-var">$backend</span><span class="hl-code">-&gt;</span><span class="hl-identifier">Stroke</span><span class="hl-brackets">(</span><span class="hl-var">$data</span><span class="hl-brackets">)</span><span class="hl-code">;
 33</span><span class="hl-inlinetags">?&gt;</span></pre></td></tr></table></div></div></div><p><br class="example-break">  </p><div class="figure"><a name="fig.datamatrix_ex00"></a><p class="title"><b>Figure�25.5.�The simplest possible datamatrix script <code class="uri"><a class="uri" href="example_src/datamatrix_ex00.html" target="_top">(<code class="filename">datamatrix_ex00.php</code>)</a></code> </b></p><div class="figure-contents"><span class="inlinemediaobject"><img src="images/datamatrix_ex00.png" alt="The simplest possible datamatrix script (datamatrix_ex00.php)"></span></div></div><p><br class="figure-break">
 34            </p><p>(As can be seen this follows the same principles as the creation of PDF417
 35                symbols)</p><p>The principle is the same for all type of data matrix symbol creations. We first
 36                create an instance of the chosen encoder. In this case a standard ECC-200 encoder by
 37                creating an instance of <code class="code">class Datamatrix</code>. We then create a suitable
 38                backend that handles the output of the barcode. By default the output will be an
 39                image encoded in the PNG image format.</p><p>The final step is to send back the generated image to the browser with a call to
 40                the method <code class="code">Backend::Stroke()</code> with the data to be encoded as its first
 41                argument.</p><p>The example above does not have any error handling. If there is some error in the
 42                process an exception will be thrown in the same way as in other places in the
 43                library. The default exception will display a standard library image error box. An
 44                example of this is shown in <a class="xref" href="ch25s02.html#fig.datamatrix-error-image" title="Figure�25.6.�Datamatrix error image">Figure�25.6</a>. </p><div class="figure"><a name="fig.datamatrix-error-image"></a><p class="title"><b>Figure�25.6.�Datamatrix error image</b></p><div class="figure-contents"><div class="mediaobject"><img src="images/datamatrix_errormsg.png" alt="Datamatrix error image"></div></div></div><br class="figure-break"><p>If some additional processing is necessary and just display a text based
 45                re-formatted error message we could change the above code to catch this exception as
 46                the following example shows.</p><p>
 47                </p><div class="example"><a name="example.datamatrix_ex0"></a><p class="title"><b>Example�25.2.�Datamatrix with basic error handling (<code class="filename">datamatrix_ex0.php</code>) </b></p><div class="example-contents"><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
 482
 493
 504
 515
 526
 537
 548
 559
 5610
 5711
 5812
 5913
 6014
 6115
 6216
 6317
 6418
 65</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags">&lt;?php</span><span class="hl-code"> </span><span class="hl-comment">//</span><span class="hl-comment"> content=&quot;text/plain; charset=utf-8&quot;</span><span class="hl-comment"></span><span class="hl-code">
 66</span><span class="hl-reserved">require_once</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">jpgraph/datamatrix/datamatrix.inc.php</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
 67 
 68</span><span class="hl-var">$data</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">The first datamatrix</span><span class="hl-quotes">'</span><span class="hl-code">;
 69</span><span class="hl-var">$encoder</span><span class="hl-code"> = </span><span class="hl-identifier">DatamatrixFactory</span><span class="hl-code">::</span><span class="hl-identifier">Create</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
 70</span><span class="hl-var">$encoder</span><span class="hl-code">-&gt;</span><span class="hl-identifier">SetEncoding</span><span class="hl-brackets">(</span><span class="hl-identifier">ENCODING_ASCII</span><span class="hl-brackets">)</span><span class="hl-code">;
 71</span><span class="hl-var">$backend</span><span class="hl-code"> = </span><span class="hl-identifier">DatamatrixBackendFactory</span><span class="hl-code">::</span><span class="hl-identifier">Create</span><span class="hl-brackets">(</span><span class="hl-var">$encoder</span><span class="hl-brackets">)</span><span class="hl-code">;
 72 
 73</span><span class="hl-comment">//</span><span class="hl-comment"> We increase the module width to 3 pixels</span><span class="hl-comment"></span><span class="hl-code">
 74</span><span class="hl-var">$backend</span><span class="hl-code">-&gt;</span><span class="hl-identifier">SetModuleWidth</span><span class="hl-brackets">(</span><span class="hl-number">3</span><span class="hl-brackets">)</span><span class="hl-code">;
 75 
 76</span><span class="hl-reserved">try</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
 77    </span><span class="hl-var">$backend</span><span class="hl-code">-&gt;</span><span class="hl-identifier">Stroke</span><span class="hl-brackets">(</span><span class="hl-var">$data</span><span class="hl-brackets">)</span><span class="hl-code">;
 78</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">catch</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">Exception</span><span class="hl-code"> </span><span class="hl-var">$e</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
 79    </span><span class="hl-reserved">echo</span><span class="hl-code"> </span><span class="hl-quotes">'</span><span class="hl-string">Datamatrix error: </span><span class="hl-quotes">'</span><span class="hl-code">.</span><span class="hl-var">$e</span><span class="hl-code">-&gt;</span><span class="hl-identifier">GetMessage</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-quotes">&quot;</span><span class="hl-special">\n</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
 80    </span><span class="hl-reserved">exit</span><span class="hl-brackets">(</span><span class="hl-number">1</span><span class="hl-brackets">)</span><span class="hl-code">;
 81</span><span class="hl-brackets">}</span><span class="hl-code">
 82</span><span class="hl-inlinetags">?&gt;</span></pre></td></tr></table></div></div></div><p><br class="example-break">  </p><div class="figure"><a name="fig.datamatrix_ex0"></a><p class="title"><b>Figure�25.7.�Datamatrix with basic error handling <code class="uri"><a class="uri" href="example_src/datamatrix_ex0.html" target="_top">(<code class="filename">datamatrix_ex0.php</code>)</a></code> </b></p><div class="figure-contents"><span class="inlinemediaobject"><img src="images/datamatrix_ex0.png" alt="Datamatrix with basic error handling (datamatrix_ex0.php)"></span></div></div><p><br class="figure-break">
 83            </p><p>In contrast to the PDF417 encodation process there is no option to select an level
 84                of error correction. The error correction level is automatically specified in the
 85                standard depending on the size of the barcode. As a final initial example the next
 86                script uses the backend method <code class="code">Backend::SetModuleWidth($aWidth)</code> to
 87                increase the width of one module.</p><p>
 88                </p><div class="example"><a name="example.datamatrix_ex1"></a><p class="title"><b>Example�25.3.�Datamatrix with modified module width (<code class="filename">datamatrix_ex1.php</code>) </b></p><div class="example-contents"><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
 892
 903
 914
 925
 936
 947
 958
 969
 9710
 9811
 9912
10013
10114
10215
10316
10417
10518
106</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags">&lt;?php</span><span class="hl-code"> </span><span class="hl-comment">//</span><span class="hl-comment"> content=&quot;text/plain; charset=utf-8&quot;</span><span class="hl-comment"></span><span class="hl-code">
107</span><span class="hl-reserved">require_once</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">jpgraph/datamatrix/datamatrix.inc.php</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
108 
109</span><span class="hl-var">$data</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">123456</span><span class="hl-quotes">'</span><span class="hl-code">;
110 
111</span><span class="hl-var">$encoder</span><span class="hl-code"> = </span><span class="hl-identifier">DatamatrixFactory</span><span class="hl-code">::</span><span class="hl-identifier">Create</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
112</span><span class="hl-var">$backend</span><span class="hl-code"> = </span><span class="hl-identifier">DatamatrixBackendFactory</span><span class="hl-code">::</span><span class="hl-identifier">Create</span><span class="hl-brackets">(</span><span class="hl-var">$encoder</span><span class="hl-brackets">)</span><span class="hl-code">;
113</span><span class="hl-var">$backend</span><span class="hl-code">-&gt;</span><span class="hl-identifier">SetModuleWidth</span><span class="hl-brackets">(</span><span class="hl-number">3</span><span class="hl-brackets">)</span><span class="hl-code">;
114 
115</span><span class="hl-comment">//</span><span class="hl-comment"> Create the barcode from the given data string and write to output file</span><span class="hl-comment"></span><span class="hl-code">
116</span><span class="hl-reserved">try</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
117    </span><span class="hl-var">$backend</span><span class="hl-code">-&gt;</span><span class="hl-identifier">Stroke</span><span class="hl-brackets">(</span><span class="hl-var">$data</span><span class="hl-brackets">)</span><span class="hl-code">;
118</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">catch</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">Exception</span><span class="hl-code"> </span><span class="hl-var">$e</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
119    </span><span class="hl-var">$errstr</span><span class="hl-code"> = </span><span class="hl-var">$e</span><span class="hl-code">-&gt;</span><span class="hl-identifier">GetMessage</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
120    </span><span class="hl-reserved">echo</span><span class="hl-code"> </span><span class="hl-quotes">&quot;</span><span class="hl-string">Datamatrix error message: </span><span class="hl-var">$errstr</span><span class="hl-special">\n</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
121</span><span class="hl-brackets">}</span><span class="hl-code">
122 
123</span><span class="hl-inlinetags">?&gt;</span></pre></td></tr></table></div></div></div><p><br class="example-break">  </p><div class="figure"><a name="fig.datamatrix_ex1"></a><p class="title"><b>Figure�25.8.�Datamatrix with modified module width <code class="uri"><a class="uri" href="example_src/datamatrix_ex1.html" target="_top">(<code class="filename">datamatrix_ex1.php</code>)</a></code> </b></p><div class="figure-contents"><span class="inlinemediaobject"><img src="images/datamatrix_ex1.png" alt="Datamatrix with modified module width (datamatrix_ex1.php)"></span></div></div><p><br class="figure-break">
124            </p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2614675"></a>25.2.2.�Error handling</h3></div></div></div><p>As in other parts of the library the Datamatrix module throws an exception when an
125                error occurs. As shown in the previous section an error image is the default if no
126                explicit <code class="code">try {} catch {}</code> statement is added to the script.</p><p>The script below shows how to catch the error, do some possible clean up and then
127                explicitly create and send back the error image.</p><p>
128                </p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
1292
1303
1314
1325
1336
1347
1358
1369
13710
138</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags">&lt;?php</span><span class="hl-code">
139...
140</span><span class="hl-reserved">try</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
141    </span><span class="hl-var">$backend</span><span class="hl-code">-&gt;</span><span class="hl-identifier">Stroke</span><span class="hl-brackets">(</span><span class="hl-var">$data</span><span class="hl-brackets">)</span><span class="hl-code">;
142</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">catch</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">Exception</span><span class="hl-code"> </span><span class="hl-var">$e</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
143    </span><span class="hl-identifier">doCleanup</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
144    </span><span class="hl-var">$errobj</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">DMErrObjectImg</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
145    </span><span class="hl-var">$errobj</span><span class="hl-code">-&gt;</span><span class="hl-identifier">Raise</span><span class="hl-brackets">(</span><span class="hl-var">$e</span><span class="hl-code">-&gt;</span><span class="hl-identifier">getMessage</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;    
146</span><span class="hl-brackets">}</span><span class="hl-code">
147</span><span class="hl-inlinetags">?&gt;</span></pre></td></tr></table></div><p>
148            </p><p>another variant of this would be to re-throw the exception after the cleanup has
149                been performed as the following example shows</p><p>
150                </p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
1512
1523
1534
1545
1556
1567
1578
1589
159</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags">&lt;?php</span><span class="hl-code">
160...
161</span><span class="hl-reserved">try</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
162    </span><span class="hl-var">$backend</span><span class="hl-code">-&gt;</span><span class="hl-identifier">Stroke</span><span class="hl-brackets">(</span><span class="hl-var">$data</span><span class="hl-brackets">)</span><span class="hl-code">;
163</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">catch</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">Exception</span><span class="hl-code"> </span><span class="hl-var">$e</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
164    </span><span class="hl-identifier">doCleanup</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
165    </span><span class="hl-reserved">throw</span><span class="hl-code"> </span><span class="hl-var">$e</span><span class="hl-code">;
166</span><span class="hl-brackets">}</span><span class="hl-code">
167</span><span class="hl-inlinetags">?&gt;</span></pre></td></tr></table></div><p>
168            </p><p>It is also possible to get hold of the internal error code that corresponds to
169                each error message by calling the PHP standard exception method
170                    <code class="code">Exception::getCode()</code> as the following example shows</p><p>
171                </p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
1722
1733
1744
1755
1766
1777
1788
1799
18010
181</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags">&lt;?php</span><span class="hl-code">
182...
183</span><span class="hl-reserved">try</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
184    </span><span class="hl-var">$backend</span><span class="hl-code">-&gt;</span><span class="hl-identifier">Stroke</span><span class="hl-brackets">(</span><span class="hl-var">$data</span><span class="hl-brackets">)</span><span class="hl-code">;
185</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">catch</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">Exception</span><span class="hl-code"> </span><span class="hl-var">$e</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
186    </span><span class="hl-var">$errstr</span><span class="hl-code"> = </span><span class="hl-var">$e</span><span class="hl-code">-&gt;</span><span class="hl-identifier">GetMessage</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
187    </span><span class="hl-var">$errcode</span><span class="hl-code"> = </span><span class="hl-var">$e</span><span class="hl-code">-&gt;</span><span class="hl-identifier">GetCode</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
188    </span><span class="hl-reserved">echo</span><span class="hl-code"> </span><span class="hl-quotes">&quot;</span><span class="hl-string">Datamatrix error (</span><span class="hl-var">$errcode</span><span class="hl-string">). Message: </span><span class="hl-var">$errstr</span><span class="hl-special">\n</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
189</span><span class="hl-brackets">}</span><span class="hl-code">
190</span><span class="hl-inlinetags">?&gt;</span></pre></td></tr></table></div><p>
191            </p><p><a class="xref" href="ch25s02.html#table.datamatrix-error-messages" title="Table�25.3.�Datamatrix error messages">Table�25.3</a> lists the errors that can be
192                thrown by the datamatrix module (the table deliberately excludes internal error
193                messages).</p><p>
194                </p><div class="table"><a name="table.datamatrix-error-messages"></a><p class="title"><b>Table�25.3.�Datamatrix error messages</b></p><div class="table-contents"><table summary="Datamatrix error messages" border="1"><colgroup><col><col></colgroup><thead><tr><th align="center">Error number</th><th align="center">Error messages</th></tr></thead><tbody><tr><td class="error-number" align="center">-1</td><td class="error-text">Data is too long to fit specified symbol
195                                    size</td></tr><tr><td class="error-number" align="center">-2</td><td class="error-text">The BASE256 data is too long to fit
196                                    available symbol size</td></tr><tr><td class="error-number" align="center">-3</td><td class="error-text">Data must have at least three characters
197                                    for C40 encodation</td></tr><tr><td class="error-number" align="center">-4</td><td class="error-text">Data must have at least three characters
198                                    for TEXT encodation</td></tr><tr><td class="error-number" align="center">-8</td><td class="error-text">The given data can not be encoded using X12
199                                    encodation.</td></tr><tr><td class="error-number" align="center">-9</td><td class="error-text">The "tilde" encoded data is not
200                                    valid.</td></tr><tr><td class="error-number" align="center">-10</td><td class="error-text">Data must have at least three characters
201                                    for X12 encodation</td></tr><tr><td class="error-number" align="center">-11</td><td class="error-text">Specified data can not be encoded with
202                                    datamatrix 000 140</td></tr><tr><td class="error-number" align="center">-12</td><td class="error-text">Can not create image</td></tr><tr><td class="error-number" align="center">-13</td><td class="error-text">Invalid color specification</td></tr><tr><td class="error-number" align="center">-15</td><td class="error-text">This PHP installation does not support the
203                                    chosen image encoding format</td></tr><tr><td class="error-number" align="center">-20</td><td class="error-text">The specification for shape of matrix is
204                                    out of bounds (0,29)</td></tr><tr><td class="error-number" align="center">-21</td><td class="error-text">Cannot open the data file specifying bit
205                                    placement for Datamatrix 200</td></tr><tr><td class="error-number" align="center">-22</td><td class="error-text">Datafile for bit placement is corrupt, crc
206                                    checks fails.</td></tr><tr><td class="error-number" align="center">-26</td><td class="error-text">Cannot open the data file specifying bit
207                                    placement for Datamatrix 140</td></tr><tr><td class="error-number" align="center">-30</td><td class="error-text">The symbol size specified for ECC140 type
208                                    Datamatrix is not valid</td></tr><tr><td class="error-number" align="center">-31</td><td class="error-text">Data is too long to fit into any available
209                                    matrix size for datamatrix 140</td></tr><tr><td class="error-number" align="center">-34</td><td class="error-text">Cannot open file %s for writing.</td></tr><tr><td class="error-number" align="center">-35</td><td class="error-text">Cannot write to file %s .</td></tr><tr><td class="error-number" align="center">-99</td><td class="error-text">EDIFACT encodation not implemented</td></tr><tr><td class="error-number" align="center">-100</td><td class="error-text">
210                                    <p>Datamatrix Error: HTTP headers have already been
211                                        sent.</p>
212                                    <p>Caused by output from file %s at line %d</p>
213                                    <p>Explanation: HTTP headers have already been sent back to
214                                        the browser indicating the data as text before the library
215                                        got a chance to send it's image HTTP header to this browser.
216                                        This makes it impossible for the Datamatrix library to send
217                                        back image data to the browser (since that would be
218                                        interpretated as text by the browser and show up as junk
219                                        text).</p>
220                                    <p>Most likely you have some text in your script before the
221                                        call to DatamatrixBackend::Stroke(). </p>
222                                    <p>If this texts gets sent back to the browser the browser
223                                        will assume that all data is plain text. Look for any text
224                                        (even spaces and newlines) that might have been sent back to
225                                        the browser.</p>
226                                    <p>For example it is a common mistake to leave a blank line
227                                        before the opening "&lt;?php"</p>
228                                </td></tr></tbody></table></div></div><p><br class="table-break">
229            </p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2615310"></a>25.2.3.�Encodation options</h3></div></div></div><p>The primary encodation option is to manually specify the shape of the Datamatrix
230                barcode. By default the symbol size will be chosen as the smallest possible. However
231                some application require that usage of a fixed size symbol shape. </p><p>The available shapes are listed in <a class="xref" href="ch25.html#table.datamatrix-ecc200-size" title="Table�25.2.�Maximum data capacity for the different symbol sizes in ECC-200 Data Matrix subset.">Table�25.2</a>. The wanted shape is specified when
232                the instance of the encoder is created or by a call to
233                    <code class="code">Datamatrix::SetSize()</code>. The shape is specified by a symbolic
234                constant that corresponds to each available shape in <a class="xref" href="ch25.html#table.datamatrix-ecc200-size" title="Table�25.2.�Maximum data capacity for the different symbol sizes in ECC-200 Data Matrix subset.">Table�25.2</a>. The symbol name is
235                    "<code class="code">DMAT_&lt;H&gt;x&lt;W&gt;</code>" where <code class="code">&lt;H&gt;</code> is the height (in
236                modules) and <code class="code">&lt;W&gt;</code> is the width (in modules). For example the
237                following line specifies a 24 module square symbol </p><p>
238                </p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
239</pre></td><td class="hl-main" valign="top"><pre><span class="hl-code">$encoder-&gt;SetSize(DMAT_24x24);</span></pre></td></tr></table></div><p>
240            </p><p>In addition to the sizes specified in <a class="xref" href="ch25.html#table.datamatrix-ecc200-size" title="Table�25.2.�Maximum data capacity for the different symbol sizes in ECC-200 Data Matrix subset.">Table�25.2</a> there is also an option to specify
241                automatic sizing (which is the default). The following script sets the sizing to be
242                automatic.</p><p>
243                </p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
244</pre></td><td class="hl-main" valign="top"><pre><span class="hl-code">$encoder-&gt;SetSize(DMAT_AUTO);</span></pre></td></tr></table></div><p>
245            </p><p>As was discussed in <a class="xref" href="ch25.html#sec.encodation-efficiency" title="25.1.5.� Encodation efficiency">Section�25.1.5</a> there are
246                several ways by which data can be encoded. By default the encoder will use an
247                optimal algorithm to create a combination of the available encodation schema to
248                create the smallest possible symbol. In some application it might however be stated
249                that a specific encodation method should be chosen. </p><p>The encoding method is a property of the encodation class and is set with a call
250                to </p><p>
251                </p><div class="itemizedlist"><ul type="disc"><li><p><code class="code">Datamatrix::SetEncoding($aEncoding=ENCODING_ASCII)</code>
252                        </p></li></ul></div><p>
253            </p><p>The encoding is specified as one of the symbolic constants shown in <a class="xref" href="ch25s02.html#table.datamatrix-symbikc-encoding" title="Table�25.4.�Datamatrix encodation schemas">Table�25.4</a></p><div class="table"><a name="table.datamatrix-symbikc-encoding"></a><p class="title"><b>Table�25.4.�Datamatrix encodation schemas</b></p><div class="table-contents"><table summary="Datamatrix encodation schemas" border="1"><colgroup><col><col><col></colgroup><thead><tr><th>Symbolic constant</th><th>Encoding schema</th><th>Character set</th></tr></thead><tbody><tr><td>ENCODING_AUTO</td><td>--</td><td>An optimal combination of all encodation schema</td></tr><tr><td>ENCODING_ASCII</td><td>ASCII</td><td>Most efficient for numeric data</td></tr><tr><td>ENCODING_C40</td><td>C40</td><td>Primarily upper-case alphanumeric</td></tr><tr><td>ENCODING_TEXT</td><td>Text</td><td>Primarily lower-case alphanumeric</td></tr><tr><td>ENCODING_X12</td><td>X12</td><td>ANSI X12 EDI data set</td></tr><tr><td>ENCODING_EDIFACT</td><td>EDIFACT</td><td>ASCII values 32-94</td></tr><tr><td>ENCODING_BASE256</td><td>Base 256</td><td>All byte values 0-255</td></tr></tbody></table></div></div><br class="table-break"><p>The following example sets the ASCII encodation schema</p><p>
254                </p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
255</pre></td><td class="hl-main" valign="top"><pre><span class="hl-code">$encoder-&gt;SetEncoding(ENCODING_ASCII);</span></pre></td></tr></table></div><p>
256            </p><p>The default automatic encoding is the same as</p><p>
257                </p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
258</pre></td><td class="hl-main" valign="top"><pre><span class="hl-code">$encoder-&gt;SetSize(ENCODING_AUTO);</span></pre></td></tr></table></div><p>
259            </p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2615653"></a>25.2.4.�Processing special input characters</h3></div></div></div><p>In order to allow the specification of special input data in normal ASCII
260                encodation for easy entering from a keyboard the library supports what is commonly
261                known as "tilde" processing. When this is enabled (it is disabled by default)
262                certain character sequences that begins with a tilde sign (i.e. "~") will be
263                translated to a special ASCII value that cannot normally be entered from a
264                keyboard.</p><p>When tilde mode is enabled with a call to the method </p><p>
265                </p><div class="itemizedlist"><ul type="disc"><li><p><code class="code">Datamatrix::SetTilde($aFlg=true)</code></p></li></ul></div><p>
266            </p><p><a class="xref" href="ch25s02.html#table.datamatrix-tilde-processing" title="Table�25.5.�Tilde processing translating">Table�25.5</a> lists the translations
267                that will then be enabled on the input data. Some of the translations are technical
268                and requires full understanding of Datamatrix standard to be used.</p><div class="table"><a name="table.datamatrix-tilde-processing"></a><p class="title"><b>Table�25.5.�Tilde processing translating</b></p><div class="table-contents"><table summary="Tilde processing translating" border="1"><colgroup><col><col></colgroup><thead><tr><th>Input sequence</th><th>Translation</th></tr></thead><tbody><tr><td>~X : where 'X' is a character in the range [@-Z]</td><td>Translates to ASCII value 0-25, i.e. ~@==0, ~A==1, ~B==2 ,
269                                ...</td></tr><tr><td>~1</td><td>Represents the character FNC1. Alternate Data Type Identifier.
270                                See Table 6: ISO 16022</td></tr><tr><td>~2nnmmfffggg</td><td>
271                                <p>Structured Append. This code is only allowed in the first
272                                    position of the data. Up to 16 ECC 200 symbols may be appended
273                                    in a structured format. The structured append is specified with
274                                    symbol index, total number of symbols and a file identification. </p>
275                                <p>The first two values "nn" is the position of this particular
276                                    symbol specified as two digits in the range of 01-16. </p>
277                                <p>The second two values "mm" is the total number of symbols in
278                                    this structured append in the range 02-16 specified as two
279                                    digits. </p>
280                                <p>The last two 3 digit codewords are the file identification
281                                    specified with digits and necessary leading 0:s to make it a
282                                    full three digits. For example. Assume that we want to encode
283                                    the 4:th symbol from a total of 9 symbols and with the file
284                                    identification "0,1". This would then be encoded as
285                                    "~20409000001". Note: Internally this is translated to three
286                                    codewords with the reserved starting codeword of ASCII
287                                    233.</p>
288                            </td></tr><tr><td>~5 and ~6</td><td>
289                                <p>The special 05 and 06 Macros. Can only be in the first
290                                    position and is used to encode industry standard headers in
291                                    certain structured formats. </p>
292                                <p>Macro 05 is translated by the barcode reader to : Symbol
293                                    prefix: chr(30) chr(05) chr(29) Symbol postfix: chr(30) chr(04)
294                                    Macro 06 is translated by the barcode reader to : Symbol prefix:
295                                    chr(30) chr(06) chr(29 Symbol postfix: chr(30) chr(04)</p>
296                            </td></tr><tr><td>~7nnnnnn</td><td>
297                                <p>Extended Channel Interpretation (ECI) nnnnnn. </p>
298                                <p>The ECI protocol allows the output data stream to have
299                                    interpretations different from that of the default character
300                                    set. See "<span class="italic">Extended Channel
301                                        Interpretation Assignments</span>" document (available
302                                    from ISO) for a list of channels and there meaning. </p>
303                                <p>The ECI is identified by a 6-digit number which is encoded
304                                    according to Table 10: in the ISO 16022:2000 specification. This
305                                    is the encodation that is used for the "~7" tilde specification
306                                    of the ECI protocol.</p>
307                            </td></tr><tr><td>~9</td><td>
308                                <p>Send the special control code (ASCII=234) so that the reader
309                                    will interpret the rest of the symbol as a bar code reader
310                                    programming instruction.</p>
311                            </td></tr><tr><td>~dnnn</td><td>
312                                <p>Character value as 3 digits, i.e. ~d142 means ASCII value
313                                    142</p>
314                            </td></tr></tbody></table></div></div><br class="table-break"></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2615957"></a>25.2.5.�Creating different backends</h3></div></div></div><p>In order to create the actual output one or more backends must be created. The
315                Datamatrix supports the following backends:</p><p>
316                </p><div class="itemizedlist"><ul type="disc"><li><p><code class="code">BACKEND_IMAGE</code>, Create an image backend. This is the
317                            default backend if no explicit backend is specified.</p></li><li><p><code class="code">BACKEND_PS</code>, Create a postscript backend. The text string
318                            that represents the postscript for the barcode is returned directly from
319                            the <code class="code">Backend::Stroke()</code></p></li><li><p><code class="code">BACKEND_EPS</code>, Create an encapsulated postscript
320                            backend</p></li><li><p><code class="code">BACKEND_ASCII</code>, This is a special backend which will
321                            generate an ASCII rendering of the datamatrix barcode. This is mostly
322                            practical for technical investigations regarding the technical structure
323                            of a Datamatrix barcode.</p></li></ul></div><p>
324            </p><p>The following code snippet shows two ways of creating a barcode image
325                backend.</p><p>
326                </p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
3272
328</pre></td><td class="hl-main" valign="top"><pre><span class="hl-code">// Create an image backend
329$backend = DatamatrixBackendFactory::Create($encoder);</span></pre></td></tr></table></div><p>
330            </p><p>
331                </p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
3322
333</pre></td><td class="hl-main" valign="top"><pre><span class="hl-code">// Create an image backend
334$backend = DatamatrixBackendFactory::Create($encoder, BACKEND_IMAGE);</span></pre></td></tr></table></div><p>
335            </p><p>The following code shows a complete script to generate a postscript output</p><p>
336                </p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
3372
3383
3394
3405
3416
3427
3438
3449
34510
34611
34712
34813
34914
35015
35116
35217
353</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags">&lt;?php</span><span class="hl-code">
354</span><span class="hl-reserved">require_once</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">jpgraph/datamatrix/datamatrix.inc.php</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
355</span><span class="hl-var">$data</span><span class="hl-code"> = </span><span class="hl-quotes">'</span><span class="hl-string">0123456789</span><span class="hl-quotes">'</span><span class="hl-code">;
356 
357</span><span class="hl-comment">//</span><span class="hl-comment"> Create and set parameters for the encoder</span><span class="hl-comment"></span><span class="hl-code">
358</span><span class="hl-var">$encoder</span><span class="hl-code"> = </span><span class="hl-identifier">DatamatrixFactory</span><span class="hl-code">::</span><span class="hl-identifier">Create</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
359 
360</span><span class="hl-comment">//</span><span class="hl-comment"> Create the image backend (default)</span><span class="hl-comment"></span><span class="hl-code">
361</span><span class="hl-var">$backend</span><span class="hl-code"> = </span><span class="hl-identifier">DatamatrixBackendFactory</span><span class="hl-code">::</span><span class="hl-identifier">Create</span><span class="hl-brackets">(</span><span class="hl-var">$encoder</span><span class="hl-code">, </span><span class="hl-identifier">BACKEND_PS</span><span class="hl-brackets">)</span><span class="hl-code">;
362</span><span class="hl-reserved">try</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
363    </span><span class="hl-var">$ps_txt</span><span class="hl-code"> = </span><span class="hl-var">$backend</span><span class="hl-code">-&gt;</span><span class="hl-identifier">Stroke</span><span class="hl-brackets">(</span><span class="hl-var">$data</span><span class="hl-brackets">)</span><span class="hl-code">;
364    </span><span class="hl-reserved">echo</span><span class="hl-code"> </span><span class="hl-quotes">'</span><span class="hl-string">&lt;pre&gt;</span><span class="hl-quotes">'</span><span class="hl-code">.</span><span class="hl-var">$ps_txt</span><span class="hl-code">.</span><span class="hl-quotes">'</span><span class="hl-string">&lt;/pre&gt;</span><span class="hl-quotes">'</span><span class="hl-code">;
365</span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">catch</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">Exception</span><span class="hl-code"> </span><span class="hl-var">$e</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
366    </span><span class="hl-var">$errstr</span><span class="hl-code"> = </span><span class="hl-var">$e</span><span class="hl-code">-&gt;</span><span class="hl-identifier">GetMessage</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
367    </span><span class="hl-reserved">echo</span><span class="hl-code"> </span><span class="hl-quotes">&quot;</span><span class="hl-string">Datamatrix error message: </span><span class="hl-var">$errstr</span><span class="hl-special">\n</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
368</span><span class="hl-brackets">}</span><span class="hl-code">
369</span><span class="hl-inlinetags">?&gt;</span></pre></td></tr></table></div><p>
370            </p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2616079"></a>25.2.6.�Generic backend methods</h3></div></div></div><p>The following methods are available to adjust the final look and feel of the
371                barcode</p><p>
372                </p><div class="orderedlist"><ol type="1"><li><p><code class="code">Backend::SetColor($aOne, $aZero,
373                            $aBackground='white')</code></p><p>the color of the 'black' and 'white' modules in the barcode and the
374                            quiet area around the barcode</p></li><li><p><code class="code">Backend::SetQuietZone($aSize)</code></p><p>the size of the "quiet zone" in pixels for the image backend and in
375                            points (1 pt = 1/72 inch) for the postscript backend</p></li><li><p><code class="code">Backend::SetModuleWidth($aWidth)</code></p><p>the module width specified in pixels for the image backend and in
376                            points (1 pt = 1/72 inch) for the postscript backend</p></li><li><p><code class="code">Backend::Stroke($aData, $aFileName='')</code></p><p>create the barcode and send it back the client or store it to a file
377                            if the second parameter is set. For postscript backends the postscript
378                            string is returned directly from the method. (See example above)</p></li></ol></div><p>
379            </p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2616167"></a>25.2.7.�Image backend methods</h3></div></div></div><p>For the image backend it is possible to adjust the image encoding format with the
380                following method</p><p>
381                </p><div class="orderedlist"><ol type="1"><li><p><code class="code">Backend::SetImgFormat($aFormat,$aQuality=75)</code></p><p>Specify image format. Possible values are </p><p>
382                            </p><div class="itemizedlist"><ul type="disc"><li><p><code class="code">'auto'</code></p></li><li><p><code class="code">'png'</code></p></li><li><p><code class="code">'jpeg'</code></p></li><li><p><code class="code">'gif'</code></p></li></ul></div><p>
383                        </p><p>For 'jpeg' format the quality parameter is a number in range [1-100]
384                            and specified how much compression / (Data loss) should be used. 100=no
385                            compression. Normal values are in the range [60-95]</p><p>The following example sets the image format to 'JPEG' with quality
386                            80.</p><p>
387                            </p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
388</pre></td><td class="hl-main" valign="top"><pre><span class="hl-code">$backend-&gt;SetImgFormat('jpeg', 80);</span></pre></td></tr></table></div><p>
389                        </p></li></ol></div><p>
390            </p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2616…

Large files files are truncated, but you can click here to view the full file