開始使用相機條碼掃描器

本主題描述如何在 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 (此處為您的相機名稱)

如果有一個以上的相機,且其內建於電腦的底座中,則名稱可能會區分前置後置相機。

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 持續掃描條碼而不停止

另請參閱