如何获取连接的扫描仪列表并在uwp中扫描文档 [英] How to get the connected scanner list and scan the document in uwp

查看:133
本文介绍了如何获取连接的扫描仪列表并在uwp中扫描文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,我是uwp的新手。



我想扫描一份文件。我安装了NuGet的NTwain 3.6.0软件包。现在我的问题是如何获取扫描仪列表并扫描文档。



我尝试过:



不知道....................................... ..............

hello friends i am new in uwp.

i want to scan a document. i am installed the NTwain 3.6.0 Package from NuGet. now my question is how to get the scanner list and scan the document.

What I have tried:

dont know.....................................................

推荐答案

最后我找到了解决方案。在谷歌上:)



Finally i found the solution. on google :)

class DeviceWatcherHelper
 {
     public DeviceWatcherHelper(
         ObservableCollection<DeviceInformationDisplay> resultCollection,
         CoreDispatcher dispatcher)
     {
         this.resultCollection = resultCollection;
         this.dispatcher = dispatcher;
     }

     public delegate void DeviceChangedHandler(DeviceWatcher deviceWatcher, string id);
     public event DeviceChangedHandler DeviceChanged;

     public DeviceWatcher DeviceWatcher => deviceWatcher;
     public bool UpdateStatus = true;

     public void StartWatcher(DeviceWatcher deviceWatcher)
     {
         this.deviceWatcher = deviceWatcher;

         // Connect events to update our collection as the watcher report results.
         deviceWatcher.Added += Watcher_DeviceAdded;
         deviceWatcher.Updated += Watcher_DeviceUpdated;
         deviceWatcher.Removed += Watcher_DeviceRemoved;
         //deviceWatcher.EnumerationCompleted += Watcher_EnumerationCompleted;
         //deviceWatcher.Stopped += Watcher_Stopped;

         deviceWatcher.Start();
     }

     public void StopWatcher()
     {
         // Since the device watcher runs in the background, it is possible that
         // a notification is "in flight" at the time we stop the watcher.
         // In other words, it is possible for the watcher to become stopped while a
         // handler is running, or for a handler to run after the watcher has stopped.

         if (IsWatcherStarted(deviceWatcher))
         {
             // We do not null out the deviceWatcher yet because we want to receive
             // the Stopped event.
             deviceWatcher.Stop();
         }
     }

     public void Reset()
     {
         if (deviceWatcher != null)
         {
             StopWatcher();
             deviceWatcher = null;
         }
     }

     DeviceWatcher deviceWatcher = null;
     ObservableCollection<DeviceInformationDisplay> resultCollection;
     CoreDispatcher dispatcher;

     static bool IsWatcherStarted(DeviceWatcher watcher)
     {
         return (watcher.Status == DeviceWatcherStatus.Started) ||
             (watcher.Status == DeviceWatcherStatus.EnumerationCompleted);
     }

     public bool IsWatcherRunning()
     {
         if (deviceWatcher == null)
         {
             return false;
         }

         DeviceWatcherStatus status = deviceWatcher.Status;
         return (status == DeviceWatcherStatus.Started) ||
             (status == DeviceWatcherStatus.EnumerationCompleted) ||
             (status == DeviceWatcherStatus.Stopping);
     }

     private async void Watcher_DeviceAdded(DeviceWatcher sender, DeviceInformation deviceInfo)
     {
         // Since we have the collection databound to a UI element, we need to update the collection on the UI thread.
         await dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
         {
             // Watcher may have stopped while we were waiting for our chance to run.
             if (IsWatcherStarted(sender))
             {
                 resultCollection.Add(new DeviceInformationDisplay(deviceInfo ));
               //  RaiseDeviceChanged(sender, deviceInfo.Id);
             }
         });
     }

     private async void Watcher_DeviceUpdated(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate)
     {
         // Since we have the collection databound to a UI element, we need to update the collection on the UI thread.
         await dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
         {
             // Watcher may have stopped while we were waiting for our chance to run.
             if (IsWatcherStarted(sender))
             {
                 // Find the corresponding updated DeviceInformation in the collection and pass the update object
                 // to the Update method of the existing DeviceInformation. This automatically updates the object
                 // for us.
                 foreach (DeviceInformationDisplay deviceInfoDisp in resultCollection)
                 {
                     if (deviceInfoDisp.Id == deviceInfoUpdate.Id)
                     {
                         deviceInfoDisp.Update(deviceInfoUpdate);
                      //   RaiseDeviceChanged(sender, deviceInfoUpdate.Id);
                         break;
                     }
                 }
             }
         });
     }

     private async void Watcher_DeviceRemoved(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate)
     {
         // Since we have the collection databound to a UI element, we need to update the collection on the UI thread.
         await dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
         {
             // Watcher may have stopped while we were waiting for our chance to run.
             if (IsWatcherStarted(sender))
             {
                 // Find the corresponding DeviceInformation in the collection and remove it
                 foreach (DeviceInformationDisplay deviceInfoDisp in resultCollection)
                 {
                     if (deviceInfoDisp.Id == deviceInfoUpdate.Id)
                     {
                         resultCollection.Remove(deviceInfoDisp);
                         break;
                     }
                 }

                 //RaiseDeviceChanged(sender, deviceInfoUpdate.Id);
             }
         });
     }

     private async void Watcher_EnumerationCompleted(DeviceWatcher sender, object obj)
     {
         await dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
         {
           //  RaiseDeviceChanged(sender, string.Empty);
         });
     }

     private async void Watcher_Stopped(DeviceWatcher sender, object obj)
     {
         await dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
         {
            // RaiseDeviceChanged(sender, string.Empty);
         });
     }

 }




