MainForm.pas 5.3 KB

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