1. Download link: GitHub - foxitsoftware/DelphiZXingQRCode: Delphi port of QR Code functionality from ZXing, a barcode image processing library.
2. Use steps:
1) Reference the unit file.
//引用单元
uses
DelphiZXIngQRCode
2) Write the function.
//生成qrcode函数
procedure GenerateQRCodeBitmap(const SourceText: string; const Encoding: TQRCodeEncoding;
const QuietZone: Integer; const Bitmap: TBitmap);
var
QRCode: TDelphiZXingQRCode;
Row, Column: Integer;
begin
QRCode := TDelphiZXingQRCode.Create;
try
QRCode.Data := SourceText;
QRCode.Encoding := Encoding;
QRCode.QuietZone := QuietZone;
Bitmap.SetSize(QRCode.Rows, QRCode.Columns);
for Row := 0 to QRCode.Rows - 1 do
begin
for Column := 0 to QRCode.Columns - 1 do
begin
if (QRCode.IsBlack[Row, Column]) then
begin
Bitmap.Canvas.Pixels[Column, Row] := clBlack;
end else
begin
Bitmap.Canvas.Pixels[Column, Row] := clWhite;
end;
end;
end;
finally
QRCode.Free;
end;
end;
3) call
procedure TForm1.Button5Click(Sender: TObject);
begin
QRCodeBitmap:=TBitmap.Create;
GenerateQRCodeBitmap('http://www.example.com', TQRCodeEncoding.qrAuto, 4, QRCodeBitmap);
image1.Picture.Bitmap:=QRCodeBitmap;
QRCodeBitmap.free;
end;
3. Code explanation:
This process accepts four parameters: SourceText indicates the text to be encoded into a QR code, Encoding indicates the encoding method, QuietZone indicates the size of the quiet zone, and Bitmap is a TBitmap object used to save the generated QR code.
In the process, we first create a TDelphiZXingQRCode object, set its Data, Encoding and QuietZone properties. Then create a TBitmap object and set its size to the Rows and Columns properties of the TDelphiZXingQRCode object. Next, we use a for loop to iterate through the IsBlack property of the TDelphiZXingQRCode object and set the corresponding pixel color to black or white. Finally, we release the TDelphiZXingQRCode object and redraw PaintBox1.
4. The effect is as follows: