Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.....................................................
Posted
Updated 11-Apr-19 19:23pm
Comments
[no name] 9-Apr-19 11:12am    
First off, you should be able to scan; without all the "tags". Then, it's probably simpler to access the "scanned image", instead of the scanner.
ketan Ram Patil 10-Apr-19 1:03am    
thank you for your valuable reply. do you have any code example or link which scan image(or document) through scanner in uwp

1 solution

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

 }


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.
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900