1Barcode::Code128(3) User Contributed Perl Documentation Barcode::Code128(3)
2
3
4
6 Barcode::Code128 - Generate CODE 128 bar codes
7
9 use Barcode::Code128;
10
11 $code = new Barcode::Code128;
12
14 Perl 5.004, Carp, Exporter, GD (optional)
15
17 By default, nothing. However there are a number of constants that
18 represent special characters used in the CODE 128 symbology that you
19 may wish to include. For example if you are using the EAN-128 or
20 UCC-128 code, the string to encode begins with the FNC1 character. To
21 encode the EAN-128 string "00 0 0012345 555555555 8", you would do the
22 following:
23
24 use Barcode::Code128 'FNC1';
25 $code = new Barcode::Code128;
26 $code->text(FNC1.'00000123455555555558');
27
28 To have this module export one or more of these characters, specify
29 them on the "use" statement or use the special token ':all' instead to
30 include all of them. Examples:
31
32 use Barcode::Code128 qw(FNC1 FNC2 FNC3 FNC4 Shift);
33 use Barcode::Code128 qw(:all);
34
35 Here is the complete list of the exportable characters. They are
36 assigned to high-order ASCII characters purely arbitrarily for the
37 purposes of this module; the values used do not reflect any part of the
38 CODE 128 standard. Warning: Using the "CodeA", "CodeB", "CodeC",
39 "StartA", "StartB", "StartC", and "Stop" codes may cause your barcodes
40 to be invalid, and be rejected by scanners. They are inserted
41 automatically as needed by this module.
42
43 CodeA 0xf4 CodeB 0xf5 CodeC 0xf6
44 FNC1 0xf7 FNC2 0xf8 FNC3 0xf9
45 FNC4 0xfa Shift 0xfb StartA 0xfc
46 StartB 0xfd StartC 0xfe Stop 0xff
47
49 Barcode::Code128 generates bar codes using the CODE 128 symbology. It
50 can generate images in PNG or GIF format using the GD package, or it
51 can generate a text string representing the barcode that you can render
52 using some other technology if desired.
53
54 The intended use of this module is to create a web page with a bar code
55 on it, which can then be printed out and faxed or mailed to someone who
56 will scan the bar code. The application which spurred its creation was
57 an expense report tool, where the employee submitting the report would
58 print out the web page and staple the receipts to it, and the Accounts
59 Payable clerk would scan the bar code to indicate that the receipts
60 were received.
61
62 The default settings for this module produce a large image that can
63 safely be FAXed several times and still scanned easily. If this
64 requirement is not important you can generate smaller image using
65 optional parameters, described below.
66
67 If you wish to generate images with this module you must also have the
68 GD module (written by Lincoln Stein, and available from CPAN)
69 installed. Using the libgd library, GD can generate files in PNG
70 (Portable Network Graphics) or GIF (Graphic Interchange Format)
71 formats.
72
73 Starting with version 1.20, and ending with 2.0.28 (released July 21st,
74 2004), GD and the underlying libgd library could not generate GIF files
75 due to patent issues, but any modern version of libgd (since 2004) can
76 do GIF as the patent has expired. Most browsers have no trouble with
77 PNG files.
78
79 In order to ensure you have a sufficiently modern installation of the
80 GD module to do both GIF and PNG formats, we require version 2.18 of GD
81 (which in turn requires libgd 2.0.28) or higher.
82
83 If the GD module is not present, you can still use the module, but you
84 will not be able to use its functions for generating images. You can
85 use the barcode() method to get a string of "#" and " " (hash and
86 space) characters, and use your own image-generating routine with that
87 as input.
88
89 To use the the GD module, you will need to install it along with this
90 module. You can obtain it from the CPAN (Comprehensive Perl Archive
91 Network) repository of your choice under the directory
92 "authors/id/LDS". Visit http://www.cpan.org/ for more information
93 about CPAN. The GD home page is:
94 http://stein.cshl.org/WWW/software/GD/GD.html
95
97 new Usage:
98
99 $object = new Barcode::Code128
100
101 Creates a new barcode object.
102
103 option
104 Sets or retreives various options. If called with only one
105 parameter, retrieves the value for that parameter. If called with
106 more than one parameter, treats the parameters as name/value pairs
107 and sets those option values accordingly. If called with no
108 parameters, returns a hash consisting of the values of all the
109 options (hash ref in scalar context). When an option has not been
110 set, its default value is returned.
111
112 You can also set or retrieve any of these options by using it as a
113 method name. For example, to set the value of the padding option,
114 you can use either of these:
115
116 $barcode->padding(10);
117 $barcode->option("padding", 10);
118
119 The valid options, and the default value and meaning of each, are:
120
121 width undef Width of the image (*)
122 height undef Height of the image (*)
123 border 2 Size of the black border around the barcode
124 scale 2 How many pixels for the smallest barcode stripe
125 font "large" Font (**) for the text at the bottom
126 show_text 1 True/False: display the text at the bottom?
127 font_margin 2 Pixels above, below, and to left of the text
128 font_align "left" Align the text ("left", "right", or "center")
129 transparent_text 1/0(***) True/False: use transparent background for text?
130 top_margin 0 No. of pixels above the barcode
131 bottom_margin 0 No. of pixels below the barcode (& text)
132 left_margin 0 No. of pixels to the left of the barcode
133 right_margin 0 No. of pixels to the right of the barcode
134 padding 20 Size of whitespace before & after barcode
135
136 * Width and height are the default values for the $x and $y
137 arguments to the png, gif, or gd_image method (q.v.)
138
139 ** Font may be one of the following: "giant", "large", "medium",
140 "small", or "tiny". Or, it may be any valid GD font name, such as
141 "gdMediumFont".
142
143 *** The "transparent_text" option is "1" (true) by default for GIF
144 output, but "0" (false) for PNG. This is because PNG transparency
145 is not supported well by many viewing software The background color
146 is grey (#CCCCCC) when not transparent.
147
148 gif
149 png
150 gd_image
151 Usage:
152
153 $object->png($text)
154 $object->png($text, $x, $y)
155 $object->png($text, { options... })
156
157 $object->gif($text)
158 $object->gif($text, $x, $y)
159 $object->gif($text, { options... })
160
161 $object->gd_image($text)
162 $object->gd_image($text, $x, $y)
163 $object->gd_image($text, { options... })
164
165 These methods generate an image using the GD module. The
166 gd_image() method returns a GD object, which is useful if you want
167 to do additional processing to it using the GD object methods. The
168 other two create actual images. NOTE: GIF files require an old
169 version of GD, and so you probably are not able to create them -
170 see below.
171
172 The gif() and png() methods are wrappers around gd_image() that
173 create the GD object and then run the corresponding GD method to
174 create output that can be displayed or saved to a file. Note that
175 only one of these two methods will work, depending on which version
176 of GD you have - see below. The return value from gif() or png()
177 is a binary file, so if you are working on an operating system
178 (e.g. Microsoft Windows) that makes a distinction between text and
179 binary files be sure to call binmode(FILEHANDLE) before writing the
180 image to it, or the file may get corrupted. Example:
181
182 open(PNG, ">code128.png") or die "Can't write code128.png: $!\n";
183 binmode(PNG);
184 print PNG $object->png("CODE 128");
185 close(PNG);
186
187 If you have GD version 1.20 or newer, the PNG file format is the
188 only allowed option. Conversely if you have GD version prior to
189 1.20, then the GIF format is the only option. Check the
190 $object->image_format() method to find out which you have (q.v.).
191
192 Note: All of the arguments to this function are optional. If you
193 have previously specified $text to the "barcode()", "encode()", or
194 "text()" methods, you do not need to specify it again. The $x and
195 $y variables specify the size of the barcode within the image in
196 pixels. If size(s) are not specified, they will be set to the
197 minimum size, which is the length of the barcode plus 40 pixels
198 horizontally, and 15% of the length of the barcode vertically. See
199 also the $object->width() and $object->height() methods for another
200 way of specifying this.
201
202 If instead of specifying $x and $y, you pass a reference to a hash
203 of name/value pairs, these will be used as the options, overriding
204 anything set using the $object->option() (or width/height) method
205 (q.v.). However, this will not set the options so any future
206 barcodes using the same object will revert to the option list of
207 the object. If you want to set the options permanently use the
208 option, width, and/or height methods instead.
209
210 barcode
211 Usage:
212
213 $object->barcode($text)
214
215 Computes the bar code for the specified text. The result will be a
216 string of '#' and space characters representing the dark and light
217 bands of the bar code. You can use this if you have an alternate
218 printing system besides using GD to create the images.
219
220 Note: The $text parameter is optional. If you have previously
221 specified $text to the "encode()" or "text()" methods, you do not
222 need to specify it again.
223
224 Housekeeping Functions
225 The rest of the methods defined here are only for internal use, or if
226 you really know what you are doing. Some of them may be useful to
227 authors of classes that inherit from this one, or may be overridden by
228 subclasses. If you just want to use this module to generate bar codes,
229 you can stop reading here.
230
231 encode
232 Usage:
233
234 $object->encode
235 $object->encode($text)
236 $object->encode($text, $preferred_code)
237
238 Do the encoding. If $text is supplied, will automatically call the
239 text() method to set that as the text value first. If
240 $preferred_code is supplied, will try that code first. Otherwise,
241 the codes will be tried in the following manner:
242
243 1. If it is possible to use Code C for any of the text, use that
244 for as much of it as possible.
245
246 2. Check how many characters would be converted using codes A or B,
247 and use that code to convert them. If the amount is equal, code A
248 is used.
249
250 3. Repeat steps 1 and 2 until the text string has been completely
251 encoded.
252
253 text
254 Usage:
255
256 $object->text($text)
257 $text = $object->text
258
259 Set or retrieve the text for this barcode. This will be called
260 automatically by encode() or barcode() so typically this will not
261 be used directly by the user.
262
263 start
264 Usage:
265
266 $object->start($code)
267
268 If the code (see code()) is already defined, then adds the CodeA,
269 CodeB, or CodeC character as appropriate to the encoded message
270 inside the object. Typically for internal use only.
271
272 stop
273 Usage:
274
275 $object->stop()
276
277 Computes the check character and appends it along with the Stop
278 character, to the encoded string. Typically for internal use only.
279
280 code
281 Usage:
282
283 $object->code($code)
284 $code = $object->code
285
286 Set or retrieve the code for this barcode. $code may be 'A', 'B',
287 or 'C'. Typically for internal use only. Not particularly
288 meaningful unless called during the middle of encoding.
289
291 None.
292
294 Unrecognized option ($opt) for $class
295 The specified option is not valid for the module. $class should be
296 "Barcode::Code128" but if it has been inherited into another
297 module, that module will show instead. $opt is the attempted
298 option.
299
300 The gd_image() method of Barcode::Code128 requires the GD module
301 To call the "gd_image()", "png()", or "gif()" methods, the GD
302 module must be present. This module is used to create the actual
303 image. Without it, you can only use the "barcode()" method.
304
305 Scale must be a positive integer
306 The scale factor for the "gd_image()", "png()", or "gif()" methods
307 must be a positive integer.
308
309 Border ($border) must be a positive integer or zero
310 The border option cannot be a fractional or negative number.
311
312 Invalid font $font
313 The specified font is not valid. Note that this is tested using
314 GD->can(), and so any subroutine in GD.pm will pass this test - but
315 only the fonts will actually work. See the GD module documentation
316 for more.
317
318 Image width $x is too small for bar code
319 You have specified an image width that does not allow enough space
320 for the bar code to be displayed. The minimum allowable is the
321 size of the bar code itself plus 40 pixels. If in doubt, just omit
322 the width value when calling "gd_image()", "png()", or "gif()" and
323 it will use the minimum.
324
325 Image height $y is too small for bar code
326 You have specified an image height that does not allow enough space
327 for the bar code to be displayed. The minimum allowable is 15% of
328 the width of the bar code. If in doubt, just omit the height value
329 when calling "gd_image()", "png()", or "gif()" and it will use the
330 minimum.
331
332 Unable to create $x x $y image
333 An error occurred when initializing a GD::Image object for the
334 specified size. Perhaps $x and $y are too large for memory?
335
336 The gif() method of Barcode::Code128 requires the GD module
337 The gif() method of Barcode::Code128 requires version less than 1.20 of
338 GD
339 The png() method of Barcode::Code128 requires the GD module
340 The png() method of Barcode::Code128 requires at least version 1.20 of
341 GD
342 These errors indicate that the GD module, or the correct version of
343 the GD module for this method, was not present. You need to
344 install GD version 1.20 or greater to create PNG files, or a
345 version of GD less than 1.20 to create GIF files.
346
347 No encoded text found
348 This message from "barcode()" typically means that there was no
349 text message supplied either during the current method call or in a
350 previous method call on the same object. This error occurs when
351 you are trying to create a barcode by calling one of "gd_image()",
352 "png()", "gif()", or "barcode()" without having specified the text
353 to be encoded.
354
355 No text defined
356 This message from "encode()" typically means that there was no text
357 message supplied either during the current method call or in a
358 previous method call on the same object.
359
360 Invalid preferred code ``$preferred_code''
361 This error means "encode()" was called with the $preferred_code
362 optional parameter but it was not one of ``A'', ``B'', or ``C''.
363
364 Sanity Check Overflow
365 This is a serious error in "encode()" that indicates a serious
366 problem attempting to encode the requested message. This means
367 that an infinite loop was generated. If you get this error please
368 contact the author.
369
370 Unable to find encoding for ``$text''
371 Part or all of the message could not be encoded. This may mean
372 that the message contained characters not encodable in the CODE 128
373 character set, such as a character with an ASCII value higher than
374 127 (except the special control characters defined in this module).
375
376 Unable to switch from ``$old_code'' to ``$new_code''
377 This is a serious error in "start()" that indicates a serious
378 problem occurred when switching between the codes (A, B, or C) of
379 CODE 128. If you get this error please contact the author.
380
381 Unable to start with ``$new_code''
382 This is a serious error in "start()" that indicates a serious
383 problem occurred when starting encoding in one of the codes (A, B,
384 or C) of CODE 128. If you get this error please contact the
385 author.
386
387 Unknown code ``$new_code'' (should be A, B, or C)
388 This is a serious error in "code()" that indicates an invalid
389 argument was supplied. Only the codes (A, B, or C) of CODE 128 may
390 be supplied here. If you get this error please contact the author.
391
393 At least some Web browsers do not seem to handle PNG files with
394 transparent backgrounds correctly. As a result, the default for PNG is
395 to generate barcodes without transparent backgrounds - the background
396 is grey instead.
397
399 William R. Ward, wrw@bayview.com
400
402 perl(1), GD
403
404
405
406perl v5.30.1 2020-01-29 Barcode::Code128(3)