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

Generate a QR Code PNG in Memory

See more QR Code Examples

Demonstrates getting a QR code image without writing a file. With no output options the result is a PNG returned as base64 in outputs[0].base64 of the response. The example decodes it into a BinData, which holds the bytes of the PNG to send in an HTTP response, store in a database, attach to an email, or save.

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

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

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

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/');
  json.UpdateInt('render.moduleSize',6);

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

  WriteLn('format: ' + jsonOut.StringOf('outputs[0].format'));
  WriteLn('MIME type: ' + jsonOut.StringOf('outputs[0].mimeType'));
  WriteLn('image: ' + jsonOut.IntOf('outputs[0].width') + ' x ' + jsonOut.IntOf('outputs[0].height') + ' pixels');

  //  outputs[0].base64 is the PNG file, base64 encoded.
  bdPng := TBinData.Create;
  success := bdPng.AppendEncoded(jsonOut.StringOf('outputs[0].base64'),'base64');
  if (success = False) then
    begin
      WriteLn(bdPng.LastErrorText);
      Exit;
    end;
  WriteLn('PNG bytes: ' + bdPng.NumBytes);

  //  The BinData holds the image.  Here it is simply saved.
  success := bdPng.WriteFile('qa_output/qr_in_memory.png');
  if (success = False) then
    begin
      WriteLn(bdPng.LastErrorText);
      Exit;
    end;

  //  The PNG held in bdPng:

  //  image

  //  Sample output:

  //  format: png
  //  MIME type: image/png
  //  image: 222 x 222 pixels
  //  PNG bytes: 877


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