Generate code 128 bar codes
use Barcode::Code128; $code = new Barcode::Code128;
Perl 5.004, Carp, Exporter, \s-1GD\s0 (optional)
By default, nothing. However there are a number of constants that represent special characters used in the \s-1CODE\s0 128 symbology that you may wish to include. For example if you are using the \s-1EAN-128\s0 or \s-1UCC-128\s0 code, the string to encode begins with the \s-1FNC1\s0 character. To encode the \s-1EAN-128\s0 string \*(L"00 0 0012345 555555555 8\*(R", you would do the following:
use Barcode::Code128 'FNC1'; $code = new Barcode::Code128; $code->text(FNC1.'00000123455555555558');
To have this module export one or more of these characters, specify them on the \*(C`use\*(C' statement or use the special token ':all' instead to include all of them. Examples:
use Barcode::Code128 qw(FNC1 FNC2 FNC3 FNC4 Shift); use Barcode::Code128 qw(:all);
Here is the complete list of the exportable characters. They are assigned to high-order \s-1ASCII\s0 characters purely arbitrarily for the purposes of this module; the values used do not reflect any part of the \s-1CODE\s0 128 standard. Warning: Using the \*(C`CodeA\*(C', \*(C`CodeB\*(C', \*(C`CodeC\*(C', \*(C`StartA\*(C', \*(C`StartB\*(C', \*(C`StartC\*(C', and \*(C`Stop\*(C' codes may cause your barcodes to be invalid, and be rejected by scanners. They are inserted automatically as needed by this module.
CodeA 0xf4 CodeB 0xf5 CodeC 0xf6 FNC1 0xf7 FNC2 0xf8 FNC3 0xf9 FNC4 0xfa Shift 0xfb StartA 0xfc StartB 0xfd StartC 0xfe Stop 0xff
Barcode::Code128 generates bar codes using the \s-1CODE\s0 128 symbology. It can generate images in \s-1PNG\s0 or \s-1GIF\s0 format using the \s-1GD\s0 package, or it can generate a text string representing the barcode that you can render using some other technology if desired.
The intended use of this module is to create a web page with a bar code on it, which can then be printed out and faxed or mailed to someone who will scan the bar code. The application which spurred its creation was an expense report tool, where the employee submitting the report would print out the web page and staple the receipts to it, and the Accounts Payable clerk would scan the bar code to indicate that the receipts were received.
The default settings for this module produce a large image that can safely be FAXed several times and still scanned easily. If this requirement is not important you can generate smaller image using optional parameters, described below.
If you wish to generate images with this module you must also have the \s-1GD\s0 module (written by Lincoln Stein, and available from \s-1CPAN\s0) installed. Using the libgd library, \s-1GD\s0 can generate files in \s-1PNG\s0 (Portable Network Graphics) or \s-1GIF\s0 (Graphic Interchange Format) formats.
Starting with version 1.20, and ending with 2.0.28 (released July 21st, 2004), \s-1GD\s0 and the underlying libgd library could not generate \s-1GIF\s0 files due to patent issues, but any modern version of libgd (since 2004) can do \s-1GIF\s0 as the patent has expired. Most browsers have no trouble with \s-1PNG\s0 files.
In order to ensure you have a sufficiently modern installation of the \s-1GD\s0 module to do both \s-1GIF\s0 and \s-1PNG\s0 formats, we require version 2.18 of \s-1GD\s0 (which in turn requires libgd 2.0.28) or higher.
If the \s-1GD\s0 module is not present, you can still use the module, but you will not be able to use its functions for generating images. You can use the barcode() method to get a string of \*(L"#\*(R" and \*(L" \*(R" (hash and space) characters, and use your own image-generating routine with that as input.
To use the the \s-1GD\s0 module, you will need to install it along with this module. You can obtain it from the \s-1CPAN\s0 (Comprehensive Perl Archive Network) repository of your choice under the directory \*(C`authors/id/LDS\*(C'. Visit http://www.cpan.org/ for more information about \s-1CPAN\s0. The \s-1GD\s0 home page is: http://stein.cshl.org/WWW/software/GD/GD.html
Usage: $object = new Barcode::Code128 Creates a new barcode object.
Sets or retreives various options. If called with only one parameter, retrieves the value for that parameter. If called with more than one parameter, treats the parameters as name/value pairs and sets those option values accordingly. If called with no parameters, returns a hash consisting of the values of all the options (hash ref in scalar context). When an option has not been set, its default value is returned. You can also set or retrieve any of these options by using it as a method name. For example, to set the value of the padding option, you can use either of these: $barcode->padding(10); $barcode->option("padding", 10); The valid options, and the default value and meaning of each, are: width undef Width of the image (*) height undef Height of the image (*) border 2 Size of the black border around the barcode scale 2 How many pixels for the smallest barcode stripe font "large" Font (**) for the text at the bottom show_text 1 True/False: display the text at the bottom? font_margin 2 Pixels above, below, and to left of the text font_align "left" Align the text ("left", "right", or "center") transparent_text 1/0(***) True/False: use transparent background for text? top_margin 0 No. of pixels above the barcode bottom_margin 0 No. of pixels below the barcode (& text) left_margin 0 No. of pixels to the left of the barcode right_margin 0 No. of pixels to the right of the barcode padding 20 Size of whitespace before & after barcode * Width and height are the default values for the $x and $y arguments to the png, gif, or gd_image method (q.v.) ** Font may be one of the following: \*(L"giant\*(R", \*(L"large\*(R", \*(L"medium\*(R", \*(L"small\*(R", or \*(L"tiny\*(R". Or, it may be any valid \s-1GD\s0 font name, such as \*(L"gdMediumFont\*(R". *** The \*(L"transparent_text\*(R" option is \*(L"1\*(R" (true) by default for \s-1GIF\s0 output, but \*(L"0\*(R" (false) for \s-1PNG\s0. This is because \s-1PNG\s0 transparency is not supported well by many viewing software The background color is grey (#CCCCCC) when not transparent.
Usage: $object->png($text) $object->png($text, $x, $y) $object->png($text, { options... })
$object->gif($text) $object->gif($text, $x, $y) $object->gif($text, { options... })
$object->gd_image($text) $object->gd_image($text, $x, $y) $object->gd_image($text, { options... }) These methods generate an image using the \s-1GD\s0 module. The gd_image() method returns a \s-1GD\s0 object, which is useful if you want to do additional processing to it using the \s-1GD\s0 object methods. The other two create actual images. \s-1NOTE:\s0 \s-1GIF\s0 files require an old version of \s-1GD\s0, and so you probably are not able to create them - see below. The gif() and png() methods are wrappers around gd_image() that create the \s-1GD\s0 object and then run the corresponding \s-1GD\s0 method to create output that can be displayed or saved to a file. Note that only one of these two methods will work, depending on which version of \s-1GD\s0 you have - see below. The return value from gif() or png() is a binary file, so if you are working on an operating system (e.g. Microsoft Windows) that makes a distinction between text and binary files be sure to call binmode(\s-1FILEHANDLE\s0) before writing the image to it, or the file may get corrupted. Example: open(PNG, ">code128.png") or die "Can't write code128.png: $!\n"; binmode(PNG); print PNG $object->png("CODE 128"); close(PNG); If you have \s-1GD\s0 version 1.20 or newer, the \s-1PNG\s0 file format is the only allowed option. Conversely if you have \s-1GD\s0 version prior to 1.20, then the \s-1GIF\s0 format is the only option. Check the $object->image_format() method to find out which you have (q.v.). Note: All of the arguments to this function are optional. If you have previously specified $text to the \*(C`barcode()\*(C', \*(C`encode()\*(C', or \*(C`text()\*(C' methods, you do not need to specify it again. The $x and $y variables specify the size of the barcode within the image in pixels. If size(s) are not specified, they will be set to the minimum size, which is the length of the barcode plus 40 pixels horizontally, and 15% of the length of the barcode vertically. See also the $object->width() and $object->height() methods for another way of specifying this. If instead of specifying $x and $y, you pass a reference to a hash of name/value pairs, these will be used as the options, overriding anything set using the $object->option() (or width/height) method (q.v.). However, this will not set the options so any future barcodes using the same object will revert to the option list of the object. If you want to set the options permanently use the option, width, and/or height methods instead.
Usage: $object->barcode($text) Computes the bar code for the specified text. The result will be a string of '#' and space characters representing the dark and light bands of the bar code. You can use this if you have an alternate printing system besides using \s-1GD\s0 to create the images. Note: The $text parameter is optional. If you have previously specified $text to the \*(C`encode()\*(C' or \*(C`text()\*(C' methods, you do not need to specify it again.
The rest of the methods defined here are only for internal use, or if you really know what you are doing. Some of them may be useful to authors of classes that inherit from this one, or may be overridden by subclasses. If you just want to use this module to generate bar codes, you can stop reading here.
Usage: $object->encode $object->encode($text) $object->encode($text, $preferred_code) Do the encoding. If $text is supplied, will automatically call the text() method to set that as the text value first. If $preferred_code is supplied, will try that code first. Otherwise, the codes will be tried in the following manner: 1. If it is possible to use Code C for any of the text, use that for as much of it as possible. 2. Check how many characters would be converted using codes A or B, and use that code to convert them. If the amount is equal, code A is used. 3. Repeat steps 1 and 2 until the text string has been completely encoded.
Usage: $object->text($text) $text = $object->text Set or retrieve the text for this barcode. This will be called automatically by encode() or barcode() so typically this will not be used directly by the user.
Usage: $object->start($code) If the code (see code()) is already defined, then adds the CodeA, CodeB, or CodeC character as appropriate to the encoded message inside the object. Typically for internal use only.
Usage: $object->stop() Computes the check character and appends it along with the Stop character, to the encoded string. Typically for internal use only.
Usage: $object->code($code) $code = $object->code Set or retrieve the code for this barcode. $code may be 'A', 'B', or 'C'. Typically for internal use only. Not particularly meaningful unless called during the middle of encoding.
None.
The specified option is not valid for the module. $class should be \*(L"Barcode::Code128\*(R" but if it has been inherited into another module, that module will show instead. $opt is the attempted option.
To call the \*(C`gd_image()\*(C', \*(C`png()\*(C', or \*(C`gif()\*(C' methods, the \s-1GD\s0 module must be present. This module is used to create the actual image. Without it, you can only use the \*(C`barcode()\*(C' method.
The scale factor for the \*(C`gd_image()\*(C', \*(C`png()\*(C', or \*(C`gif()\*(C' methods must be a positive integer.
The border option cannot be a fractional or negative number. The specified font is not valid. Note that this is tested using \s-1GD-\s0>can(), and so any subroutine in \s-1GD\s0.pm will pass this test - but only the fonts will actually work. See the \s-1GD\s0 module documentation for more. You have specified an image width that does not allow enough space for the bar code to be displayed. The minimum allowable is the size of the bar code itself plus 40 pixels. If in doubt, just omit the width value when calling \*(C`gd_image()\*(C', \*(C`png()\*(C', or \*(C`gif()\*(C' and it will use the minimum. You have specified an image height that does not allow enough space for the bar code to be displayed. The minimum allowable is 15% of the width of the bar code. If in doubt, just omit the height value when calling \*(C`gd_image()\*(C', \*(C`png()\*(C', or \*(C`gif()\*(C' and it will use the minimum. An error occurred when initializing a GD::Image object for the specified size. Perhaps $x and $y are too large for memory?
These errors indicate that the \s-1GD\s0 module, or the correct version of the \s-1GD\s0 module for this method, was not present. You need to install \s-1GD\s0 version 1.20 or greater to create \s-1PNG\s0 files, or a version of \s-1GD\s0 less than 1.20 to create \s-1GIF\s0 files.
This message from \*(C`barcode()\*(C' typically means that there was no text message supplied either during the current method call or in a previous method call on the same object. This error occurs when you are trying to create a barcode by calling one of \*(C`gd_image()\*(C', \*(C`png()\*(C', \*(C`gif()\*(C', or \*(C`barcode()\*(C' without having specified the text to be encoded.
This message from \*(C`encode()\*(C' typically means that there was no text message supplied either during the current method call or in a previous method call on the same object.
This error means \*(C`encode()\*(C' was called with the $preferred_code optional parameter but it was not one of ``A'', ``B'', or ``C''.
This is a serious error in \*(C`encode()\*(C' that indicates a serious problem attempting to encode the requested message. This means that an infinite loop was generated. If you get this error please contact the author.
Part or all of the message could not be encoded. This may mean that the message contained characters not encodable in the \s-1CODE\s0 128 character set, such as a character with an \s-1ASCII\s0 value higher than 127 (except the special control characters defined in this module).
This is a serious error in \*(C`start()\*(C' that indicates a serious problem occurred when switching between the codes (A, B, or C) of \s-1CODE\s0 128. If you get this error please contact the author.
This is a serious error in \*(C`start()\*(C' that indicates a serious problem occurred when starting encoding in one of the codes (A, B, or C) of \s-1CODE\s0 128. If you get this error please contact the author.
This is a serious error in \*(C`code()\*(C' that indicates an invalid argument was supplied. Only the codes (A, B, or C) of \s-1CODE\s0 128 may be supplied here. If you get this error please contact the author.
At least some Web browsers do not seem to handle \s-1PNG\s0 files with transparent backgrounds correctly. As a result, the default for \s-1PNG\s0 is to generate barcodes without transparent backgrounds - the background is grey instead.
William R. Ward, [email protected]
perl\|(1), \s-1GD\s0