qr.code.vcl.pas 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //Copyright (c) 2019 by Jason Southwell
  2. //
  3. //Permission is hereby granted, free of charge, to any person obtaining a copy
  4. //of this software and associated documentation files (the "Software"), to deal
  5. //in the Software without restriction, including without limitation the rights
  6. //to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. //copies of the Software, and to permit persons to whom the Software is
  8. //furnished to do so, subject to the following conditions:
  9. //
  10. //The above copyright notice and this permission notice shall be included in all
  11. //copies or substantial portions of the Software.
  12. //
  13. //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. //SOFTWARE.
  20. unit qr.code.vcl;
  21. interface
  22. uses
  23. System.SysUtils, System.Classes, VCL.Graphics, qr.code;
  24. type
  25. TQRBitmap = class(TBitmap)
  26. private
  27. FQR : TQRCode;
  28. FShowBorder: boolean;
  29. FShowModuleGrid: boolean;
  30. function GetData: TArray<Byte>;
  31. function GetECL: TErrorCorrectionLevel;
  32. function GetMask: TMask;
  33. function GetText: string;
  34. function GetVersion: TVersion;
  35. procedure SetData(const Value: TArray<Byte>);
  36. procedure SetECL(const Value: TErrorCorrectionLevel);
  37. procedure SetMask(const Value: TMask);
  38. procedure SetText(const Value: string);
  39. procedure SetVersion(const Value: TVersion);
  40. procedure SetShowBorder(const Value: boolean);
  41. procedure SetShowModuleGrid(const Value: boolean);
  42. public
  43. constructor Create(AText : string; AWidth, AHeight : integer); overload;
  44. constructor Create(AText : string); overload;
  45. constructor Create(AText : string; APixelsPerModule : integer); overload;
  46. constructor Create(AData : TArray<Byte>; AWidth, AHeight : integer); overload;
  47. constructor Create(AData : TArray<Byte>); overload;
  48. constructor Create(AData : TArray<Byte>; APixelsPerModule : integer); overload;
  49. constructor Create; overload; override;
  50. destructor Destroy; override;
  51. procedure BeginUpdate;
  52. procedure EndUpdate;
  53. property Text : string read GetText write SetText;
  54. property Data : TArray<Byte> read GetData write SetData;
  55. property Version : TVersion read GetVersion write SetVersion;
  56. property ECL : TErrorCorrectionLevel read GetECL write SetECL;
  57. property QRMask : TMask read GetMask write SetMask;
  58. property ShowModuleGrid : boolean read FShowModuleGrid write SetShowModuleGrid;
  59. property ShowBorder : boolean read FShowBorder write SetShowBorder;
  60. end;
  61. implementation
  62. uses System.Math, System.Types;
  63. { TQRCodeBitmap }
  64. procedure TQRBitmap.BeginUpdate;
  65. begin
  66. FQR.BeginUpdate;
  67. end;
  68. constructor TQRBitmap.Create;
  69. begin
  70. inherited Create;
  71. FQR := TQRCode.Create;
  72. FQR.OnPaint :=
  73. procedure(Width, Height : Integer; BlackRects : TArray<TRect>)
  74. var
  75. R : TRect;
  76. x, y : integer;
  77. begin
  78. Self.Width := Width;
  79. Self.Height := Height;
  80. Self.Canvas.Lock;
  81. try
  82. Self.Canvas.Brush.Color := clWhite;
  83. Self.Canvas.Pen.Color := clWhite;
  84. Self.Canvas.FillRect(TRect.Create(0,0,Self.Width, Self.Height));
  85. Self.Canvas.Brush.Color := clBlack;
  86. Self.Canvas.Pen.Color := clBlack;
  87. for R in BlackRects do
  88. begin
  89. Self.Canvas.FillRect(R);
  90. end;
  91. if FShowModuleGrid then
  92. begin
  93. Self.Canvas.Pen.Color := clLtGray;
  94. Self.Canvas.Pen.Style := TPenStyle.psDot;
  95. for x := 0 to Self.Width -1 do
  96. if x mod FQR.PixelsPerModule = 0 then
  97. begin
  98. Self.Canvas.MoveTo(x,0);
  99. Self.Canvas.LineTo(x,Self.Height-1);
  100. end;
  101. for y := 0 to Self.Height-1 do
  102. begin
  103. if y mod FQR.PixelsPerModule = 0 then
  104. begin
  105. Self.Canvas.MoveTo(0,y);
  106. Self.Canvas.LineTo(Self.Height-1, y);
  107. end;
  108. end;
  109. end;
  110. if FShowBorder then
  111. begin
  112. Self.Canvas.Brush.Color := clBlack;
  113. Self.Canvas.Pen.Color := clBlack;
  114. Self.Canvas.FrameRect(TRect.Create(0,0,Self.Width, Self.Height));
  115. end;
  116. finally
  117. Self.Canvas.Unlock;
  118. end;
  119. end;
  120. end;
  121. constructor TQRBitmap.Create(AText: string; APixelsPerModule: integer);
  122. begin
  123. Create;
  124. FQR.BeginUpdate;
  125. try
  126. FQR.Text := AText;
  127. FQR.PixelsPerModule := APixelsPerModule;
  128. finally
  129. FQR.EndUpdate;
  130. end;
  131. end;
  132. constructor TQRBitmap.Create(AData: TArray<Byte>; AWidth, AHeight: integer);
  133. begin
  134. Create;
  135. FQR.BeginUpdate;
  136. try
  137. FQR.Data := AData;
  138. FQR.RenderSize := Min(AWidth, AHeight);
  139. finally
  140. FQR.EndUpdate;
  141. end;
  142. end;
  143. constructor TQRBitmap.Create(AData: TArray<Byte>; APixelsPerModule: integer);
  144. begin
  145. Create;
  146. FQR.BeginUpdate;
  147. try
  148. FQR.Data := AData;
  149. FQR.PixelsPerModule := APixelsPerModule;
  150. finally
  151. FQR.EndUpdate;
  152. end;
  153. end;
  154. constructor TQRBitmap.Create(AData: TArray<Byte>);
  155. begin
  156. Create;
  157. FQR.Data := AData;
  158. end;
  159. constructor TQRBitmap.Create(AText: string);
  160. begin
  161. Create;
  162. FQR.Text := AText;
  163. end;
  164. constructor TQRBitmap.Create(AText: string; AWidth, AHeight: integer);
  165. begin
  166. Create;
  167. FQR.BeginUpdate;
  168. try
  169. FQR.Text := AText;
  170. FQR.RenderSize := Min(AWidth, AHeight);
  171. finally
  172. FQR.EndUpdate;
  173. end;
  174. end;
  175. destructor TQRBitmap.Destroy;
  176. begin
  177. FQR.Free;
  178. inherited;
  179. end;
  180. procedure TQRBitmap.EndUpdate;
  181. begin
  182. FQR.EndUpdate;
  183. end;
  184. function TQRBitmap.GetData: TArray<Byte>;
  185. begin
  186. Result := FQR.Data;
  187. end;
  188. function TQRBitmap.GetECL: TErrorCorrectionLevel;
  189. begin
  190. Result := FQR.ECL;
  191. end;
  192. function TQRBitmap.GetMask: TMask;
  193. begin
  194. Result := FQR.Mask;
  195. end;
  196. function TQRBitmap.GetText: string;
  197. begin
  198. Result := FQR.Text;
  199. end;
  200. function TQRBitmap.GetVersion: TVersion;
  201. begin
  202. Result := FQR.Version;
  203. end;
  204. procedure TQRBitmap.SetData(const Value: TArray<Byte>);
  205. begin
  206. FQR.Data := Value;
  207. end;
  208. procedure TQRBitmap.SetECL(const Value: TErrorCorrectionLevel);
  209. begin
  210. FQR.ECL := Value;
  211. end;
  212. procedure TQRBitmap.SetMask(const Value: TMask);
  213. begin
  214. FQR.Mask := Value;
  215. end;
  216. procedure TQRBitmap.SetShowBorder(const Value: boolean);
  217. begin
  218. FShowBorder := Value;
  219. FQR.Redraw;
  220. end;
  221. procedure TQRBitmap.SetShowModuleGrid(const Value: boolean);
  222. begin
  223. FShowModuleGrid := Value;
  224. FQR.Redraw;
  225. end;
  226. procedure TQRBitmap.SetText(const Value: string);
  227. begin
  228. FQR.Text := Value;
  229. end;
  230. procedure TQRBitmap.SetVersion(const Value: TVersion);
  231. begin
  232. FQR.Version := Value;
  233. end;
  234. end.