Xamarin表单-ZXing扫描PDF417条码-如何解码Results.Text

时间:2019-10-21 16:16:11

标签: c# xamarin.forms zxing

我已经从驾驶执照中成功扫描了PDF417条形码,并且得到了字符串结果,我的问题是如何将其解码为对象?有人做过吗?

这是我的代码:

public void Scan_Barcode(object sender, EventArgs e)
        {

            var options = new MobileBarcodeScanningOptions
            {
                TryHarder = true,
                CameraResolutionSelector = HandleCameraResolutionSelectorDelegate,
                PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.PDF_417 }
            };


            BarcodeScanView.Options = options;

            BarcodeScanView.IsVisible = true;
            BarcodeScanView.IsScanning = true;
        }

        public CameraResolution HandleCameraResolutionSelectorDelegate(List<CameraResolution> availableResolutions)
        {
            if (availableResolutions == null || availableResolutions.Count < 1)
                return new CameraResolution() { Width = 800, Height = 600 };

            return availableResolutions[availableResolutions.Count - 1];
        }

        public void Handle_OnScanResult(Result result)
        {
            Console.WriteLine(result.Text);
        }

我能够通过Handle_OnScanResult方法获得Text结果,但是现在我想将其转换为对象。

这是要返回的字符串:

  

“ @ \ n \ x1e \ rANSI   636000090002DL00410278ZV03190008DLDAQT64235789 \ nDCSSAMPLE \ nDDEN \ nDACMICHAEL \ nDDFN \ nDADJOHN \ nDDGN \ nDCUJR \ nDCAD \ nDCBK \ nDCDPH \ nDBD06062016 \ nDBB06061986 \ nDBA12102024 \ nDBC1068   in \ nDAYBRO \ nDAG2300 WEST BROAD   街道\ nDAIRICHMOND \ nDAJVA \ nDAK232690000   \ nDCF2424244747474786102204 \ nDCGUSA \ nDCK123456789 \ nDDAF \ nDDB06062008 \ nDDC06062009 \ nDDD1 \ rZVZVA01 \ r“

通过此条形码:

https://user-images.githubusercontent.com/482138/51589235-b638d500-1ee6-11e9-87f0-5acb9229b9a5.png

这是我尝试将数据放入的自定义类:

public class DriversLicenseClass
    {
        public DriversLicenseClass()
        {
        }

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime DOB { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string PostalCode { get; set; }
        public string DriversLicenceNumber { get; set; }
        public DateTime Issued { get; set; }
        public DateTime Expiry { get; set; }
        public string DD { get; set; }
        public string Height { get; set; }
    }

1 个答案:

答案 0 :(得分:0)

真的没有必要先转换为JSON。只需将数据解析为对象

//response is the decoded text from the barcode
var data = response.Split('\n');

foreach(var line in data)
{

  if (line.Length > 3) {
    var code = line.Substring(0,3);
    var value = line.Substring(4);

    switch (code) {
      case "DAB":  // last name
        LastName = value;
        break;
      case "DAC":  // first name
        FirstName = value;
        break;
      ... add other cases here
    }
  }
}