unit uBarcodeScannerIntent;
interface
Uses
FMX.
platform,
{$IFDEF ANDROID}
fmx.helpers.android, androidapi.JNI.GraphicsContentViewText, androidapi.jni.JavaTypes,
androidapi.helpers,androidapi.jni.App,
{$ENDIF}
classes,types,FMX.types, system.Messaging;
type
TBarcodeScannerIntent =
class(TComponent)
private
fbarcode:
String;
fOnBarcodeScanned: TNotifyevent;
fOnScanCanceled: TNotifyevent;
fCodeType:
String;
fBarcodeFormat:
String;
const SCAN_REQUEST_CODE = 0;
var FMessageSubscriptionID: Integer;
procedure HandleActivityMessage(
const Sender: TObject;
const M: TMessage);
{$IFDEF ANDROID}
function OnActivityResult(RequestCode, ResultCode: Integer; Data: JIntent): Boolean;
{$ENDIF}
public
Constructor Create(aOwner : TComponent);
Override;
Procedure Scan;
Property CodeType:
String read fCodeType
write fCodetype;
property Barcode:
String read fbarcode;
property BarcodeFormat:
String read fBarcodeFormat
Write fBarcodeFormat;
property OnBarcodeScanned:TNotifyevent
read fOnBarcodeScanned
write fOnbarcodescanned;
property OnScanCanceled:TNotifyevent
read fOnScanCanceled
write fOnScanCanceled;
end;
implementation
{ TBarcodeScannerIntent }
constructor TBarcodeScannerIntent.create(aOwner: TComponent);
begin
inherited;
fCodetype := '
';
fbarcode := '
';
fBarcodeFormat := '
';
end;
procedure TBarcodeScannerIntent.HandleActivityMessage(
const Sender: TObject;
const M: TMessage);
begin
{$IFDEF ANDROID}
if M
is TMessageResultNotification
then
OnActivityResult(
TMessageResultNotification(M).RequestCode,
TMessageResultNotification(M).ResultCode,
TMessageResultNotification(M).Value);
{$ENDIF}
end;
{$IFDEF ANDROID}
function TBarcodeScannerIntent.OnActivityResult(RequestCode,
ResultCode: Integer; Data: JIntent): Boolean;
var
LScanContent, LScanFormat:
string;
begin
Result := False;
TMessageManager.DefaultManager.Unsubscribe(TMessageResultNotification, FMessageSubscriptionID);
FMessageSubscriptionID := 0;
if RequestCode = SCAN_REQUEST_CODE
then
begin
if ResultCode = TJActivity.JavaClass.RESULT_OK
then
begin
Result := True;
if Assigned(Data)
then
begin
LScanContent := JStringToString(Data.getStringExtra(StringToJString('
SCAN_RESULT')));
LScanFormat := JStringToString(Data.getStringExtra(StringToJString('
SCAN_RESULT_FORMAT')));
TThread.Synchronize(
nil,
procedure
begin
fbarcode := LScanContent;
fBarcodeFormat := LScanFormat;
if assigned(fOnBarcodeScanned)
then
fonbarcodescanned(self);
end
);
end;
end
else
Begin
fbarcode := '
';
fBarcodeFormat := '
';
if assigned(fOnScanCanceled)
then
fOnScanCanceled(self);
end;
end;
end;
{$ENDIF}
procedure TBarcodeScannerIntent.Scan;
{$IFDEF ANDROID}
var intent: jintent;
{$ENDIF}
begin
{$IFDEF ANDROID}
// Callback registrieren
FMessageSubscriptionID := TMessageManager.DefaultManager.SubscribeToMessage(TMessageResultNotification, HandleActivityMessage);
fBarcodeFormat := '
';
fbarcode:= '
';
Intent := TJIntent.JavaClass.init(StringToJString('
com.google.zxing.client.android.SCAN'));
Intent.setPackage(StringToJString('
com.google.zxing.client.android'));
if fCodetype <> '
'
then
intent.putExtra(stringtojstring('
SCAN_MODE'),stringtojstring(fCodetype));
sharedactivity.startActivityForResult(intent,SCAN_REQUEST_CODE);
{$ENDIF}
end;
end.