MainForm.pas 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. unit MainForm;
  2. interface
  3. uses
  4. Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  5. Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Generics.Collections, System.UITypes,
  6. Vcl.ExtCtrls, qr.code, Vcl.ComCtrls;
  7. {$SCOPEDENUMS ON}
  8. type
  9. TfrmQRMain = class(TForm)
  10. txtData: TEdit;
  11. cbECL: TComboBox;
  12. imgQR: TImage;
  13. txtWidth: TEdit;
  14. chkGrid: TCheckBox;
  15. txtDetails: TMemo;
  16. cbMask: TComboBox;
  17. cbVersion: TComboBox;
  18. procedure txtDataChange(Sender: TObject);
  19. private
  20. FQR : TQRCode;
  21. protected
  22. public
  23. procedure AfterConstruction; override;
  24. procedure BeforeDestruction; override;
  25. end;
  26. var
  27. frmQRMain: TfrmQRMain;
  28. implementation
  29. {$R *.dfm}
  30. procedure TfrmQRMain.BeforeDestruction;
  31. begin
  32. inherited;
  33. FQR.Free;
  34. end;
  35. procedure TfrmQRMain.AfterConstruction;
  36. begin
  37. inherited;
  38. cbECL.ItemIndex := 0;
  39. cbMask.ItemIndex := 8;
  40. cbVersion.ItemIndex := 0;
  41. FQR := TQRCode.Create;
  42. FQR.OnPaint :=
  43. procedure(Width, Height : integer; BlackRects : TArray<TRect>)
  44. function ECLToString : string;
  45. begin
  46. case FQR.ECLInUse of
  47. TErrorCorrectionLevel.Auto:
  48. Result := 'ERROR (Auto)';
  49. TErrorCorrectionLevel.Low:
  50. Result := 'Low';
  51. TErrorCorrectionLevel.Medium:
  52. Result := 'Medium';
  53. TErrorCorrectionLevel.Quartile:
  54. Result := 'Quartile';
  55. TErrorCorrectionLevel.High:
  56. Result := 'High';
  57. end;
  58. end;
  59. var
  60. bmp : TBitmap;
  61. R : TRect;
  62. x, y : integer;
  63. i: Integer;
  64. s, s2 : string;
  65. b : Byte;
  66. begin
  67. txtDetails.Lines.BeginUpdate;
  68. try
  69. txtDetails.Lines.Clear;
  70. txtDetails.Lines.Add('Version: '+Integer(FQR.VersionInUse).ToString);
  71. txtDetails.Lines.Add('ECL: '+ECLToString);
  72. txtDetails.Lines.Add('Module Size: '+FQR.Size.ToString+' at '+FQR.PixelsPerModule.ToString+' pixels');
  73. txtDetails.Lines.Add('Mask: '+Integer(FQR.MaskInUse).ToString);
  74. txtDetails.Lines.Add('');
  75. for i := 0 to 7 do
  76. begin
  77. txtDetails.Lines.Add('Penalty Score for Mask '+i.ToString);
  78. txtDetails.Lines.Add(' 1 = '+FQR.PenaltyScores[i][0].ToString);
  79. txtDetails.Lines.Add(' 2 = '+FQR.PenaltyScores[i][1].ToString);
  80. txtDetails.Lines.Add(' 3 = '+FQR.PenaltyScores[i][2].ToString);
  81. txtDetails.Lines.Add(' 4 = '+FQR.PenaltyScores[i][3].ToString);
  82. txtDetails.Lines.Add(' T = '+(FQR.PenaltyScores[i][0]+FQR.PenaltyScores[i][1]+FQR.PenaltyScores[i][2]+FQR.PenaltyScores[i][3]).ToString);
  83. txtDetails.Lines.Add('');
  84. end;
  85. txtDetails.Lines.Add('Bits:');
  86. txtDetails.Lines.Add(FQR.DataBits);
  87. txtDetails.Lines.Add('');
  88. txtDetails.Lines.Add('Codewords: ');
  89. s := '';
  90. s2 := '';
  91. for b in FQR.Codewords do
  92. begin
  93. s := s+', '+b.ToString;
  94. s2 := s2+', '+IntToHex(b,2);
  95. end;
  96. Delete(s,1,2);
  97. Delete(s2,1,2);
  98. txtDetails.Lines.Add(s);
  99. txtDetails.Lines.Add(s2);
  100. finally
  101. txtDetails.Lines.EndUpdate;
  102. end;
  103. bmp := TBitmap.Create;
  104. try
  105. bmp.Width := Width;
  106. bmp.Height := Height;
  107. bmp.Canvas.Lock;
  108. try
  109. bmp.Canvas.Brush.Color := clWhite;
  110. bmp.Canvas.Pen.Color := clWhite;
  111. bmp.Canvas.FillRect(TRect.Create(0,0,bmp.Width, bmp.Height));
  112. bmp.Canvas.Brush.Color := clBlack;
  113. bmp.Canvas.Pen.Color := clBlack;
  114. for R in BlackRects do
  115. begin
  116. bmp.Canvas.FillRect(R);
  117. end;
  118. if chkGrid.Checked then
  119. begin
  120. bmp.Canvas.Pen.Color := clLtGray;
  121. bmp.Canvas.Pen.Style := TPenStyle.psDot;
  122. for x := 0 to bmp.Width -1 do
  123. if x mod FQR.PixelsPerModule = 0 then
  124. begin
  125. bmp.Canvas.MoveTo(x,0);
  126. bmp.Canvas.LineTo(x,bmp.Height-1);
  127. end;
  128. for y := 0 to bmp.Height-1 do
  129. begin
  130. if y mod FQR.PixelsPerModule = 0 then
  131. begin
  132. bmp.Canvas.MoveTo(0,y);
  133. bmp.Canvas.LineTo(bmp.Height-1, y);
  134. end;
  135. end;
  136. end;
  137. bmp.Canvas.Brush.Color := clBlack;
  138. bmp.Canvas.Pen.Color := clBlack;
  139. bmp.Canvas.FrameRect(TRect.Create(0,0,bmp.Width, bmp.Height));
  140. finally
  141. bmp.Canvas.Unlock;
  142. end;
  143. imgQR.Picture.Graphic := bmp;
  144. finally
  145. bmp.Free;
  146. end;
  147. end;
  148. txtDataChange(Self);
  149. end;
  150. procedure TfrmQRMain.txtDataChange(Sender: TObject);
  151. begin
  152. FQR.BeginUpdate;
  153. try
  154. FQR.ECL := TErrorCorrectionLevel(cbECL.ItemIndex);
  155. FQR.Text := txtData.Text;
  156. FQR.Mask := TMask(cbMask.ItemIndex);
  157. FQR.Version := TVersion(cbVersion.ItemIndex);
  158. FQR.RenderSize := StrToIntDef(txtWidth.Text, 300);
  159. finally
  160. FQR.EndUpdate;
  161. end;
  162. end;
  163. end.