Sample code for 30+ languages & platforms
Delphi ActiveX Requires Chilkat v11.7.0+

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 Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
json: TChilkatJsonObject;
fmt: TChilkatFormats;
jsonOut: TChilkatJsonObject;
width: Integer;
height: Integer;
rowBytes: Integer;
bd: TChilkatBinData;
sbRow: TChilkatStringBuilder;
row: Integer;
col: Integer;
b: Integer;
mask: Integer;

begin
success := 0;

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

json := TChilkatJsonObject.Create(Self);
json.UpdateString('payload.text','CHILKAT');
json.UpdateBool('output.includeModuleData',1);

fmt := TChilkatFormats.Create(Self);
jsonOut := TChilkatJsonObject.Create(Self);
success := fmt.QrCode('generate',json.ControlInterface,jsonOut.ControlInterface);
if (success = 0) then
  begin
    Memo1.Lines.Add(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');
Memo1.Lines.Add('kind: ' + jsonOut.StringOf('symbols[0].moduleData.kind'));
Memo1.Lines.Add('modules: ' + IntToStr(width) + ' x ' + IntToStr(height) + ', ' + IntToStr(rowBytes) + ' bytes per row');

//  The matrix is height rows of rowBytes bytes.
bd := TChilkatBinData.Create(Self);
success := bd.AppendEncoded(jsonOut.StringOf('symbols[0].moduleData.bytesBase64'),'base64');
if (success = 0) then
  begin
    Memo1.Lines.Add(bd.LastErrorText);
    Exit;
  end;

sbRow := TChilkatStringBuilder.Create(Self);

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;

    Memo1.Lines.Add(sbRow.GetAsString());
  end;

//  The same symbol as the PNG that the call also returned:

//  image

//  Sample output:

//  kind: bitMatrix
//  modules: 21 x 21, 3 bytes per row
//  ##############              ##############
//  ##          ##      ####    ##          ##
//  ##  ######  ##  ##  ####    ##  ######  ##
//  ##  ######  ##  ##          ##  ######  ##
//  ##  ######  ##  ##  ##  ##  ##  ######  ##
//  ##          ##  ####  ##    ##          ##
//  ##############  ##  ##  ##  ##############
//                  ####
//  ##  ##########      ####    ##########
//      ####          ##  ########    ######
//  ####  ########    ####  ##  ####      ####
//        ##  ##      ############    ####  ##
//    ######  ####  ######  ##    ##  ##
//                  ##  ##  ##    ##  ##    ##
//  ##############        ##  ##    ##      ##
//  ##          ##  ##  ##        ######  ####
//  ##  ######  ##  ####  ##  ##    ##
//  ##  ######  ##  ##  ############    ##
//  ##  ######  ##  ##      ##  ####  ##
//  ##          ##    ##  ########          ##
//  ##############  ##      ##      ######