Lazarus Pascal Requires Chilkat v11.7.0+
Lazarus Pascal
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 Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.BinData,
Chilkat.StringBuilder,
Chilkat.Formats,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
json: TJsonObject;
fmt: TFormats;
jsonOut: TJsonObject;
width: Integer;
height: Integer;
rowBytes: Integer;
bd: TBinData;
sbRow: TStringBuilder;
row: Integer;
col: Integer;
b: Integer;
mask: Integer;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
json := TJsonObject.Create;
json.UpdateString('payload.text','CHILKAT');
json.UpdateBool('output.includeModuleData',True);
fmt := TFormats.Create;
jsonOut := TJsonObject.Create;
success := fmt.QrCode('generate',json,jsonOut);
if (success = False) then
begin
WriteLn(fmt.LastErrorText);
Exit;
end;
width := jsonOut.IntOf('symbols[0].moduleData.width');
height := jsonOut.IntOf('symbols[0].moduleData.height');
rowBytes := jsonOut.IntOf('symbols[0].moduleData.rowBytes');
WriteLn('kind: ' + jsonOut.StringOf('symbols[0].moduleData.kind'));
WriteLn('modules: ' + width + ' x ' + height + ', ' + rowBytes + ' bytes per row');
// The matrix is height rows of rowBytes bytes.
bd := TBinData.Create;
success := bd.AppendEncoded(jsonOut.StringOf('symbols[0].moduleData.bytesBase64'),'base64');
if (success = False) then
begin
WriteLn(bd.LastErrorText);
Exit;
end;
sbRow := TStringBuilder.Create;
mask := 128;
for row := 0 to height - 1 do
begin
sbRow.Clear();
for col := 0 to width - 1 do
begin
// 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 mod 8) = 0) then
begin
mask := 128;
end;
b := bd.GetByte((row * rowBytes) + (col / 8));
if (((b / mask) mod 2) = 1) then
begin
sbRow.Append('##');
end
else
begin
sbRow.Append(' ');
end;
mask := mask / 2;
end;
WriteLn(sbRow.GetAsString());
end;
// The same symbol as the PNG that the call also returned:
//
// Sample output:
// kind: bitMatrix
// modules: 21 x 21, 3 bytes per row
// ############## ##############
// ## ## #### ## ##
// ## ###### ## ## #### ## ###### ##
// ## ###### ## ## ## ###### ##
// ## ###### ## ## ## ## ## ###### ##
// ## ## #### ## ## ##
// ############## ## ## ## ##############
// ####
// ## ########## #### ##########
// #### ## ######## ######
// #### ######## #### ## #### ####
// ## ## ############ #### ##
// ###### #### ###### ## ## ##
// ## ## ## ## ## ##
// ############## ## ## ## ##
// ## ## ## ## ###### ####
// ## ###### ## #### ## ## ##
// ## ###### ## ## ############ ##
// ## ###### ## ## ## #### ##
// ## ## ## ######## ##
// ############## ## ## ######
json.Free;
fmt.Free;
jsonOut.Free;
bd.Free;
sbRow.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.