共用方式為


從您的應用程式掃描

本主題描述如何使用扁平化、摘要器或自動設定的掃描來源,從您的應用程式掃描內容。

重要 API

若要從您的應用程式掃描,您必須先宣告新的 DeviceInformation 物件並取得 DeviceClass 類型,以列出可用的掃描器。 只有使用 WIA 驅動程式在本機安裝的掃描器會列出並可供您的應用程式使用。

當您的應用程式列出可用的掃描器之後,就可以根據掃描器類型使用自動設定的掃描設定,或只使用可用的平板或送紙器掃描來源進行掃描。 若要使用自動設定,掃描器必須啟用自動設定,且不得同時配備平板和送紙器掃描器。 如需詳細資訊,請參閱自動設定的掃描

列舉可用的掃描器

Windows 不會自動偵測掃描器。 您必須執行此步驟,您的應用程式才能與掃描器通訊。 在此範例中,掃描儀裝置列舉是使用 Windows.Devices.Enumeration 命名空間來完成。

  1. 首先,將這些 using 語句新增至您的類別定義檔。
    using Windows.Devices.Enumeration;
    using Windows.Devices.Scanners;
  1. 接下來,實作裝置監看員以開始列舉掃描器。 如需詳細資訊,請參閱列舉裝置
    void InitDeviceWatcher()
    {
       // Create a Device Watcher class for type Image Scanner for enumerating scanners
       scannerWatcher = DeviceInformation.CreateWatcher(DeviceClass.ImageScanner);

       scannerWatcher.Added += OnScannerAdded;
       scannerWatcher.Removed += OnScannerRemoved;
       scannerWatcher.EnumerationCompleted += OnScannerEnumerationComplete;
    }
  1. 建立掃描器新增時的事件處理常式。
    private async void OnScannerAdded(DeviceWatcher sender,  DeviceInformation deviceInfo)
    {
       await
       MainPage.Current.Dispatcher.RunAsync(
             Windows.UI.Core.CoreDispatcherPriority.Normal,
             () =>
             {
                MainPage.Current.NotifyUser(String.Format("Scanner with device id {0} has been added", deviceInfo.Id), NotifyType.StatusMessage);

                // search the device list for a device with a matching device id
                ScannerDataItem match = FindInList(deviceInfo.Id);

                // If we found a match then mark it as verified and return
                if (match != null)
                {
                   match.Matched = true;
                   return;
                }

                // Add the new element to the end of the list of devices
                AppendToList(deviceInfo);
             }
       );
    }

掃描

  1. 取得 ImageScanner 物件

針對每個 ImageScannerScanSource 列舉類型,無論是 DefaultAutoConfiguredFlatbedFeeder,您必須先呼叫 ImageScanner.FromIdAsync 方法來建立 ImageScanner 物件,如下所示。

   ImageScanner myScanner = await ImageScanner.FromIdAsync(deviceId);
  1. 只要掃描

若要使用預設設定進行掃描,您的應用程式依賴 Windows.Devices.Scanners 命名空間來選取掃描器,並從該來源掃描。 不會變更掃描設定。 可能的掃描器是自動設定、平板或送紙器。 這種類型的掃描最有可能產生成功的掃描作業,即使它從錯誤的來源掃描,例如扁平化而不是饋送器。

注意如果使用者將檔放在送紙器中掃描,掃描器會改為從平板掃描。 如果使用者嘗試從空的摘要器進行掃描,掃描作業將不會產生任何掃描的檔案。  

    var result = await myScanner.ScanFilesToFolderAsync(ImageScannerScanSource.Default,
        folder).AsTask(cancellationToken.Token, progress);
  1. 從自動設定、Flatbed 或Feeder來源掃描

您的應用程式可以使用裝置的自動設定掃描,以最理想的掃描設定進行掃描。 使用此選項時,裝置本身可以根據所掃描的內容來判斷最佳掃描設定,例如色彩模式和掃描解析度。 裝置會在執行階段間選取每個新掃描作業的掃描設定。

注意並非所有掃描器都支援這項功能,因此應用程式在使用這項設定之前,必須先檢查掃描器是否支援此功能。

在此範例中,應用程式會先檢查掃描器是否能夠自動設定,然後掃描。 若要指定平板或送紙器掃描器,只需將 AutoConfigured 取代為 FlatbedFeeder

    if (myScanner.IsScanSourceSupported(ImageScannerScanSource.AutoConfigured))
    {
        ...
        // Scan API call to start scanning with Auto-Configured settings.
        var result = await myScanner.ScanFilesToFolderAsync(
            ImageScannerScanSource.AutoConfigured, folder).AsTask(cancellationToken.Token, progress);
        ...
    }

預覽掃描

您可以在掃描至資料夾之前,新增程式碼以預覽掃描。 在下列範例中,應用程式會檢查 Flatbed 掃描器是否支援預覽,然後預覽掃描。

if (myScanner.IsPreviewSupported(ImageScannerScanSource.Flatbed))
{
    rootPage.NotifyUser("Scanning", NotifyType.StatusMessage);
                // Scan API call to get preview from the flatbed.
                var result = await myScanner.ScanPreviewToStreamAsync(
                    ImageScannerScanSource.Flatbed, stream);

取消掃描

您可以讓使用者在掃描中途取消掃描作業,如下所示。

void CancelScanning()
{
    if (ModelDataContext.ScenarioRunning)
    {
        if (cancellationToken != null)
        {
            cancellationToken.Cancel();
        }                
        DisplayImage.Source = null;
        ModelDataContext.ScenarioRunning = false;
        ModelDataContext.ClearFileList();
    }
}

使用進度掃描

  1. 建立 System.Threading.CancellationTokenSource 物件。
cancellationToken = new CancellationTokenSource();
  1. 設定進度事件處理常式,並取得掃描的進度。
    rootPage.NotifyUser("Scanning", NotifyType.StatusMessage);
    var progress = new Progress<UInt32>(ScanProgress);

掃描圖片庫

使用者可以使用 FolderPicker 類別動態掃描至任何資料夾,但您必須在指令清單中宣告圖片庫功能,讓使用者掃描該資料夾。 如需應用程式功能的詳細資訊,請參閱應用程式功能宣告