Barcode::Code128
Barcode::Code128 - Generate CODE 128 bar codes
use Barcode::Code128; $code = new Barcode::Code128;
Perl 5.004, Carp, Exporter, GD (optional)
By default, nothing. However there are a number of constants that represent special characters used in the CODE 128 symbology that you may wish to include. For example if you are using the EAN-128 or UCC-128 code, the string to encode begins with the FNC1 character. To encode the EAN-128 string "00 0 0012345 555555555 8", 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 use 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 ASCII characters purely arbitrarily for the
purposes of this module; the values used do not reflect any part of
the CODE 128 standard. Warning: Using the CodeA, CodeB,
CodeC, StartA, StartB, StartC, and Stop 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 CODE 128 symbology. It can generate images in PNG or GIF format using the GD 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 GD.pm module (written by Lincoln Stein, and available from CPAN) installed. Version 1.20 or higher of GD generates a PNG file, due to issues with the GIF patent. If you want to create a GIF, you must use version 1.19 or earlier of GD. However, most browsers have no trouble with PNG files.
If the GD 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 "#" and " " (hash and space) characters, and use your own image-generating routine with that as input.
To use the the GD module, you will need to install it along with this
module. You can obtain it from the CPAN (Comprehensive Perl Archive
Network) repository of your choice under the directory
authors/id/LDS. Visit http://www.cpan.org/ for more information
about CPAN. The GD 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: "giant", "large", "medium", "small", or "tiny". Or, it may be any valid GD font name, such as "gdMediumFont".
*** The "transparent_text" option is "1" (true) by default for GIF output, but "0" (false) for PNG. This is because PNG 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) # for old versions of GD only
$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 GD module. The gd_image() method returns a GD object, which is useful if you want to do additional processing to it using the GD object methods. The other two create actual images. NOTE: GIF files require an old version of GD, 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 GD object and then run the corresponding GD 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 GD 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(FILEHANDLE) 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 GD version 1.20 or newer, the PNG file format is the only allowed option. Conversely if you have GD version prior to 1.20, then the GIF 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 barcode(), encode(), or
text() 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 GD to create the images.
Note: The $text parameter is optional. If you have previously
specified $text to the encode() or text() 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.
$class should be
"Barcode::Code128" but if it has been inherited into another module,
that module will show instead. $opt is the attempted option.
gd_image(), png(), or gif() methods, the GD
module must be present. This module is used to create the actual
image. Without it, you can only use the barcode() method.
gd_image(), png(), or gif() methods
must be a positive integer.
gd_image(), png(), or gif() and it will
use the minimum.
gd_image(), png(), or gif() and it will use the
minimum.
$x and $y are too large for memory?
barcode() 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 gd_image(),
png(), gif(), or barcode() without having specified the text
to be encoded.
encode() 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.
encode() was called with the $preferred_code
optional parameter but it was not one of ``A'', ``B'', or ``C''.
encode() 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.
start() that indicates a serious problem
occurred when switching between the codes (A, B, or C) of CODE 128.
If you get this error please contact the author.
start() that indicates a serious problem
occurred when starting encoding in one of the codes (A, B, or C) of
CODE 128. If you get this error please contact the author.
code() that indicates an invalid
argument was supplied. Only the codes (A, B, or C) of CODE 128 may be
supplied here. If you get this error please contact the author.
At least some Web browsers do not seem to handle PNG files with transparent backgrounds correctly. As a result, the default for PNG is to generate barcodes without transparent backgrounds - the background is grey instead.
William R. Ward, wrw@bayview.com
perl(1), GD