{
* Copyright 2015 E Spelt for test project stuff
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
procedure TMainForm.FormCreate(Sender: TObject);
var
AppEventSvc: IFMXApplicationEventService;
begin
if TPlatformServices.Current.SupportsPlatformService
(IFMXApplicationEventService, IInterface(AppEventSvc)) then
begin
AppEventSvc.SetApplicationEventHandler(AppEvent);
end;
TDialogService.ShowMessage
('The app needs to access the camera to scan barcodes ...',
procedure(const AResult: TModalResult)
begin
APostRationaleProc;
end)
end;
procedure TMainForm.btnStartCameraClick(Sender: TObject);
begin
PermissionsService.RequestPermissions([fPermissionCamera],
CameraPermissionRequestResult, ExplainReason);
end;
procedure TMainForm.btnStopCameraClick(Sender: TObject);
begin
CameraComponent1.Active := false;
end;
procedure TMainForm.CameraComponent1SampleBufferReady(Sender: TObject; const ATime: TMediaTime);
begin
TThread.Synchronize(TThread.CurrentThread,
procedure
begin
CameraComponent1.SampleBufferToBitmap(imgCamera.Bitmap, True);
if (fScanInProgress) then
begin
exit;
end;
{ This code will take every 4 frame. }
inc(fFrameTake);
if (fFrameTake mod 4 <> 0) then
begin
exit;
end;
if Assigned(fScanBitmap) then
FreeAndNil(fScanBitmap);
TThread.CreateAnonymousThread(
procedure
var
ReadResult: TReadResult;
ScanManager: TScanManager;
begin
fScanInProgress := True;
ScanManager := TScanManager.Create(TBarcodeFormat.Auto, nil);
try
try
ReadResult := ScanManager.Scan(fScanBitmap);
except
on E: Exception do
begin
TThread.Synchronize(TThread.CurrentThread,
procedure
begin
lblScanStatus.Text := E.Message;
end);
exit;
end;
end;
TThread.Synchronize(TThread.CurrentThread,
procedure
begin
if (Length(lblScanStatus.Text) > 10) then
begin
lblScanStatus.Text := '*';
end;
lblScanStatus.Text := lblScanStatus.Text + '*';
if (ReadResult <> nil) then
begin
Memo1.Lines.Insert(0, ReadResult.Text);
end;
end);
finally
if ReadResult <> nil then
FreeAndNil(ReadResult);
ScanManager.Free;
fScanInProgress := false;
end;
end).Start();
end;
{ Make sure the camera is released if you're going away. }
function TMainForm.AppEvent(AAppEvent: TApplicationEvent;
AContext: TObject): Boolean;
begin
case AAppEvent of
TApplicationEvent.WillBecomeInactive, TApplicationEvent.EnteredBackground,
TApplicationEvent.WillTerminate:
CameraComponent1.Active := false;
end;
end;