c++ Zxing:区分128条码和GSI-128

2wnc66cl  于 2023-04-01  发布在  其他
关注(0)|答案(2)|浏览(380)

使用C++ Builder 10.1柏林,我正在开发一个在Android设备上扫描条形码的应用程序。为了获得条形码,我通过Intent调用ZXing应用程序。当我扫描GSI-128条形码时,返回类型是CODE-128。在我的应用程序中,根据条形码的类型(128和GSI-128),处理过程是不同的。
是否有方法区分这些类型的条形码?

rlcwz9us

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并处理结果:扫描条形码

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;
pxq42qpu

pxq42qpu2#

在使用startActivityForResult(intent,0);调用Intent之前,只需使用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);

相关问题