Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.7.0+

QR Code Error Correction Levels

See more QR Code Examples

Demonstrates the four QR error-correction levels. L, M, Q and H allow a reader to recover the data when about 7%, 15%, 25% and 30% of the symbol is damaged or covered. More error correction leaves less room for data, so the same text needs a larger symbol at a higher level. The default is M. The example also shows boostErrorCorrection, which raises the level for free when the chosen version has room to spare.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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.JsonArray,
  Chilkat.Formats,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  fmt: TFormats;
  json: TJsonObject;
  jsonOut: TJsonObject;
  levels: TJsonArray;
  i: Integer;
  json2: TJsonObject;

begin
  success := False;

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

  fmt := TFormats.Create;
  json := TJsonObject.Create;
  jsonOut := TJsonObject.Create;
  json.UpdateString('payload.text','https://www.chilkatsoft.com/barcode_overview.asp');

  //  Encode the same text at each level.
  levels := TJsonArray.Create;
  levels.Load('["L","M","Q","H"]');

  for i := 0 to 3 do
    begin
      json.UpdateString('encoding.qr.errorCorrection',levels.StringAt(i));
      success := fmt.QrCode('generate',json,jsonOut);
      if (success = False) then
        begin
          WriteLn(fmt.LastErrorText);
          Exit;
        end;
      WriteLn('level ' + levels.StringAt(i) + ': version ' + jsonOut.IntOf('symbols[0].selectedEncoding.version') + ', ' + jsonOut.IntOf('symbols[0].dimensions.modulesWide')
           + ' modules wide');
    end;

  //  The four symbols.  They hold the same text, and the higher the level, the larger the symbol:

  //  image

  //  A symbol often has room left over.  boostErrorCorrection uses it: the level is raised
  //  as far as the same version allows, so the symbol is no larger but is more robust.
  json2 := TJsonObject.Create;
  json2.UpdateString('payload.text','https://chilkatsoft.com');
  json2.UpdateString('encoding.qr.errorCorrection','L');
  success := fmt.QrCode('generate',json2,jsonOut);
  if (success = False) then
    begin
      WriteLn(fmt.LastErrorText);
      Exit;
    end;
  WriteLn('level L requested: version ' + jsonOut.IntOf('symbols[0].selectedEncoding.version') + ', level ' + jsonOut.StringOf('symbols[0].selectedEncoding.errorCorrection'));

  json2.UpdateBool('encoding.qr.boostErrorCorrection',True);
  success := fmt.QrCode('generate',json2,jsonOut);
  if (success = False) then
    begin
      WriteLn(fmt.LastErrorText);
      Exit;
    end;
  WriteLn('level L with boost: version ' + jsonOut.IntOf('symbols[0].selectedEncoding.version') + ', level ' + jsonOut.StringOf('symbols[0].selectedEncoding.errorCorrection'));

  //  Level L, and level M reached by the boost.  The two symbols are the same size:

  //  image

  //  Sample output:

  //  level L: version 3, 29 modules wide
  //  level M: version 4, 33 modules wide
  //  level Q: version 5, 37 modules wide
  //  level H: version 6, 41 modules wide
  //  level L requested: version 2, level L
  //  level L with boost: version 2, level M


  fmt.Free;
  json.Free;
  jsonOut.Free;
  levels.Free;
  json2.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.