Bar Code Scanner in WINRT (Windows Store 8.1)

This article describes about how to create a bar code scanner, claim it for exclusive use, enable it to receive data and read a barcode.

This article describes about how to create a bar code scanner, claim it for exclusive use, enable it to receive data and read a barcode.

Open the Package.appxmanifest file in view code mode and add below capability.
  <Capabilities>
    <DeviceCapability Name="pointOfService" />
  </Capabilities>

1) Create barcode scanner:

Use the BarCodeScanner class, to represent the barcode scanner device.

BarCodeScanner uses the GetDefaultAsync() function to get the first available barcode scanner.

BarcodeScanner scanner=await BarcodeScanner.GetDefaultAsync();

2) Claim the Bar Code scanner for exclusive use:

After successful creation of barcode scanner, claim the scanner for exclusive use and enable it. So that data received events are received.

// claim the barcode scanner
ClaimedBarcodeScanner claimedScanner=await scanner.ClaimScannerAsync();

// enable the claimed barcode scanner
await claimedScanner.EnableAsync();

3) Add event handlers:

After successfully claiming, attach the data received event handler. This will be fired when a barcode is scanned by the barcode scanner.

claimedScanner.DataReceived += claimedScanner_DataReceived;
async void claimedScanner_DataReceived(ClaimedBarcodeScanner sender, BarcodeScannerDataReceivedEventArgs args)
        {
             // need to update the UI data on the dispatcher thread.
            // update the UI with the data received from the scan.
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
             {
                 // read the data from the buffer and convert to string.
                ScenarioOutputScanDataLabel.Text = GetDataLabelString(args.Report.ScanDataLabel, args.Report.ScanDataType);

                ScenarioOutputScanData.Text = GetDataString(args.Report.ScanData);

                ScenarioOutputScanDataType.Text = BarcodeSymbologies.GetName(args.Report.ScanDataType);
            });
        }

string GetDataLabelString(IBuffer data, uint scanDataType)
        {
            string result = null;
            // Only certain data types contain encoded text.
            //   To keep this simple, we'll just decode a few of them.
            if (data == null)
            {
                result = "No data";
            }
            else
            {
                switch (BarcodeSymbologies.GetName(scanDataType))
                {
                    case "Upca":
                    case "UpcaAdd2":
                    case "UpcaAdd5":
                    case "Upce":
                    case "UpceAdd2":
                    case "UpceAdd5":
                    case "Ean8":
                    case "TfStd":
                         // The UPC, EAN8, and 2 of 5 families encode the digits 0..9
                         // which are then sent to the app in a UTF8 string (like "01234")

                         // This is not an exhaustive list of symbologies that can be converted to a string

                        DataReader reader = DataReader.FromBuffer(data);
                        result = reader.ReadString(data.Length);
                          break;
                     default:
                          // Some other symbologies (typically 2-D symbologies) contain binary data that
                         //  should not be converted to text.
                        result = string.Format("Decoded data unavailable. Raw label data: {0}", GetDataString(data));
                          break;
                 }
             }

             return result;
        }

string GetDataString(IBuffer data)
        {
            StringBuilder result = new StringBuilder();

             if (data == null)
            {
                 result.Append("No data");
            }
            else
            {
                 // Just to show that we have the raw data, we'll print the value of the bytes.
                 // Arbitrarily limit the number of bytes printed to 20 so the UI isn't overloaded.
                const uint MAX_BYTES_TO_PRINT = 20;
                uint bytesToPrint = Math.Min(data.Length, MAX_BYTES_TO_PRINT);

                DataReader reader = DataReader.FromBuffer(data);
                byte[] dataBytes = new byte[bytesToPrint];
                 reader.ReadBytes(dataBytes);

                 for (uint byteIndex = 0; byteIndex < bytesToPrint; ++byteIndex)
                  {
                     result.AppendFormat("{0:X2} ", dataBytes[byteIndex]);
                 }

                 if (bytesToPrint < data.Length)
                 {
                      result.Append("...");
                 }
             }

             return result.ToString();
        }

Set IsDecodeDataEnabled to decode data by default.By setting this, API will decode the raw data from the barcode scanner and send the ScanDataLabel and ScanDataType in the DataReceived event.

claimedScanner.IsDecodeDataEnabled = true;

Download the working sample with various scenario's from below link.
http://nullskull.com/FileUpload/-407123783_BarCodeScanner.zip      

By Siva Jagan Dhulipalla   Popularity  (1984 Views)