Barcode-Code128

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

1.11  Fri Oct 29 22:26:12 1999
        - Fix bug in test script

2.00  Mon May 28 14:54:21 2001
        - Changes suggested by Lance Held <lheld@nanogen.com>:
          * Add $preferred_code option to encode() to allow user to specify a
            preference of e.g. code B
          * Modify _encodable to not be so eager to switch to code C
        - Make GD optional; support either GIF or PNG (Add gd_image
          and gif methods)
        - Add scale option to gif, png, gd_image
        - Alter gd_image to use options
        - Update diagnostics section to match same sequence they appear in code
        - Add font_align option (courtesy Gavin Brock <gavin.brock@nssmb.com>)
        - Add transparent_text option

2.01  Tue Jul 17 13:07:04 PDT 2007
        - Change AUTOLOAD to not try to call destroy()
          As suggested in http://rt.cpan.org/Public/Bug/Display.html?id=14850

2.20  Tue Mar 29 11:51:38 CDT 2011

MANIFEST  view on Meta::CPAN

Changes
MANIFEST
Makefile.PL
README
lib/Barcode/Code128.pm
t/barcode.t
t/code128.gif
t/code128.png
t/gif.t
t/png.t
META.yml                                 Module meta-data (added by MakeMaker)
META.json                                Module JSON meta-data (added by MakeMaker)

README  view on Meta::CPAN

    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

EXAMPLE:
    use Barcode::Code128;
    $object = new Barcode::Code128;
    print "Content-Type: image/png\n\n";
    $object->png('CODE 128');

AUTHOR: William R. Ward, wrw@cpan.org

TERMS: This module is placed into the public domain.  You are free to
    use, modify, or redistribute this module in any way for commercial
    or other uses.  My only request is that if you change it, please
    submit copies of your changed code (or diffs) so that I can
    incorporate them into the version on CPAN.  Also, in order to
    reduce the likelihood of confusion please do not distribute a
    modified version of this module unless you change the name first.

lib/Barcode/Code128.pm  view on Meta::CPAN

    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.

lib/Barcode/Code128.pm  view on Meta::CPAN

            $count++;
        }
        return $count;
    }
}

##----------------------------------------------------------------------------

=item gif

=item png

=item 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 C<$text> to the C<barcode()>, C<encode()>, or
C<text()> methods, you do not need to specify it again.  The C<$x> and

lib/Barcode/Code128.pm  view on Meta::CPAN


sub gif
{
    my($self, $text, $x, $y, $scale) = @_;
    croak "The gif() method of Barcode::Code128 requires the GD module"
        unless $GD_VERSION;
    my $image = $self->gd_image($text, $x, $y, $scale);
    return $image->gif();
}

sub png
{
    my($self, $text, $x, $y, $scale) = @_;
    croak "The png() method of Barcode::Code128 requires the GD module"
        unless $GD_VERSION;
    my $image = $self->gd_image($text, $x, $y, $scale);
    return $image->png();
}

##----------------------------------------------------------------------------

=item barcode

Usage:

    $object->barcode($text)

lib/Barcode/Code128.pm  view on Meta::CPAN

=over 4

=item Unrecognized option ($opt) for $class

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

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

To call the C<gd_image()>, C<png()>, or C<gif()> methods, the GD
module must be present.  This module is used to create the actual
image.  Without it, you can only use the C<barcode()> method.

=item Scale must be a positive integer

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

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

The border option cannot be a fractional or negative number.

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

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

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

=item Unable to create $x x $y image

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

=item The gif() method of Barcode::Code128 requires the GD module

=item The gif() method of Barcode::Code128 requires version less than 1.20 of GD

=item The png() method of Barcode::Code128 requires the GD module

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

=item No encoded text found

This message from C<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 C<gd_image()>,
C<png()>, C<gif()>, or C<barcode()> without having specified the text
to be encoded.

=item No text defined

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

=item Invalid preferred code ``$preferred_code''

t/gif.t  view on Meta::CPAN

use strict;

use Test::More tests=>294;
use Barcode::Code128 qw(FNC1);


 SKIP: {
     eval { require GD; };

     skip "GD not installed - skipping test", 294 if ($@);
     skip "GD version >= 1.20 and < 2.18 - use png", 294
         unless ($GD::VERSION < 1.20 or $GD::VERSION >= 2.18);

     my $code = new Barcode::Code128;

     my $test = $code->gif("CODE 128");

     my $good = GD::Image->newFromGif('t/code128.gif');
     my $image = GD::Image->newFromGifData($test);

     for (my $x=0; $x< $image->width; $x++) {

t/png.t  view on Meta::CPAN

# -*- CPerl -*-
#
#  test script for generating png files from Barcode::Code128
#
#  Make sure the module loads correctly - if GD is less than 1.20, skip tests
#
#  Lukas Mueller <lam87@cornell.edu>, June 2012

use strict;

use Test::More tests=>294;
use Barcode::Code128 qw(FNC1);

SKIP:
{


     eval { require GD; };

     skip "GD not installed - skipping test", 294 if ($@);

     skip "GD version < 1.20 - no png support", 294 unless $GD::VERSION > 1.20;

     my $code = new Barcode::Code128;

     my $test = $code->png("CODE 128");

     my $good = GD::Image->newFromPng('t/code128.png');
     my $image = GD::Image->newFromPngData($test);

     for (my $x=0; $x< $image->width; $x++)
     {


          my $y = int($image->height()/2);

           my ($r, $g, $b) = $image->rgb($image->getPixel($x, $y));
           my ($R, $G, $B) = $good->rgb($good->getPixel($x, $y));



( run in 1.110 second using v1.01-cache-2.11-cpan-df04353d9ac )