扫描文档页面上的
XAML代码:







on Scan Document Page XAML code :


<Border  extensions:Mouse.Cursor="Hand"  Margin="42,10,76,0" BorderThickness="1" BorderBrush="#E1E1E1" CornerRadius="5" VerticalAlignment="Stretch">
          <ComboBox Padding="5 0 0 0"  Name="CmbScannerList"  extensions:Mouse.Cursor="Hand" SelectedValuePath="rid" BorderBrush="Transparent" BorderThickness="0" FontSize="13" FontWeight="Light" FontFamily="{StaticResource inventoryRegularFont}" TabIndex="8"   Header="" PlaceholderForeground="#414042" PlaceholderText="Connected Devices List " HorizontalAlignment="Left" VerticalAlignment="Top"  Margin="0,-1,0,0" Width="532" Height="57">
              <ComboBox.ItemTemplate>
                  <DataTemplate x:DataType="local:DeviceInformationDisplay" >
                      <TextBlock Name="{x:Bind Id }" Text="{x:Bind Name }" />
                  </DataTemplate>
              </ComboBox.ItemTemplate>
          </ComboBox>
      </Border>





扫描文档页面代码:





scan document page code behind:

public sealed partial class ScanDocumentPage : Page
   {
       private DeviceWatcherHelper deviceWatcherHelper;

       private ObservableCollection<DeviceInformationDisplay> resultCollection = new ObservableCollection<DeviceInformationDisplay>();

        public ScanDocumentPage()
       {
           this.InitializeComponent();


           deviceWatcherHelper = new DeviceWatcherHelper(resultCollection, Dispatcher);

           StartWatcher();
       }

         private void StartWatcher()
       {

           resultCollection.Clear();
           DeviceWatcher deviceWatcher;
           deviceWatcher = DeviceInformation.CreateWatcher(DeviceClass.All ); //here you can set the All or ImageScanner or Audiodevices
            deviceWatcherHelper.StartWatcher(deviceWatcher);
       }
          protected override void OnNavigatedTo(NavigationEventArgs e)
       {
           CmbScannerList.ItemsSource = resultCollection; // here you can bind the devices list.
       }


这篇关于如何获取连接的扫描仪列表并在uwp中扫描文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