NAME

Barcode::Code128 - Generate CODE 128 bar codes

SYNOPSIS

use Barcode::Code128;

$code = new Barcode::Code128;

REQUIRES

Perl 5.004, Carp, Exporter, GD (optional)

EXPORTS

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

DESCRIPTION

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 module (written by Lincoln Stein, and available from CPAN) installed. Using the libgd library, GD can generate files in PNG (Portable Network Graphics) or GIF (Graphic Interchange Format) formats.

Starting with version 1.20, and ending with 2.0.28 (released July 21st, 2004), GD and the underlying libgd library could not generate GIF files due to patent issues, but any modern version of libgd (since 2004) can do GIF as the patent has expired. Most browsers have no trouble with PNG files.

In order to ensure you have a sufficiently modern installation of the GD module to do both GIF and PNG formats, we require version 2.18 of GD (which in turn requires libgd 2.0.28) or higher.

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

METHODS

new

Usage:

$object = new Barcode::Code128

Creates a new barcode object.

option

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.

gif
png
gd_image

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 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.

barcode

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.

Housekeeping Functions

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.

encode

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.

text

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.

start

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.

stop

Usage:

$object->stop()

Computes the check character and appends it along with the Stop character, to the encoded string. Typically for internal use only.

code

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.

CLASS VARIABLES

None.

DIAGNOSTICS

Unrecognized option ($opt) for $class

The specified option is not valid for the module. $class should be "Barcode::Code128" but if it has been inherited into another module, that module will show instead. $opt is the attempted option.

The gd_image() method of Barcode::Code128 requires the GD module

To call the 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.

Scale must be a positive integer

The scale factor for the gd_image(), png(), or gif() methods must be a positive integer.

Border ($border) must be a positive integer or zero

The border option cannot be a fractional or negative number.

Invalid font $font

The specified font is not valid. Note that this is tested using GD->can(), and so any subroutine in GD.pm will pass this test - but only the fonts will actually work. See the GD module documentation for more.

Image width $x is too small for bar code

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 gd_image(), png(), or gif() and it will use the minimum.

Image height $y is too small for bar code

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 gd_image(), png(), or gif() and it will use the minimum.

Unable to create $x x $y image

An error occurred when initializing a GD::Image object for the specified size. Perhaps $x and $y are too large for memory?

The gif() method of Barcode::Code128 requires the GD module
The gif() method of Barcode::Code128 requires version less than 1.20 of GD
The png() method of Barcode::Code128 requires the GD module
The png() method of Barcode::Code128 requires at least version 1.20 of GD

These errors indicate that the GD module, or the correct version of the GD module for this method, was not present. You need to install GD version 1.20 or greater to create PNG files, or a version of GD less than 1.20 to create GIF files.

No encoded text found

This message from 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.

No text defined

This message from 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.

Invalid preferred code ``$preferred_code''

This error means encode() was called with the $preferred_code optional parameter but it was not one of ``A'', ``B'', or ``C''.

Sanity Check Overflow

This is a serious error in 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.

Unable to find encoding for ``$text''

Part or all of the message could not be encoded. This may mean that the message contained characters not encodable in the CODE 128 character set, such as a character with an ASCII value higher than 127 (except the special control characters defined in this module).

Unable to switch from ``$old_code'' to ``$new_code''

This is a serious error in 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.

Unable to start with ``$new_code''

This is a serious error in 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.

Unknown code ``$new_code'' (should be A, B, or C)

This is a serious error in 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.

BUGS

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.

AUTHOR

William R. Ward, wrw@bayview.com

SEE ALSO

perl(1), GD