-
Notifications
You must be signed in to change notification settings - Fork 37
/
Data.Main.pas
479 lines (424 loc) · 12.2 KB
/
Data.Main.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
unit Data.Main;
interface
uses
System.SysUtils, System.Classes, ZXing.ScanManager, ZXing.BarcodeFormat,
ZXing.ReadResult, ZXing.ResultPoint, FMX.Types, FMX.Media, FMX.Graphics,
System.Messaging, System.Types, System.Diagnostics, FireDAC.Stan.Intf,
FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS,
FireDAC.Phys.Intf, FireDAC.DApt.Intf, Data.DB, FireDAC.Comp.DataSet,
FireDAC.Comp.Client;
const
SCAN_EACH_MS: Integer = 133;
type
TScanResult = class
private
FFrame: TBitmap;
FResult: TReadResult;
FProcessingTime: TStopWatch;
procedure SetFrame(const Value: TBitmap);
procedure SetProcessingTime(const Value: TStopWatch);
procedure SetResult(const Value: TReadResult);
public
constructor Create(const AFrame: TBitmap; const AResult: TReadResult);
destructor Destroy; override;
property Frame: TBitmap read FFrame write SetFrame;
property Result: TReadResult read FResult write SetResult;
property ProcessingTime: TStopWatch read FProcessingTime
write SetProcessingTime;
end;
TScanningMessage = Class(TMessage<Boolean>);
TScanResultMessage = Class(TObjectMessage<TScanResult>);
TScanPointMessage = Class(TMessage<TPointF>);
TScanSettingMessage = Class(TMessage<TVideoCaptureSetting>);
TTorchModeMessage = Class(TMessage<Boolean>);
TCameraBufferMessage = Class(TMessage<TBitmap>);
THardwareBackMessage = Class(TMessage<Integer>);
TMainDM = class(TDataModule)
DataTable: TFDMemTable;
DataTableScannedAt: TDateTimeField;
DataTableKind: TStringField;
DataTableValue: TStringField;
DataTableBitmap: TGraphicField;
DataTableThumb: TGraphicField;
DataTableScanCount: TIntegerField;
procedure CameraComponent1SampleBufferReady(Sender: TObject;
const ATime: TMediaTime);
procedure DataModuleCreate(Sender: TObject);
private
{ Private declarations }
FCameraComponent: TCameraComponent;
FLastScan: TDateTime;
FScanning: Boolean;
FWaitingResult: Boolean;
function CropBitmap(const ABitmap: TBitmap): TBitmap;
procedure ScanFrames(const AReducedBuffer: TBitmap);
procedure SetCameraSettings(ACameraComponent: TCameraComponent);
procedure OnResultPointHandler(const APoint: IResultPoint);
function GetTorchModeOn: Boolean;
public
{ Public declarations }
procedure Scan(const ABitmap: TBitmap; const ACropBitmap: Boolean = True); overload;
procedure Scan(const AFileName: string); overload;
procedure StartScanning;
procedure StopScanning;
procedure ToggleTorch;
property Scanning: Boolean read FScanning;
property TorchModeOn: Boolean read GetTorchModeOn;
end;
var
MainDM: TMainDM;
function BarcodeToString(const ABarcode: TBarcodeFormat): string;
procedure Beep;
implementation
{%CLASSGROUP 'FMX.Controls.TControl'}
{$R *.dfm}
uses
System.Threading, DateUtils, Rtti, TypInfo, IOUtils, Variants
{$IFDEF ANDROID}
, Androidapi.JNI.Media, Androidapi.JNI.JavaTypes
{$ENDIF}
, FMX.Ani, UITypes;
function BarcodeToString(const ABarcode: TBarcodeFormat): string;
begin
Result := '';
case ABarcode of
Auto:
Result := '*';
AZTEC:
Result := 'AZTEC';
CODABAR:
Result := 'CODABAR';
CODE_39:
Result := 'CODE_39';
CODE_93:
Result := 'CODE_93';
CODE_128:
Result := 'CODE_128';
DATA_MATRIX:
Result := 'DATA_MATRIX';
EAN_8:
Result := 'EAN_8';
EAN_13:
Result := 'EAN_13';
ITF:
Result := 'ITF';
MAXICODE:
Result := 'MAXICODE';
PDF_417:
Result := 'PDF_417';
QR_CODE:
Result := 'QR_CODE';
RSS_14:
Result := 'RSS_14';
RSS_EXPANDED:
Result := 'RSS_EXPANDED';
UPC_A:
Result := 'UPC_A';
UPC_E:
Result := 'UPC_E';
UPC_EAN_EXTENSION:
Result := 'UPC_EAN_EXTENSION';
MSI:
Result := 'MSI';
PLESSEY:
Result := 'PLESSEY';
end;
end;
procedure Beep;
{$IFDEF ANDROID}
var
Volume: Integer;
StreamType: Integer;
ToneType: Integer;
ToneGenerator: JToneGenerator;
{$ENDIF}
begin
{$IFDEF ANDROID}
Volume := TJToneGenerator.JavaClass.MAX_VOLUME;
StreamType := TJAudioManager.JavaClass.STREAM_ALARM;
ToneType := TJToneGenerator.JavaClass.TONE_CDMA_EMERGENCY_RINGBACK;
try
ToneGenerator := TJToneGenerator.JavaClass.init(StreamType, Volume);
ToneGenerator.startTone(ToneType, 100);
finally
ToneGenerator.release;
end;
{$ENDIF}
end;
procedure TMainDM.CameraComponent1SampleBufferReady(Sender: TObject;
const ATime: TMediaTime);
begin
if Sender is TCameraComponent then
begin
TThread.Synchronize(nil,
procedure
var
LBuffer, LReducedBuffer: TBitmap;
begin
LBuffer := TBitmap.Create;
try
TCameraComponent(Sender).SampleBufferToBitmap(LBuffer, True);
LReducedBuffer := CropBitmap(LBuffer);
try
TMessageManager.DefaultManager.SendMessage(Sender,
TCameraBufferMessage.Create(LReducedBuffer));
if FWaitingResult and (MilliSecondsBetween(FLastScan, Now) >=
SCAN_EACH_MS) then
ScanFrames(LReducedBuffer);
finally
if Assigned(LReducedBuffer) then
FreeAndnil(LReducedBuffer);
end;
finally
LBuffer.Free;
end;
end);
end;
end;
function TMainDM.CropBitmap(const ABitmap: TBitmap): TBitmap;
var
LCropW, LCropH, LCropMargin: Integer;
begin
LCropMargin := Round(Abs(ABitmap.Width - ABitmap.Height) / 2);
LCropW := LCropMargin;
LCropH := 0;
if ABitmap.Width < ABitmap.Height then
begin
LCropW := 0;
LCropH := LCropMargin;
end;
Result := TBitmap.Create(ABitmap.Width - (2 * LCropW),
ABitmap.Height - (2 * LCropH));
Result.CopyFromBitmap(ABitmap, Rect(LCropW, LCropH, ABitmap.Width - LCropW,
ABitmap.Height - LCropH), 0, 0);
end;
procedure TMainDM.DataModuleCreate(Sender: TObject);
begin
FLastScan := Now;
FScanning := False;
FWaitingResult := False;
TMessageManager.DefaultManager.SubscribeToMessage(TScanResultMessage,
procedure(const Sender: TObject; const M: TMessage)
const
POINT_SIZE = 3;
var
LValue: TScanResult;
LFrameWithPoints: TBitmap;
LPoint: IResultPoint;
LThumb: TBitmap;
LKind, LText: string;
begin
LValue := (M as TScanResultMessage).Value;
LKind := BarcodeToString(LValue.Result.BarcodeFormat);
LText := LValue.Result.ToString;
if DataTable.Locate('Kind;Value', VarArrayOf([LKind, LText])) then
begin
DataTable.Edit;
try
DataTable.FieldByName('ScanCount').AsInteger :=
DataTable.FieldByName('ScanCount').AsInteger + 1;
except
DataTable.Cancel;
raise;
end;
end
else
begin // add new record
LFrameWithPoints := TBitmap.Create(LValue.Frame.Width,
LValue.Frame.Height);
try
LFrameWithPoints.CopyFromBitmap(LValue.Frame);
LFrameWithPoints.Canvas.BeginScene();
try
LFrameWithPoints.Canvas.Fill.Color := TAlphaColorRec.Lime;
for LPoint in LValue.Result.resultPoints do
begin
LFrameWithPoints.Canvas.FillRect(RectF(LPoint.x - POINT_SIZE,
LPoint.y - POINT_SIZE, LPoint.x + POINT_SIZE,
LPoint.y + POINT_SIZE), 0, 0, [TCorner.TopLeft,
TCorner.TopRight, TCorner.BottomLeft,
TCorner.BottomRight], 0.8);
end;
finally
LFrameWithPoints.Canvas.EndScene();
end;
LThumb := LValue.Frame.CreateThumbnail(100, 100);
try
DataTable.AppendRecord([LValue.Result.timeStamp, LKind, LText,
LThumb, LFrameWithPoints, 1]);
finally
LThumb.Free;
end;
finally
LFrameWithPoints.Free;
end;
end;
end);
end;
function TMainDM.GetTorchModeOn: Boolean;
begin
Result := Assigned(FCameraComponent) and (FCameraComponent.TorchMode = TTorchMode.ModeOn);
end;
procedure TMainDM.OnResultPointHandler(const APoint: IResultPoint);
begin
TThread.ForceQueue(
nil
, procedure
begin
TMessageManager.DefaultManager.SendMessage(Self,
TScanPointMessage.Create(PointF(APoint.x, APoint.y)));
end
);
end;
procedure TMainDM.Scan(const ABitmap: TBitmap;
const ACropBitmap: Boolean = True);
var
LReducedBuffer: TBitmap;
begin
if ACropBitmap then
LReducedBuffer := CropBitmap(ABitmap)
else
begin
LReducedBuffer := TBitmap.Create(ABitmap.Width, ABitmap.Height);
try
LReducedBuffer.CopyFromBitmap(ABitmap);
except
LReducedBuffer.Free;
raise;
end;
end;
FWaitingResult := True;
ScanFrames(LReducedBuffer);
end;
procedure TMainDM.Scan(const AFileName: string);
var
LBitmap: TBitmap;
begin
LBitmap := TBitmap.Create;
try
LBitmap.LoadFromFile(AFileName);
Scan(LBitmap, False);
finally
LBitmap.Free;
end;
end;
procedure TMainDM.ScanFrames(const AReducedBuffer: TBitmap);
var
LReadResult: TReadResult;
LScanManager: TScanManager;
begin
if not Assigned(AReducedBuffer) or (AReducedBuffer.Width < 2) then
Exit;
FLastScan := Now;
if (not FWaitingResult) or (not FScanning) then // already found
Exit;
LScanManager := TScanManager.Create(TBarcodeFormat.Auto, nil);
try
{$IFNDEF IOS}
LScanManager.OnResultPoint := OnResultPointHandler;
{$ENDIF IOS}
LReadResult := nil;
try
LReadResult := LScanManager.Scan(AReducedBuffer);
except
LReadResult := nil;
end;
if (LReadResult <> nil) and FWaitingResult then
begin
TThread.Synchronize(nil,
procedure
begin
StopScanning;
TMessageManager.DefaultManager.SendMessage(Self,
TScanResultMessage.Create(TScanResult.Create(AReducedBuffer,
LReadResult)));
end);
end;
finally
FreeAndNil(LScanManager);
if Assigned(LReadResult) then
FreeAndNil(LReadResult);
end;
end;
procedure TMainDM.SetCameraSettings(ACameraComponent: TCameraComponent);
var
LSetting: TVideoCaptureSetting;
begin
for LSetting in ACameraComponent.AvailableCaptureSettings do
begin
if (LSetting.FrameRate >= 24) and (LSetting.Width <= 700) then
begin
ACameraComponent.SetCaptureSetting(LSetting);
TMessageManager.DefaultManager.SendMessage(Self,
TScanSettingMessage.Create(LSetting));
Break;
end;
end;
ACameraComponent.FocusMode := TFocusMode.ContinuousAutoFocus;
ACameraComponent.CaptureSettingPriority :=
TVideoCaptureSettingPriority.FrameRate;
end;
procedure TMainDM.StartScanning;
begin
if not Assigned(FCameraComponent) then
begin
FCameraComponent := TCameraComponent.Create(nil);
SetCameraSettings(FCameraComponent);
FCameraComponent.OnSampleBufferReady := CameraComponent1SampleBufferReady;
FCameraComponent.Active := True;
end;
FScanning := True;
FWaitingResult := True;
TMessageManager.DefaultManager.SendMessage(Self, TScanningMessage.Create(True));
end;
procedure TMainDM.StopScanning;
begin
FWaitingResult := False;
FScanning := False;
if Assigned(FCameraComponent) then
begin
FCameraComponent.OnSampleBufferReady := nil;
{$IFDEF MSWINDOWS}
//FCameraComponent.Active := False;
{$ENDIF}
FreeAndNil(FCameraComponent);
end;
TMessageManager.DefaultManager.SendMessage(Self, TScanningMessage.Create(False));
end;
procedure TMainDM.ToggleTorch;
begin
if Assigned(FCameraComponent) then
begin
if FCameraComponent.TorchMode = TTorchMode.ModeOff then
FCameraComponent.TorchMode := TTorchMode.ModeOn
else
FCameraComponent.TorchMode := TTorchMode.ModeOff;
end;
TMessageManager.DefaultManager.SendMessage(Self, TTorchModeMessage.Create(TorchModeOn));
end;
{ TScanResult }
constructor TScanResult.Create(const AFrame: TBitmap;
const AResult: TReadResult);
begin
inherited Create;
FFrame := AFrame;
FResult := AResult;
end;
destructor TScanResult.Destroy;
begin
FreeAndNil(FFrame);
inherited;
end;
procedure TScanResult.SetFrame(const Value: TBitmap);
begin
FFrame := Value;
end;
procedure TScanResult.SetProcessingTime(const Value: TStopWatch);
begin
FProcessingTime := Value;
end;
procedure TScanResult.SetResult(const Value: TReadResult);
begin
FResult := Value;
end;
end.