Training
Module
Use AL-controlled barcode scanning in Dynamics 365 Business Central - Training
Learn how Business Central provides barcode scanning capabilities in the mobile application and AL language.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
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.
using Windows.Devices.Enumeration;
using Windows.Devices.PointOfService;
Use one of the BarcodeScanner.GetDeviceSelector methods to obtain a BarcodeScanner object for each connected barcode scanner.
string selector = BarcodeScanner.GetDeviceSelector();
string selector = BarcodeScanner.GetDeviceSelector(PosConnectionTypes.Local);
DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(selector);
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).
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.
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.
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);
}
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);
}
}
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.
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 |
Training
Module
Use AL-controlled barcode scanning in Dynamics 365 Business Central - Training
Learn how Business Central provides barcode scanning capabilities in the mobile application and AL language.
Documentation
Enable or disable the software decoder for the Camera Barcode Scanner - UWP applications
Learn how to set a system registry key in Windows 10 to enable or disable the software decoder for the Camera Barcode Scanner.
Camera barcode scanner system requirements - UWP applications
Lists the requirements for using camera barcode scanner from a UWP app.
Host a camera barcode scanner preview in a UWP application - UWP applications
Host a camera barcode scanner preview in a UWP application on Windows 10 Version 1803 or later.