ketan Ram Patil Ответов: 1

Как получить список подключенных сканеров и отсканировать документ в uwp


Здравствуйте, друзья, я новичок в uwp.

я хочу отсканировать документ. я установил пакет NTwain 3.6.0 из NuGet. теперь мой вопрос заключается в том, как получить список сканеров и сканировать документ.

Что я уже пробовал:

не знаю.....................................................

Gerry Schmitz

Во-первых, вы должны быть в состоянии сканировать; без всех "тегов". Тогда, вероятно, проще получить доступ к "сканированному изображению", а не к сканеру.

ketan Ram Patil

спасибо за ваш ценный ответ. есть ли у вас какой-либо пример кода или ссылка, которые сканируют изображение(или документ) через сканер в uwp

1 Ответов

Рейтинг:
9

ketan Ram Patil

Наконец я нашел решение. на 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 код :


<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>


сканирование кода страницы документа сзади:

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.
       }