相机条形码扫描仪入门

本主题介绍如何在 UWP 应用程序中设置基本相机条形码扫描仪。

注意

Windows 10/11 内置的软件解码器由 Digimarc Corporation 提供。

以下代码片段仅用于演示目的。 有关完整的工作示例,请参阅条形码扫描仪示例

步骤 1:向应用部件清单添加功能声明

  1. 在 Microsoft Visual Studio 的“解决方案资源管理器”中,通过双击“package.appxmanifest”项,打开应用程序清单的设计器。
  2. 选择“功能”选项卡。
  3. 选中网络摄像头PointOfService 对应的方框。

注意

软件解码器需要具备网络摄像头功能才能从摄像头接收帧,以解码条形码并在应用程序中提供预览。

步骤 2:添加 using 指令

using Windows.Devices.Enumeration;
using Windows.Devices.PointOfService;

步骤 3:定义设备选择器

使用 BarcodeScanner.GetDeviceSelector 方法之一获取每个连接的条形码扫描仪的 BarcodeScanner 对象。

选项 A:查找所有条形码扫描仪

string selector = BarcodeScanner.GetDeviceSelector();

选项 B:根据范围查找所有条形码扫描仪(在本例中,我们按本地连接类型进行筛选)

string selector = BarcodeScanner.GetDeviceSelector(PosConnectionTypes.Local);
DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(selector);

步骤 4:枚举条形码扫描仪

如果预计设备列表在应用程序的生命周期内不会发生更改,请使用 DeviceInformation.FindAllAsync 获取一次性快照。 但是,如果条形码扫描仪列表在应用程序的生命周期内可能会更改,请改用 DeviceWatcher

重要

使用 GetDefaultAsync 枚举 PointOfService 设备可能会导致不一致的行为,因为它只返回在类中找到的第一个设备(这可能随会话的变化而改变)。

选项 A:根据步骤 3 中创建的选择器枚举所有已连接条形码扫描仪的快照

在此代码片段中,我们将创建一个 DeviceInformationCollection 对象并使用 ****DeviceInformation.FindAllAsync 填充它。

DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(selector);

提示

有关使用 DeviceInformation.FindAllAsync 的详细信息,请参阅枚举设备快照

选项 B:根据步骤 3 中创建的选择器枚举可用的条形码扫描仪,并观察该集合的更改

在此代码片段中,我们使用 DeviceInformation.CreateWatcher 创建一个 DeviceWatcher

DeviceWatcher deviceWatcher = DeviceInformation.CreateWatcher(selector);
watcher.Added += Watcher_Added;
watcher.Removed += Watcher_Removed;
watcher.Updated += Watcher_Updated;
watcher.Start();

提示

有关详细信息,请参阅枚举和监视设备DeviceWatcher

步骤 5:识别相机条形码扫描仪

相机条码扫描仪由一个相机(连接到计算机)和一个软件解码器组成,Windows 会为它们动态配对,从而为通用 Windows 平台 (UWP) 应用程序创建功能齐全的条形码扫描仪

BarcodeScanner.VideoDeviceID 可用于区分相机条形码扫描仪和物理条形码扫描仪。 非 NULL VideoDeviceID 指示来自设备集合的条形码扫描仪对象是相机条形码扫描仪。 如果具有多个相机条形码扫描仪,可能需要生成一个单独的集合,该集合不包括物理条形码扫描仪。

使用 Windows 附带的解码器的相机条形码扫描仪被标识为:

Microsoft BarcodeScanner(此处为你的相机名称

如果有多个相机,且这些相机已内置于计算机机箱内,名称中可能会使用 frontrear 加以区分。

DeviceWatcher 启动后(请参阅步骤 4:枚举条形码扫描仪),它会枚举每个已连接的设备。 在以下代码片段中,我们将每个可用的扫描仪添加到 BarcodeScanner 集合,并将该集合绑定到 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;
        }
    });
}

ListBoxSelectedIndex 更改时(在上一个代码片段中默认选择第一项),我们会查询设备信息(SelectScannerAsync 任务在步骤 6:领取相机条形码扫描仪中实施)。

private async void ScannerSelection_Changed(object sender, SelectionChangedEventArgs args)
{
    var selectedScannerInfo = (BarcodeScanner)args.AddedItems[0];
    var deviceId = selectedScannerInfo.DeviceId;

    await SelectScannerAsync(deviceId);
}

步骤 6:领取相机条形码扫描仪

调用 BarcodeScanner.ClaimScannerAsync 以获得相机条形码扫描仪的独占使用权。

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

步骤 7:系统提供的预览

需要相机预览来帮助用户将相机瞄准条形码。 Windows 提供基本的相机预览,用于启动用于控制相机条形码扫描仪的对话框。

调用 ClaimedBarcodeScanner.ShowVideoPreview 即可打开对话框,调用 ClaimedBarcodeScanner.HideVideoPreview 即可将其关闭。

提示

请参阅托管预览以在你的应用程序中托管相机条形码扫描仪的预览。

步骤 8:启动扫描

可以通过调用 StartSoftwareTriggerAsync 来启动扫描过程。

根据“IsDisabledOnDataReceived”的值,扫描仪可能在扫描一个条形码后即停止,或持续扫描直到你调用“StopSoftwareTriggerAsync”

设置所需的 IsDisabledOnDataReceived 值以控制解码条形码时扫描仪的行为。

说明
True 仅扫描一个条形码,然后停止
False 连续扫描条形码而不停止

另请参阅