Imager::zxing - decode barcodes from Imager images using libzxing
use Imager::zxing; my $decoder = Imager::zxing::Decoder->new; # list accepted formats separated by '|' print $decoder->formats; # list available formats print $decoder->avail_formats # set the accepted formats $decoder->set_formats("DataMatrix|QRCode") or die $decoder->error; # decode any barcodes my $im = Imager->new(file => "somefile.png"); my @results = $decoder->decode($im); for my $result (@results) { print $result->text, "\n"; }
A primitive wrapper around zxing-cpp
This requires at least 1.4.0 of zxing-cpp, but 2.1.0 is preferable.
Currently only supports decoding and doesn't expose much of the interface.
To use this:
Create a decoder object:
use Imager::zxing; my $decoder = Imager::zxing::Decoder->new;
Configure it if needed, most likely by setting the accepted barcode encodings:
$decoder->set_formats("DataMatrix|QRCode");
Load an image using Imager:
my $img = Imager->new(file => "somename.png") or die "Cannot load image ", Imager->errstr;
The available file formats depends on the libraries Imager was built with.
Decode the barcode:
my @results = $decoder->decode($img) or die "No barcodes found";
Process the results:
for my $r (@results) { print $r->format, ": ", $r->text, "\n"; }
new
my $decoder = Imager::zxing::Decoder->new;
Create a new decoder object, does not accept any parameters.
Default is to process all available barcode formats.
avail_formats
my @formats = Imager::zxing::Decoder->avail_formats
Returns a list of the barcode formats that are decodable.
Create a decoder with:
decode(image)
Attempts to decode barcodes from the supplied Imager image object.
Returns a list of result objects, or an empty list if none are found.
my $img = Imager->new(file => "somefile.png") or die Imager->errstr; my @results = $decoder->decode($img);
formats()
Returns the formats the decoder accepts as a | separated string.
|
print $decoder->formats # default output: # Aztec|Codabar|Code39|Code93|Code128|DataBar|DataBarExpanded|DataMatrix|EAN-8|EAN-13|ITF|MaxiCode|PDF417|QRCode|UPC-A|UPC-E|MicroQRCode
set_formats(formats)
Sets the barcode formats that the decoder will decode, as a space, | or comma separated string.
set_return_errors($bool)
Set to non-zero to include results with soft errors such as checksum errors.
$decoder->set_return_errors(1);
set_pure($bool)
Set to non-zero to only accept results where the image is an aligned image where the image is only the barcode.
Note: this appears to be non-functional in my testing, this accepted a rotated image.
$decoder->set_pure(1);
Result objects are returned by the decoder decode() method:
my @results = $decoder->decode($image);
text()
Returns the decoded text.
my $text = $result->text;
is_valid()
True if the result represents a valid decoded barcode.
is_mirrored()
True if the result is from a mirrored barcode.
is_inverted()
True if the barcode image has inverted dark/light. Requires zxing 2.0.0 to be valid.
format()
The format of the decoded barcode.
position()
The co-ordinates of the top left, top right, bottom left and bottom right points of the decoded barcode in the supplied image, as a list.
orientation()
The rotation of the barcode image in degrees.
Imager::zxing is licensed under the same terms as perl itself.
To install Imager::zxing, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Imager::zxing
CPAN shell
perl -MCPAN -e shell install Imager::zxing
For more information on module installation, please visit the detailed CPAN module installation guide.