function LaunchActivityForResult(const Intent: JIntent; RequestCode: Integer): Boolean;
var
ResolveInfo: JResolveInfo;
begin
ResolveInfo := SharedActivity.getPackageManager.resolveActivity(Intent, 0);
Result := ResolveInfo <> nil;
if Result then
SharedActivity.startActivityForResult(Intent, RequestCode);
end;
//For more info see https://github.com/zxing/zxing/wiki/Scanning-Via-Intent
procedure LaunchQRScanner(RequestCode: Integer);
var
Intent: JIntent;
begin
Intent := TJIntent.JavaClass.init(StringToJString('com.google.zxing.client.android.SCAN'));
Intent.setPackage(StringToJString('com.google.zxing.client.android'));
// If you want to target QR codes
//Intent.putExtra(StringToJString('SCAN_MODE'), StringToJString('QR_CODE_MODE'));
if not LaunchActivityForResult(Intent, RequestCode) then
Toast('Cannot display QR scanner', ShortToast);
end;
uses
System.Messaging,
...
type
TMainForm = class(TForm)
...
private
const ScanRequestCode = 0;
var FMessageSubscriptionID: Integer;
procedure HandleActivityMessage(const Sender: TObject; const M: TMessage);
function OnActivityResult(RequestCode, ResultCode: Integer; Data: JIntent): Boolean;
...
end;
...
uses
FMX.Platform.Android,
Androidapi.Helpers,
Androidapi.JNI.App,
Androidapi.JNI.Toast,
LaunchActivities,
...
procedure TMainForm.BarcodeScannerButtonClick(Sender: TObject);
begin
FMessageSubscriptionID := TMessageManager.DefaultManager.SubscribeToMessage(TMessageResultNotification,
HandleActivityMessage);
LaunchQRScanner(ScanRequestCode);
end;
procedure TMainForm.HandleActivityMessage(const Sender: TObject; const M: TMessage);
begin
if M is TMessageResultNotification then
OnActivityResult(TMessageResultNotification(M).RequestCode, TMessageResultNotification(M).ResultCode,
TMessageResultNotification(M).Value);
end;
function TMainForm.OnActivityResult(RequestCode, ResultCode: Integer; Data: JIntent): Boolean;
var
ScanContent, ScanFormat: string;
begin
Result := False;
TMessageManager.DefaultManager.Unsubscribe(TMessageResultNotification, FMessageSubscriptionID);
FMessageSubscriptionID := 0;
// For more info see https://github.com/zxing/zxing/wiki/Scanning-Via-Intent
if RequestCode = ScanRequestCode then
begin
if ResultCode = TJActivity.JavaClass.RESULT_OK then
begin
if Assigned(Data) then
begin
ScanContent := JStringToString(Data.getStringExtra(StringToJString('SCAN_RESULT')));
ScanFormat := JStringToString(Data.getStringExtra(StringToJString('SCAN_RESULT_FORMAT')));
Toast(Format('Found %s format barcode:'#10'%s', [ScanFormat, ScanContent]), LongToast);
end;
end
else if ResultCode = TJActivity.JavaClass.RESULT_CANCELED then
begin
Toast('You cancelled the scan', ShortToast);
end;
Result := True;
end;
end;
2条答案
按热度按时间rlcwz9us1#
参见ZXing的文档:
Scanning Via Intent
ZXing提供了一个
IntentIntegrator
类来启动扫描并解析结果。条形码类型在ScanResults.formatName
字段中。如果不想使用
IntentIntegrator
,则必须手动解析结果Intent
。条形码类型在Intent的SCAN_RESULT_FORMAT
额外字符串字段中可用。无论哪种方式,您都可以使用FireMonkey的messaging framework来接收结果
Intent
。它将被 Package 在TMessageResultNotification
消息中。以下是Brian Long的博客中的一个 Delphi 中的ZXing扫描示例:
在 Delphi XE6 Android应用程序中启动Activity并处理结果:扫描条形码
pxq42qpu2#
在使用
startActivityForResult(intent,0);
调用Intent之前,只需使用intent->putExtra(StringToJString("ASSUME_GS1"), true);
添加一个参数。如果条形码是GS1-128,则会在条形码处添加前缀“]C1”。