user979331
user979331

Reputation: 11861

Xamarin Forms - ZXing Scan PDF417 Barcode - How to decode Results.Text

I have successfully scanned a PDF417 Barcode from a drivers licence and I have results in a string, my question is how would I decode this to an object? Has anyone done this before?

Here is my code:

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);
        }

I am able to get the Text results in the method Handle_OnScanResult, but now I want to convert that to an object.

Here is the string that gets returned:

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

From this barcode:

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

Here is my custom class I am trying to put data into:

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

Upvotes: 1

Views: 829

Answers (1)

Jason
Jason

Reputation: 89102

there's really no point into converting to JSON first. Just parse the data into your object

//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
    }
  }
}

Upvotes: 1

Related Questions