Get started with a camera barcode scanner
This topic describes how to set up a basic camera barcode scanner in a UWP application.
Note
The software decoder built into Windows 10/11 is provided by Digimarc Corporation.
The following code snippets are for demonstration purposes only. For a complete working sample, see the Barcode scanner sample.
Step 1: Add capability declarations to your app manifest
- In Microsoft Visual Studio, in Solution Explorer, open the designer for the application manifest by double-clicking the package.appxmanifest item.
- Select the Capabilities tab.
- Check the boxes for Webcam and PointOfService.
Note
The Webcam capability is required for the software decoder to receive frames from the camera to both decode the barcode and provide a preview in your application.
Step 2: Add using
directives
using Windows.Devices.Enumeration;
using Windows.Devices.PointOfService;
Step 3: Define your device selector
Use one of the BarcodeScanner.GetDeviceSelector methods to obtain a BarcodeScanner object for each connected barcode scanner.
Option A: Find all barcode scanners
string selector = BarcodeScanner.GetDeviceSelector();
Option B: Find all barcode scanners based on scope (for this example, we filter on Local connection type)
string selector = BarcodeScanner.GetDeviceSelector(PosConnectionTypes.Local);
DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(selector);
Step 4: Enumerate barcode scanners
If you do not expect the list of devices to change during the lifespan of your application, use DeviceInformation.FindAllAsync to get a one-time snapshot. However, if the list of barcode scanners could change during the lifespan of your application, use a DeviceWatcher instead.
Important
Using GetDefaultAsync to enumerate PointOfService devices can result in inconsistent behavior as it only returns the first device found in the class (which can change from session to session).
Option A: Enumerate a snapshot of all connected barcode scanners based on the selector created in Step 3
In this snippet, we create a DeviceInformationCollection object and use ****DeviceInformation.FindAllAsync to populate it.
DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(selector);
Tip
See Enumerate a snapshot of devices for more information on using DeviceInformation.FindAllAsync.
Option B: Enumerate available barcode scanners based on the selector created in Step 3 and watch for changes to that collection
In this snippet, we create a DeviceWatcher using DeviceInformation.CreateWatcher.
DeviceWatcher deviceWatcher = DeviceInformation.CreateWatcher(selector);
watcher.Added += Watcher_Added;
watcher.Removed += Watcher_Removed;
watcher.Updated += Watcher_Updated;
watcher.Start();
Tip
For more information, see Enumerate and watch devices and DeviceWatcher.
Step 5: Identify camera barcode scanners
A camera barcode scanner consists of a camera (attached to a computer) combined with a software decoder, which Windows dynamically pairs to create a fully functional barcode scanner for Universal Windows Platform (UWP) apps.
BarcodeScanner.VideoDeviceID can be used to differentiate between camera barcode scanners and physical barcode scanners. A non-NULL VideoDeviceID indicates that the barcode scanner object from your device collection is a camera barcode scanner. If you have more than one camera barcode scanner you might want to build a separate collection that excludes physical barcode scanners.
Camera barcode scanners using the decoder that ships with Windows are identified as:
Microsoft BarcodeScanner (name of your camera here)
If there is more than one camera, and they are built into the chassis of the computer, the name might differentiate between front and rear cameras.
When the DeviceWatcher starts (see Step 4: Enumerate barcode scanners), it enumerates through each connected device. In the following snippet, we add each available scanner to a BarcodeScanner collection and bind the collection to a ListBox.
ObservableCollection<BarcodeScanner> barcodeScanners =
new ObservableCollection<BarcodeScanner>();
private async void Watcher_Added(DeviceWatcher sender, DeviceInformation args)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
barcodeScanners.Add(new BarcodeScanner(args.Name, args.Id));
// Select the first scanner by default.
if (barcodeScanners.Count == 1)
{
ScannerListBox.SelectedIndex = 0;
}
});
}
When the SelectedIndex of the ListBox changes (the first item is selected by default in the previous snippet), we query the device info (the SelectScannerAsync
task is implemented in Step 6: Claim the camera barcode scanner).
private async void ScannerSelection_Changed(object sender, SelectionChangedEventArgs args)
{
var selectedScannerInfo = (BarcodeScanner)args.AddedItems[0];
var deviceId = selectedScannerInfo.DeviceId;
await SelectScannerAsync(deviceId);
}
Step 6: Claim the camera barcode scanner
Call BarcodeScanner.ClaimScannerAsync to obtain exclusive use of the camera barcode scanner.
private async Task SelectScannerAsync(string scannerDeviceId)
{
selectedScanner = await BarcodeScanner.FromIdAsync(scannerDeviceId);
if (selectedScanner != null)
{
claimedScanner = await selectedScanner.ClaimScannerAsync();
if (claimedScanner != null)
{
await claimedScanner.EnableAsync();
}
else
{
rootPage.NotifyUser("Failed to claim the selected barcode scanner", NotifyType.ErrorMessage);
}
}
else
{
rootPage.NotifyUser("Failed to create a barcode scanner object", NotifyType.ErrorMessage);
}
}
Step 7: System provided preview
A camera preview is required to help the user aim the camera at a barcode. Windows provides a basic camera preview that launches a dialog to control of the camera barcode scanner.
Call ClaimedBarcodeScanner.ShowVideoPreview to open the dialog and ClaimedBarcodeScanner.HideVideoPreview to close it.
Tip
See Hosting Preview to host the preview for camera barcode scanner in your application.
Step 8: Initiate scan
You can initiate the scan process by calling StartSoftwareTriggerAsync.
Depending on the value of IsDisabledOnDataReceived the scanner might scan only one barcode then stop or scan continuously until you call StopSoftwareTriggerAsync.
Set the desired value of IsDisabledOnDataReceived to control the scanner behavior when a barcode is decoded.
Value | Description |
---|---|
True | Scan only one barcode then stop |
False | Continuously scan barcodes without stopping |