DelphiZXingQRCodeTestAppMainForm.pas 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. unit DelphiZXingQRCodeTestAppMainForm;
  2. // Demo app for ZXing QRCode port to Delphi, by Debenu Pty Ltd (www.debenu.com)
  3. // Need a PDF SDK? Checkout Debenu Quick PDF Library: http://www.debenu.com/products/development/debenu-pdf-library/
  4. interface
  5. uses
  6. Windows, Messages, SysUtils, Variants, Classes, Graphics,
  7. Controls, Forms, Dialogs, DelphiZXingQRCode, ExtCtrls,
  8. StdCtrls;
  9. type
  10. TForm1 = class(TForm)
  11. edtText: TEdit;
  12. Label1: TLabel;
  13. cmbEncoding: TComboBox;
  14. Label2: TLabel;
  15. Label3: TLabel;
  16. edtQuietZone: TEdit;
  17. Label4: TLabel;
  18. PaintBox1: TPaintBox;
  19. procedure FormDestroy(Sender: TObject);
  20. procedure FormCreate(Sender: TObject);
  21. procedure PaintBox1Paint(Sender: TObject);
  22. procedure edtTextChange(Sender: TObject);
  23. procedure cmbEncodingChange(Sender: TObject);
  24. procedure edtQuietZoneChange(Sender: TObject);
  25. private
  26. QRCodeBitmap: TBitmap;
  27. public
  28. procedure Update;
  29. end;
  30. var
  31. Form1: TForm1;
  32. implementation
  33. {$R *.dfm}
  34. procedure TForm1.cmbEncodingChange(Sender: TObject);
  35. begin
  36. Update;
  37. end;
  38. procedure TForm1.edtQuietZoneChange(Sender: TObject);
  39. begin
  40. Update;
  41. end;
  42. procedure TForm1.edtTextChange(Sender: TObject);
  43. begin
  44. Update;
  45. end;
  46. procedure TForm1.FormCreate(Sender: TObject);
  47. begin
  48. QRCodeBitmap := TBitmap.Create;
  49. edtText.Text := 'BEGIN:VCARD' + #13#10 +
  50. 'VERSION:3.0' + #13#10 +
  51. 'FN:张志林' + #13#10 +
  52. 'TEL;CELL:9999999' + #13#10 +
  53. 'ORG:网易研发部' + #13#10 +
  54. 'TITLE:技术总监' + #13#10 +
  55. 'TEL;WORK:123456789' + #13#10 +
  56. 'TEL;WORK:987654321' + #13#10 +
  57. 'EMAIL:zhangzhilin@sina.com' + #13#10 +
  58. 'END:VCARD' + #13#10;
  59. Update;
  60. end;
  61. procedure TForm1.FormDestroy(Sender: TObject);
  62. begin
  63. QRCodeBitmap.Free;
  64. end;
  65. procedure TForm1.PaintBox1Paint(Sender: TObject);
  66. var
  67. Scale: Double;
  68. begin
  69. PaintBox1.Canvas.Brush.Color := clWhite;
  70. PaintBox1.Canvas.FillRect(Rect(0, 0, PaintBox1.Width, PaintBox1.Height));
  71. if ((QRCodeBitmap.Width > 0) and (QRCodeBitmap.Height > 0)) then
  72. begin
  73. if (PaintBox1.Width < PaintBox1.Height) then
  74. begin
  75. Scale := PaintBox1.Width / QRCodeBitmap.Width;
  76. end else
  77. begin
  78. Scale := PaintBox1.Height / QRCodeBitmap.Height;
  79. end;
  80. PaintBox1.Canvas.StretchDraw(Rect(0, 0, Trunc(Scale * QRCodeBitmap.Width), Trunc(Scale * QRCodeBitmap.Height)), QRCodeBitmap);
  81. end;
  82. end;
  83. procedure TForm1.Update;
  84. var
  85. QRCode: TDelphiZXingQRCode;
  86. Row, Column: Integer;
  87. begin
  88. QRCode := TDelphiZXingQRCode.Create;
  89. try
  90. QRCode.Data := edtText.Text;
  91. QRCode.Encoding := TQRCodeEncoding(cmbEncoding.ItemIndex);
  92. QRCode.QuietZone := StrToIntDef(edtQuietZone.Text, 4);
  93. QRCodeBitmap.SetSize(QRCode.Rows, QRCode.Columns);
  94. for Row := 0 to QRCode.Rows - 1 do
  95. begin
  96. for Column := 0 to QRCode.Columns - 1 do
  97. begin
  98. if (QRCode.IsBlack[Row, Column]) then
  99. begin
  100. QRCodeBitmap.Canvas.Pixels[Column, Row] := clBlack;
  101. end else
  102. begin
  103. QRCodeBitmap.Canvas.Pixels[Column, Row] := clWhite;
  104. end;
  105. end;
  106. end;
  107. finally
  108. QRCode.Free;
  109. end;
  110. PaintBox1.Repaint;
  111. end;
  112. end.