Bewerken

Share via


Working with barcode scanner symbologies

This topic explains how to use the Universal Windows Platform (UWP) barcode scanner APIs to process barcode symbologies without manually configuring the scanner.

A barcode symbology is the mapping of data to a specific barcode format. Some common symbologies include UPC, Code 128, QR Code, and so on.

Determine which symbologies are supported

Since your application may be used with different barcode scanner models from multiple manufacturers, you may want to query the scanner to determine the list of symbologies that it supports. This can be useful if your application requires a specific symbology that may not be supported by all scanners or you need to enable symbologies that have been either manually or programmatically disabled on the scanner.

Once you have a BarcodeScanner object by using BarcodeScanner.FromIdAsync, call GetSupportedSymbologiesAsync to obtain a list of symbologies supported by the device.

The following example gets a list of the supported symbologies of the barcode scanner, and displays them in a text block:

private void DisplaySupportedSymbologies(BarcodeScanner barcodeScanner, TextBlock textBlock) 
{
    var supportedSymbologies = await barcodeScanner.GetSupportedSymbologiesAsync();

    foreach (uint item in supportedSymbologies)
    {
        string symbology = BarcodeSymbologies.GetName(item);
        textBlock.Text += (symbology + "\n");
    }
}

Determine if a specific symbology is supported

To determine if the scanner supports a specific symbology you can call IsSymbologySupportedAsync.

The following example checks if the barcode scanner supports the Code32 symbology:

bool symbologySupported = await barcodeScanner.IsSymbologySupportedAsync(BarcodeSymbologies.Code32);

Change which symbologies are recognized

In some cases, you may want to use a subset of symbologies that the barcode scanner supports. This is particularly useful to block symbologies that you do not intend to use in your application. For example, to ensure a user scans the right barcode, you could constrain scanning to UPC or EAN when acquiring item SKUs and constrain scanning to Code 128 when acquiring serial numbers.

Once you know the symbologies that your scanner supports, you can set the symbologies that you want it to recognize. This can be done after you have established a ClaimedBarcodeScanner object using ClaimScannerAsync. You can call SetActiveSymbologiesAsync to enable a specific set of symbologies while those omitted from your list are disabled.

The following example sets the active symbologies of a claimed barcode scanner to Code39 and Code39Ex:

private async void SetSymbologies(ClaimedBarcodeScanner claimedBarcodeScanner) 
{
    var symbologies = new List<uint>{ BarcodeSymbologies.Code39, BarcodeSymbologies.Code39Ex };
    await claimedBarcodeScanner.SetActiveSymbologiesAsync(symbologies);
}

Barcode symbology attributes

Different barcode symbologies can have different attributes, such as supporting multiple decode lengths, transmitting the check digit to the host as part of the raw data, and check digit validation. With the BarcodeSymbologyAttributes class, you can get and set these attributes for a given ClaimedBarcodeScanner and barcode symbology.

You can get the attributes of a given symbology with GetSymbologyAttributesAsync. The following code snippet gets the attributes of the Upca symbology for a ClaimedBarcodeScanner.

BarcodeSymbologyAttributes barcodeSymbologyAttributes = 
    await claimedBarcodeScanner.GetSymbologyAttributesAsync(BarcodeSymbologies.Upca);

When you've finished modifying the attributes and are ready to set them, you can call SetSymbologyAttributesAsync. This method returns a bool, which is true if the attributes were successfully set.

bool success = await claimedBarcodeScanner.SetSymbologyAttributesAsync(
    BarcodeSymbologies.Upca, barcodeSymbologyAttributes);

Restrict scan data by data length

Some symbologies are variable length such as Code 39 or Code 128. Barcodes of these symbologies can be located near each other containing different data often of specific length. Setting the specific length of the data you require can prevent invalid scans.

Before setting the decode length, check whether the barcode symbology supports multiple lengths with IsDecodeLengthSupported. Once you know that it's supported, you can set the DecodeLengthKind, which is of type BarcodeSymbologyDecodeLengthKind. This property can be any of the following values:

  • AnyLength: Decode lengths of any number.
  • Discrete: Decode lengths of either DecodeLength1 or DecodeLength2 single-byte characters.
  • Range: Decode lengths between DecodeLength1 and DecodeLength2 single-byte characters. The order of DecodeLength1 and DecodeLength2 do not matter (either can be higher or lower than the other).

Finally, you can set the values of DecodeLength1 and DecodeLength2 to control the length of the data you require.

The following code snippet demonstrates setting the decode length:

private async Task<bool> SetDecodeLength(
    ClaimedBarcodeScanner scanner,
    uint symbology, 
    BarcodeSymbologyDecodeLengthKind kind, 
    uint decodeLength1, 
    uint decodeLength2)
{
    bool success = false;
    BarcodeSymbologyAttributes attributes = await scanner.GetSymbologyAttributesAsync(symbology);

    if (attributes.IsDecodeLengthSupported)
    {
        attributes.DecodeLengthKind = kind;
        attributes.DecodeLength1 = decodeLength1;
        attributes.DecodeLength2 = decodeLength2;
        success = await scanner.SetSymbologyAttributesAsync(symbology, attributes);
    }

    return success;
}

Check digit transmission

Another attribute you can set on a symbology is whether the check digit will be transmitted to the host as part of the raw data. Before setting this, make sure that the symbology supports check digit transmission with IsCheckDigitTransmissionSupported. Then, set whether check digit transmission is enabled with IsCheckDigitTransmissionEnabled.

The following code snippet demonstrates setting check digit transmission:

private async Task<bool> SetCheckDigitTransmission(ClaimedBarcodeScanner scanner, uint symbology, bool isEnabled)
{
    bool success = false;
    BarcodeSymbologyAttributes attributes = await scanner.GetSymbologyAttributesAsync(symbology);

    if (attributes.IsCheckDigitTransmissionSupported)
    {
        attributes.IsCheckDigitTransmissionEnabled = isEnabled;
        success = await scanner.SetSymbologyAttributesAsync(symbology, attributes);
    }

    return success;
}

Check digit validation

You can also set whether the barcode check digit will be validated. Before setting this, make sure that the symbology supports check digit validation with IsCheckDigitValidationSupported. Then, set whether check digit validation is enabled with IsCheckDigitValidationEnabled.

The following code snippet demonstrates setting check digit validation:

private async Task<bool> SetCheckDigitValidation(ClaimedBarcodeScanner scanner, uint symbology, bool isEnabled)
{
    bool success = false;
    BarcodeSymbologyAttributes attributes = await scanner.GetSymbologyAttributesAsync(symbology);

    if (attributes.IsCheckDigitValidationSupported)
    {
        attributes.IsCheckDigitValidationEnabled = isEnabled;
        success = await scanner.SetSymbologyAttributesAsync(symbology, attributes);
    }

    return success;
}

Support and feedback

Find answers to your questions

Have questions? Ask us on either our Docs Q&A forum with the UWP tag or on Stack Overflow with the pointofservice tag.

Help us locate your questions:

  • Add the pointofservice tag to your question on Stack Overflow.
  • Include the "UWP" term in your post on the Q&A forum

See also