The Road to Delphi

Delphi – Free Pascal – Oxygene

Generating Qr Codes with Delphi

25 Comments

 The QR Codes are a  special kind of images that used to represent two-dimensional barcodes. They are also known as hardlinks or physical world hyperlinks.

The QR Codes can store up to 4,296 alphanumeric characters of arbitrary text. The  QR codes can be read by an optical device with the appropriate software. Such devices range from dedicated QR code readers to mobile phones.

On this post I will show you how using the Google Chart Tools / Image Charts (aka Chart API) you can easily generate QR codes.

Using this API is very  straightforward, all you need to do is to generate a QR Code is make a Get request to this URI

http://chart.apis.google.com/chart?chs=200x200&cht=qr&chld=M&chl=Go+Delphi+Go

And how response you will get a png image (you can change the output format to gif adding the chof parameter to the URI  like so : chof=gif).

On the Google Chart Documentation you can find more info about the parameters to generate a QR Code.

Note : If you want encode more of 2000 chars do you need make a post request (this up to you).

Finally this a sample Delphi source to generate a QR Code.


uses
 PngImage,
 HTTPApp,
 WinInet;

type
TQrImage_ErrCorrLevel=(L,M,Q,H);

const
UrlGoogleQrCode='http://chart.apis.google.com/chart?chs=%dx%d&cht=qr&chld=%s&chl=%s';
QrImgCorrStr   : array [TQrImage_ErrCorrLevel] of string=('L','M','Q','H');

procedure WinInet_HttpGet(const Url: string;Stream:TStream);
const
BuffSize = 1024*1024;
var
  hInter   : HINTERNET;
  UrlHandle: HINTERNET;
  BytesRead: DWORD;
  Buffer   : Pointer;
begin
  hInter := InternetOpen('', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  if Assigned(hInter) then
  begin
    Stream.Seek(0,0);
    GetMem(Buffer,BuffSize);
    try
        UrlHandle := InternetOpenUrl(hInter, PChar(Url), nil, 0, INTERNET_FLAG_RELOAD, 0);
        if Assigned(UrlHandle) then
        begin
          repeat
            InternetReadFile(UrlHandle, Buffer, BuffSize, BytesRead);
            if BytesRead>0 then
             Stream.WriteBuffer(Buffer^,BytesRead);
          until BytesRead = 0;
          InternetCloseHandle(UrlHandle);
        end;
    finally
      FreeMem(Buffer);
    end;
    InternetCloseHandle(hInter);
  end
end;

//this function return a Stream (PngImage inside) with a Qr code.
procedure GetQrCode(Width,Height:Word;Correction_Level:TQrImage_ErrCorrLevel;const Data:string;StreamImage : TMemoryStream);
Var
 EncodedURL  : string;
begin
  EncodedURL:=Format(UrlGoogleQrCode,[Width,Height,QrImgCorrStr[Correction_Level],HTTPEncode(Data)]);
  WinInet_HttpGet(EncodedURL,StreamImage);
end;

Check the source code of the application on Github

Author: Rodrigo

Just another Delphi guy.

25 thoughts on “Generating Qr Codes with Delphi

  1. Asombroso, excelente.

    ¿Se podrá decodificar la imagen usando una webcam?

  2. Pingback: Generating Qr Codes with delphi

  3. Excelente,!! ¿Donde puedo encontrar el algoritmo para decodificar la imagen? ¿alguna idea?

  4. Pingback: Generating QR-Code Using Delphi | 宝宝软件应用平台-产品博客

  5. Why use RAR for a Windows App when Windows can’t handle it?

  6. Pingback: 如何在delphi中使用google chart api来显示QRCode | 创意纪

  7. the compilation can´t find the PngImage file!!! where is the file?

    • Maybe you are using an old version of delphi. The post explain how use the gif format instead which is supported for old versions. else you can use a third party library with png support,

  8. Can I use this for Delphi 7? I get the errormessage “Invalid GIF signature”
    I have tried to change from PngImage to GIFImage, but the same error appears.

  9. I have tried PngImage and GifImage bur running the application shows the same error “Invalid GIF signature”

  10. I have tried with:
    Png:
    http://pngdelphi.sourceforge.net
    Gustavo Huffenbacher Daud (gustavo.daud@terra.com.br)
    and
    Gif:
    Author(s): anme: Anders Melander, anders@melander.dk
    fila: Filip Larsen
    rps: Reinier Sterkenburg

    I’m just using the code, that can be downloaded above on this site.

    • Check this link (binaries and sources included) for a Delphi 7 project which uses the TGifImage component from Anders Melanders, this demo app download and display a QrImage in gif format.

      Demo Qr Delphi 7

  11. There is a small hint for the line 50 of the sourcecode:

    Because the message has to be formated in UTF-8 you shoud call the HTTPEncode method with the UTF-8 coded string, e.g. like that: HTTPEncode(Utf8Encode(Data))

    Otherwise you can get problems with special characters (results in a qr-code with no information inside)

  12. Pingback: Delphi QR Code component / generator? | Ask Programming & Technology

  13. The company I work for has just released an open source Delphi QR code generator called DelphiZXingQRCode. It’s a Delphi port of a Java barcode image processing library called ZXing.

    http://www.debenu.com/open-source/delphizxingqrcode/

  14. Pingback: Liens de l’épisode 4 de la saison 3 | Le Dev du Jeudi

  15. The Internet isn’t reuquired if you use the local library, such as ZXing, 2D Barcode VCL Components from http://www.han-soft.com/barcode2d.php

Leave a comment