2016-09-16 326 views
0

使用C++ Builder 10.1柏林,我正在開發一款應用程序來掃描Android設備上的條形碼。爲了獲得條形碼,我通過Intent調用ZXing應用程序。當我掃描GSI-128條形碼時,返回類型是CODE-128。在我的應用程序中,根據條碼類型(128和GSI-128)的不同,處理過程會有所不同。Zxing:區分128條碼和GSI-128

有沒有一種方法來區分這些類型的條形碼?

回答

0

在調用與startActivityForResult(intent,0);意圖,只是intent->putExtra(StringToJString("ASSUME_GS1"), true);添加參數。如果條形碼是GS1-128,則前綴「] C1」將添加到您的條形碼中。

_di_JIntent intent; 
msgID = TMessageManager::DefaultManager->SubscribeToMessage(__classid(TMessageResultNotification), &retourScanTel); 
intent = TJIntent::Create(); 
intent->setAction(StringToJString("com.google.zxing.client.android.SCAN")); 
intent->putExtra(StringToJString("ASSUME_GS1"), true); 
SharedActivity()->startActivityForResult(intent,0); 
0

見斑馬線的文檔:

Scanning Via Intent

斑馬線提供IntentIntegrator類來啓動掃描,並解析結果。條形碼類型位於ScanResults.formatName字段中。如果你不想使用IntentIntegrator,你將不得不手動解析結果Intent。條形碼類型可在Intent的SCAN_RESULT_FORMAT額外的字符串字段中找到。

無論哪種方式,您都可以使用FireMonkey的messaging framework接收結果Intent。它將被包裹在TMessageResultNotification消息中。

下面是布賴恩龍的博客在Delphi斑馬線掃描的例子:

Launching activities and handling results in Delphi XE6 Android apps: Scanning a bar code

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; 
+0

感謝雷米,但我已經做了這一點,但兩個128和GSI-128條形碼,在'SCAN_RESULT_FORMAT'的價值是相同的('CODE-128')。 – Dylan