代码格式化错误
unit main;
{
* 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.
* Implemented by E. Spelt for Delphi
}
interface
uses
System.SysUtils,
System.Types,
System.UITypes,
System.Classes,
System.Variants,
System.Math.Vectors,
System.Actions,
System.Threading,
System.Permissions,
FMX.Types,
FMX.Controls,
FMX.Forms,
FMX.Graphics,
FMX.Dialogs,
FMX.Objects,
FMX.StdCtrls,
FMX.Media,
FMX.Platform,
FMX.MultiView,
FMX.ListView.Types,
FMX.ListView,
FMX.Layouts,
FMX.ActnList,
FMX.TabControl,
FMX.ListBox,
FMX.Controls.Presentation,
FMX.ScrollBox,
FMX.Memo,
FMX.Controls3D,
ZXing.BarcodeFormat,
ZXing.ReadResult,
ZXing.ScanManager, FMX.Memo.Types;
type
TMainForm = class(TForm)
btnStartCamera: TButton;
btnStopCamera: TButton;
lblScanStatus: TLabel;
imgCamera: TImage;
ToolBar1: TToolBar;
btnMenu: TButton;
Layout2: TLayout;
ToolBar3: TToolBar;
CameraComponent1: TCameraComponent;
Memo1: TMemo;
openDlg: TOpenDialog;
Camera1: TCamera;
procedure btnStartCameraClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnStopCameraClick(Sender: TObject);
procedure CameraComponent1SampleBufferReady(Sender: TObject;
const ATime: TMediaTime);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
fPermissionCamera: String;
fScanInProgress: Boolean;
fFrameTake: Integer;
fScanBitmap: TBitmap;
procedure ParseImage();
{$IF CompilerVersion >= 35.0}
// after Delphi 11 Alexandria
procedure CameraPermissionRequestResult(Sender: TObject;
const APermissions: TClassicStringDynArray;
const AGrantResults: TClassicPermissionStatusDynArray);
procedure ExplainReason(Sender: TObject; const APermissions: TClassicStringDynArray;
const APostRationaleProc: TProc);
{$ELSE}
// before Delphi 11 Alexandria
procedure CameraPermissionRequestResult(Sender: TObject;
const APermissions: TArray<string>;
const AGrantResults: TArray<TPermissionStatus>);
procedure ExplainReason(Sender: TObject; const APermissions: TArray<string>;
const APostRationaleProc: TProc);
{$ENDIF}
function AppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
end;
var
MainForm: TMainForm;
implementation
uses
{$IFDEF ANDROID}
Androidapi.Helpers,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.Os,
{$ENDIF}
FMX.DialogService;
{$R *.fmx}
procedure TMainForm.FormCreate(Sender: TObject);
var
AppEventSvc: IFMXApplicationEventService;
begin
if TPlatformServices.Current.SupportsPlatformService
(IFMXApplicationEventService, IInterface(AppEventSvc)) then
begin
AppEventSvc.SetApplicationEventHandler(AppEvent);
end;
lblScanStatus.Text := '';
fFrameTake := 0;
fScanBitmap := nil;
{$IFDEF ANDROID}
fPermissionCamera := JStringToString(TJManifest_permission.JavaClass.CAMERA);
{$ENDIF}
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
if Assigned(fScanBitmap) then
FreeAndNil(fScanBitmap);
end;
{$IF CompilerVersion >= 35.0}
// after Delphi 11 Alexandria
procedure TMainForm.CameraPermissionRequestResult(Sender: TObject;
const APermissions: TClassicStringDynArray;
const AGrantResults: TClassicPermissionStatusDynArray);
{$ELSE}
// before Delphi 11 Alexandria
procedure TMainForm.CameraPermissionRequestResult(Sender: TObject;
const APermissions: TArray<string>;
const AGrantResults: TArray<TPermissionStatus>);
{$ENDIF}
begin
if (Length(AGrantResults) = 1) and
(AGrantResults[0] = TPermissionStatus.Granted) then
begin
CameraComponent1.Active := false;
CameraComponent1.Quality := FMX.Media.TVideoCaptureQuality.MediumQuality;
CameraComponent1.Kind := FMX.Media.TCameraKind.BackCamera;
CameraComponent1.FocusMode := FMX.Media.TFocusMode.ContinuousAutoFocus;
CameraComponent1.Active := True;
lblScanStatus.Text := '';
Memo1.Lines.Clear;
end
else
TDialogService.ShowMessage
('Cannot scan for barcodes because the required permissions is not granted')
end;
{$IF CompilerVersion >= 35.0}
// after Delphi 11 Alexandria
procedure TMainForm.ExplainReason(Sender: TObject;
const APermissions: TClassicStringDynArray; const APostRationaleProc: TProc);
{$ELSE}
// before Delphi 11 Alexandria
procedure TMainForm.ExplainReason(Sender: TObject;
const APermissions: TArray<string>; const APostRationaleProc: TProc);
{$ENDIF}
begin
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);
fScanBitmap := TBitmap.Create();
fScanBitmap.Assign(imgCamera.Bitmap);
ParseImage();
end);
end;
procedure TMainForm.ParseImage();
begin
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;
end.
//这个是扫二维码那个zxing.delphi项目里的ZXing.Delphi-v_3.0\demo\aTestApp\main.pas的代码,
//下载地址为https://codeload.github.com/Spelt/ZXing.Delphi/zip/refs/heads/v_3.0
//按ctrl+w整理代码格式报错.提示如下:
//格式化错误(301,1):缺失有效符号。
//当前标识符:"End"
//如果该错误由附近的编译指令引发您可尝试在设置中修改编译指令处理方式后重试,
[ 本帖最后由 西方失败 于 2025-1-28 00:50 编辑 ]
|