-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
zint_qr_epc.pas
318 lines (288 loc) · 8.75 KB
/
zint_qr_epc.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
unit zint_qr_epc;
interface
//https://de.wikipedia.org/wiki/EPC-QR-Code
//https://github.com/mtgrosser/girocode
uses
System.SysUtils,System.Classes,System.Types,System.StrUtils
,Vcl.Graphics
,zint,zint_render_wmf, zint_render_canvas, zint_render_svg,zint_helper,zint_qr
;
//TODO Support all Character-Sets, currently only UTF8
type
TZintEPCQR = class(TObject)
public type
TVersion = (epcqrVersion_1, //Version (001 or 002)
epcqrVersion_2);
TCharacterSet = (epcqrCharacterSet_UTF8, //Character-Sets (1= UTF-8, 2 = ISO 8859-1, 3 = ISO 8859-2, 4 = ISO 8859-4, 5 = ISO 8859-5, 6 = ISO 8859-7, 7 = ISO 8859-10, 8 = ISO 8859-15)
epcqrCharacterSet_ISO_8859_1,
epcqrCharacterSet_ISO_8859_2,
epcqrCharacterSet_ISO_8859_4,
epcqrCharacterSet_ISO_8859_5,
epcqrCharacterSet_ISO_8859_7,
epcqrCharacterSet_ISO_8859_10,
epcqrCharacterSet_ISO_8859_15);
private
FInformation: String;
FName: String;
FCharacterSet: TZintEPCQR.TCharacterSet;
FVersion: TZintEPCQR.TVersion;
FServiceTag: String;
FIdentification: String;
FBIC: String;
FAmount: Currency;
FIBAN: String;
FReference: String;
FPurpose: String;
FPurposeOfPayment: String;
procedure SetAmount(const Value: Currency);
procedure SetBIC(const Value: String);
procedure SetCharacterSet(const Value: TZintEPCQR.TCharacterSet);
procedure SetIBAN(const Value: String);
procedure SetInformation(const Value: String);
procedure SetName(const Value: String);
procedure SetReference(const Value: String);
procedure SetVersion(const Value: TZintEPCQR.TVersion);
procedure SetPurpose(const Value: String);
procedure SetPurposeOfPayment(const Value: String);
function GetVersionAsString : String;
function GetCharacterSetAsString : String;
function GetNameAsString : String;
function GetAmountAsString : String;
function GetPurposeOfPaymentAsString : String;
function GetInformationAsString : String;
function IsValidSEPACharSet(const AValue : String) : Boolean;
public
constructor Create;
procedure Clear;
function GetQRCode(const AWidth, AHeight : Integer;out AImage : TMetafile; out AErrorCode : Integer) : Boolean;
public
property ServiceTag : String read FServiceTag; //BCD
property Version : TZintEPCQR.TVersion read FVersion write SetVersion; //001
property CharacterSet : TZintEPCQR.TCharacterSet read FCharacterSet write SetCharacterSet; //1
property Identification : String read FIdentification; // SCT
property BIC : String read FBIC write SetBIC; // BPOTBEB1 optional in EWR https://de.wikipedia.org/wiki/Europ%C3%A4ischer_Wirtschaftsraum
property Name : String read FName write SetName; // Red Cross of Belgium max 70 Chars
property IBAN : String read FIBAN write SetIBAN; // BE72000000001616
property Amount : Currency read FAmount write SetAmount; // EUR1
property Purpose : String read FPurpose write SetPurpose; //optional, max 4 Chars
property Reference : String read FReference write SetReference; // RF18 5390 0754 7034 Referenz (strukturierter 35-Zeichen-Code gem. ISO 11649 RF Creditor Reference)
property PurposeOfPayment : String read FPurposeOfPayment write SetPurposeOfPayment; //wenn, dann kein Reference, Verwendungszweck (unstrukturierter maximal 140 Zeichen langer Text)
property Information : String read FInformation write SetInformation; // optional info Sample QR code
end;
implementation
{ TZintEPCQR }
constructor TZintEPCQR.Create;
begin
Clear;
end;
function TZintEPCQR.GetAmountAsString: String;
begin
Result := Format('%n',[FAmount]);
Result := 'EUR'+ReplaceText(Result,',','.');
end;
function TZintEPCQR.GetCharacterSetAsString: String;
begin
case FCharacterSet of
epcqrCharacterSet_ISO_8859_1: Result := '2';
epcqrCharacterSet_ISO_8859_2: Result := '3';
epcqrCharacterSet_ISO_8859_4: Result := '4';
epcqrCharacterSet_ISO_8859_5: Result := '5';
epcqrCharacterSet_ISO_8859_7: Result := '6';
epcqrCharacterSet_ISO_8859_10: Result := '7';
epcqrCharacterSet_ISO_8859_15: Result := '8';
else Result := '1'; //epcqrCharacterSet_UTF8: ;
end;
end;
function TZintEPCQR.GetInformationAsString: String;
begin
Result := FInformation;
end;
function TZintEPCQR.GetNameAsString: String;
begin
Result := FName;
end;
function TZintEPCQR.GetPurposeOfPaymentAsString: String;
begin
Result := FPurposeOfPayment;
end;
function TZintEPCQR.GetQRCode(const AWidth, AHeight : Integer;
out AImage: TMetafile; out AErrorCode: Integer): Boolean;
var
symbol : TZintSymbol;
rt : TZintRenderTargetWMF;
wmf : TMetaFile;
data : String;
begin
Result := false;
AErrorCode := -1;
if (not IsValidSEPACharSet(FName)) or
(not IsValidSEPACharSet(FPurposeOfPayment)) or
(not IsValidSEPACharSet(FInformation)) then
exit;
if not (Length(Trim(FName)) in [1..70]) then
begin
AErrorCode := -2;
exit;
end;
if (Length(Trim(FIBAN)) = 0) then
begin
AErrorCode := -3;
exit;
end;
if (FAmount <= 0) then
begin
AErrorCode := -4;
exit;
end;
if Trim(FPurpose) <> '' then
if Length(Trim(FPurpose)) <> 4 then
begin
AErrorCode := -5;
exit;
end;
if (Trim(FReference) <> '') and (Trim(FPurposeOfPayment) <> '') then
begin
AErrorCode := -6;
exit;
end;
if (Trim(FReference) <> '') and (not FReference.StartsWith('RF',true)) then
begin
AErrorCode := -7;
exit;
end;
if (Trim(FReference) = '') then
if not (Length(Trim(FPurposeOfPayment)) in [1..140]) then
begin
AErrorCode := -8;
exit;
end;
wmf := TMetafile.Create;
symbol :=TZintSymbol.Create(nil);
rt := TZintRenderTargetWMF.Create(nil);
try
try
data := FServiceTag +#10+
GetVersionAsString+#10+
GetCharacterSetAsString+#10+
FIdentification+#10+
FBIC+#10+
GetNameAsString+#10+
FIBAN+#10+
GetAmountAsString+#10+
FPurpose+#10+
FReference+#10+
GetPurposeOfPaymentAsString+#10+
GetInformationAsString;
symbol.SymbolType := zsQRCODE;
symbol.QRCodeOptions.ECCLevel := qreLevelM;
symbol.QRCodeOptions.Size := qrsAuto;
symbol.input_mode := UNICODE_MODE;
symbol.Encode(data, true);
symbol.primary := StrToArrayOfChar('');
wmf.SetSize(AWidth,AHeight);
rt.HexagonScale:= 1;
rt.RenderAdjustMode:=TZintRenderAdjustMode.ramScale;
rt.ShowText := false;
rt.Metafile := wmf;
rt.MinModuleWidth := 0;
rt.MaxModuleWidth := 0;
Symbol.Render(rt);
AImage := wmf;
wmf := nil;
AErrorCode := 0;
Result := true;
except
on E:Exception do
begin
AErrorCode := -9;
end;
end;
finally
symbol.Free;
rt.Free;
if Assigned(wmf) then
wmf.Free;
end;
end;
function TZintEPCQR.GetVersionAsString: String;
begin
case FVersion of
epcqrVersion_2 : Result := '002';
else Result := '001';
end;
end;
function TZintEPCQR.IsValidSEPACharSet(const AValue: String): Boolean;
var
i : Integer;
begin
Result := true;
for i := 1 to Length(AValue) do
begin
if CharInSet(AValue[i],['a'..'z']) then
continue;
if CharInSet(AValue[i],['A'..'Z']) then
continue;
if CharInSet(AValue[i],['0'..'9']) then
continue;
if CharInSet(AValue[i],['/', '?', ':', '(', ')', '.', ',', '''', '+', '-', ' ']) then
continue;
Result := false;
break;
end;
end;
procedure TZintEPCQR.Clear;
begin
FServiceTag := 'BCD';
FVersion := TZintEPCQR.TVersion.epcqrVersion_2;
FCharacterSet := TZintEPCQR.TCharacterSet.epcqrCharacterSet_UTF8;
FIdentification := 'SCT';
FBIC := '';
FName := '';
FIBAN := '';
FAmount := 0;
FPurpose := '';
FReference := '';
FPurposeOfPayment := '';
FInformation := '';
end;
procedure TZintEPCQR.SetAmount(const Value: Currency);
begin
FAmount := Value;
end;
procedure TZintEPCQR.SetBIC(const Value: String);
begin
FBIC := Value;
end;
procedure TZintEPCQR.SetCharacterSet(const Value: TZintEPCQR.TCharacterSet);
begin
FCharacterSet := Value;
end;
procedure TZintEPCQR.SetIBAN(const Value: String);
begin
FIBAN := Value;
end;
procedure TZintEPCQR.SetInformation(const Value: String);
begin
FInformation := Value;
end;
procedure TZintEPCQR.SetName(const Value: String);
begin
FName := Value;
end;
procedure TZintEPCQR.SetPurpose(const Value: String);
begin
FPurpose := Value;
end;
procedure TZintEPCQR.SetPurposeOfPayment(const Value: String);
begin
FPurposeOfPayment := Value;
end;
procedure TZintEPCQR.SetReference(const Value: String);
begin
FReference := Value;
end;
procedure TZintEPCQR.SetVersion(const Value: TZintEPCQR.TVersion);
begin
FVersion := Value;
end;
end.