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

Measure a QR Code without Rendering It

See more QR Code Examples

Demonstrates the measure operation, which does everything generate does except draw the image. It reports the version chosen and the exact size the image would have, which is useful for laying out a page or a label before any image is made. Unlike generate, a code that does not fit the limits is not a failure for measure: the call succeeds, fits is false, and reasons[] says why.

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

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

procedure RunDemo;
var
  success: Boolean;
  json: TJsonObject;
  fmt: TFormats;
  jsonOut: TJsonObject;

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','https://www.chilkatsoft.com/barcode_overview.asp');
  json.UpdateInt('render.moduleSize',8);

  fmt := TFormats.Create;
  jsonOut := TJsonObject.Create;
  success := fmt.QrCode('measure',json,jsonOut);
  if (success = False) then
    begin
      WriteLn(fmt.LastErrorText);
      Exit;
    end;

  WriteLn('fits: ' + jsonOut.BoolOf('fits'));
  WriteLn('QR version: ' + jsonOut.IntOf('symbols[0].selectedEncoding.version'));
  WriteLn('image would be: ' + jsonOut.IntOf('measurements[0].canvasWidth') + ' x ' + jsonOut.IntOf('measurements[0].canvasHeight') + ' pixels');
  WriteLn('symbol: ' + jsonOut.IntOf('measurements[0].symbolWidth') + ' pixels wide at x = ' + jsonOut.IntOf('measurements[0].symbolX'));

  //  Would it fit in a space 200 pixels wide?
  json.UpdateInt('render.maxWidth',200);
  success := fmt.QrCode('measure',json,jsonOut);
  if (success = False) then
    begin
      WriteLn(fmt.LastErrorText);
      Exit;
    end;

  WriteLn('within 200 pixels, fits: ' + jsonOut.BoolOf('fits'));
  if (jsonOut.BoolOf('fits') = False) then
    begin
      WriteLn('reason: ' + jsonOut.StringOf('reasons[0].code') + ' (' + jsonOut.StringOf('reasons[0].message') + ')');
    end;

  //  What measure reported, drawn as a diagram.  The measure operation itself makes no image:

  //  image

  //  Sample output:

  //  fits: True
  //  QR version: 4
  //  image would be: 328 x 328 pixels
  //  symbol: 264 pixels wide at x = 32
  //  within 200 pixels, fits: False
  //  reason: noFit (exceedsMaxWidth)


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