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.
[sourcecode wraplines=”false” language=”delphi”]
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;
[/sourcecode]


