Kod: Tümünü seç
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, IPPeerClient,
IPPeerServer, System.Tether.Manager, System.Tether.AppProfile, FMX.Edit,
FMX.StdCtrls, FMX.Platform, FMX.Helpers.Android, Androidapi.Helpers,
AndroidApi.JNI.GraphicsContentViewText, Androidapi.Jni.JavaTypes, System.Rtti,
FMX.Controls.Presentation, FMX.ALDevBarcode;
type
TForm1 = class(TForm)
SpeedButton1: TSpeedButton;
Edit1: TEdit;
procedure SpeedButton1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
FBarcode: TALDevBarcode;
procedure BarcodeScanResult(Sender: TObject; AResult: string);
public
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
procedure TForm1.BarcodeScanResult(Sender: TObject; AResult: string);
begin
MessageDlg(AResult, TMsgDlgType.mtError, [TMsgDlgBtn.mbOK], 0, TMsgDlgBtn.mbOK,
procedure (const AResult: TModalResult)
begin
end
);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FBarcode := TALDevBarcode.Create(Self);
FBarcode.OnScanResult := BarcodeScanResult;
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
FBarcode.Scan;
Edit1.Text:= 'barkod'
end;
end.
Kod: Tümünü seç
unit FMX.ALDevBarcode;
interface
uses
FMX.Types, FMX.Platform, System.Classes, System.Rtti,
Androidapi.JNI.GraphicsContentViewText, Androidapi.Helpers;
type
TALDevBarcodeScanEvent = procedure (Sender: TObject; AResult: string) of object;
TALDevBarcode = class(TFmxObject)
private
FPreservedClipboardValue: TValue;
FMonitorClipboard: Boolean;
FClipService: IFMXClipboardService;
FOnScanResult: TALDevBarcodeScanEvent;
function HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
procedure DoScanResult(AValue: string);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Scan;
property OnScanResult: TALDevBarcodeScanEvent read FOnScanResult write FOnScanResult;
end;
implementation
{ TALDevBarcode }
constructor TALDevBarcode.Create(AOwner: TComponent);
var
aFMXApplicationEventService: IFMXApplicationEventService;
begin
inherited Create(AOwner);
FMonitorClipboard := False;
if not TPlatformServices.Current.SupportsPlatformService(IFMXClipboardService, IInterface(FClipService)) then
FClipService := nil;
if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, IInterface(aFMXApplicationEventService)) then
aFMXApplicationEventService.SetApplicationEventHandler(HandleAppEvent)
else
Log.d('Application Event Service is not supported.');
end;
destructor TALDevBarcode.Destroy;
begin
inherited Destroy;
end;
procedure TALDevBarcode.DoScanResult(AValue: string);
begin
if Assigned(FOnScanResult) then
FOnScanResult(Self, AValue);
end;
function TALDevBarcode.HandleAppEvent(AAppEvent: TApplicationEvent;
AContext: TObject): Boolean;
function GetBarcodeValue: Boolean;
var
value: String;
begin
Result := False;
FMonitorClipboard := False;
if (FClipService.GetClipboard.ToString <> 'nil') then
begin
DoScanResult(FClipService.GetClipboard.ToString);
FClipService.SetClipboard(FPreservedClipboardValue);
Result := True;
end;
end;
begin
Result := False;
if FMonitorClipboard and (AAppEvent = TApplicationEvent.BecameActive) then
Result := GetBarcodeValue;
end;
procedure TALDevBarcode.Scan;
var
intent: JIntent;
begin
if Assigned(FClipService) then
begin
FPreservedClipboardValue := FClipService.GetClipboard;
FMonitorClipboard := True;
FClipService.SetClipboard('nil');
intent := TJIntent.Create;
intent.setAction(StringToJString('com.google.zxing.client.android.SCAN'));
//intent.putExtras(TJIntent.JavaClass.EXTRA_TEXT, StringToJString('"SCAN_MODE", "CODE_39"'));
SharedActivity.startActivityForResult(intent, 0);
end;
end;
end.