Perl Requires Chilkat v11.7.0+
Chilkat2 Perl
Get the Modules of a QR Code (Bit Matrix)
See more QR Code Examples
Demonstrates getting the QR code itself, as a grid of dark and light modules, to draw with something other than the built-in PNG and SVG output: on a canvas, in a PDF, on a display, or with a label printer language. With output.includeModuleData the response has symbols[0].moduleData, a bit matrix with one bit per module, each row starting on a new byte, most significant bit first, 1 for dark. The quiet zone is not part of the matrix. The example prints the symbol using two characters per module.Chilkat Perl Downloads
use strict;
use warnings;
use Chilkat2;
# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
my $json = Chilkat2::JsonObject->new;
$json->UpdateString('payload.text', 'CHILKAT');
$json->UpdateBool('output.includeModuleData', 1);
my $fmt = Chilkat2::Formats->new;
my $jsonOut = Chilkat2::JsonObject->new;
my $success = $fmt->QrCode('generate', $json, $jsonOut);
if (!$success) {
print $fmt->LastErrorText, "\n";
exit;
}
my $width = $jsonOut->IntOf('symbols[0].moduleData.width');
my $height = $jsonOut->IntOf('symbols[0].moduleData.height');
my $rowBytes = $jsonOut->IntOf('symbols[0].moduleData.rowBytes');
print 'kind: ', $jsonOut->StringOf('symbols[0].moduleData.kind'), "\n";
print 'modules: ', $width, ' x ', $height, ', ', $rowBytes, " bytes per row\n";
# The matrix is height rows of rowBytes bytes.
my $bd = Chilkat2::BinData->new;
$success = $bd->AppendEncoded($jsonOut->StringOf('symbols[0].moduleData.bytesBase64'), 'base64');
if (!$success) {
print $bd->LastErrorText, "\n";
exit;
}
my $sbRow = Chilkat2::StringBuilder->new;
my $b = 0;
my $mask = 128;
for my $row (0 .. $height - 1) {
$sbRow->Clear();
for my $col (0 .. $width - 1) {
# The module at (col,row) is a bit of the byte at (row * rowBytes) + (col / 8).
# The leftmost module of each byte is its most significant bit.
if (($col % 8) == 0) {
$mask = 128;
}
$b = $bd->GetByte(($row * $rowBytes) + (int($col / 8)));
if (((int($b / $mask)) % 2) == 1) {
$sbRow->Append('##');
} else {
$sbRow->Append(' ');
}
$mask = int($mask / 2);
}
print $sbRow->GetAsString(), "\n";
}
# The same symbol as the PNG that the call also returned:
#
# Sample output:
# kind: bitMatrix
# modules: 21 x 21, 3 bytes per row
# ############## ##############
# ## ## #### ## ##
# ## ###### ## ## #### ## ###### ##
# ## ###### ## ## ## ###### ##
# ## ###### ## ## ## ## ## ###### ##
# ## ## #### ## ## ##
# ############## ## ## ## ##############
# ####
# ## ########## #### ##########
# #### ## ######## ######
# #### ######## #### ## #### ####
# ## ## ############ #### ##
# ###### #### ###### ## ## ##
# ## ## ## ## ## ##
# ############## ## ## ## ##
# ## ## ## ## ###### ####
# ## ###### ## #### ## ## ##
# ## ###### ## ## ############ ##
# ## ###### ## ## ## #### ##
# ## ## ## ######## ##
# ############## ## ## ######