Exemplo n.º 1
0
        public async Task <IEnumerable <StorageFile> > ScanPicturesAsync(
            String scannerDeviceId, ImageScannerScanSource source,
            StorageFolder destinationFolder,
            Double hScanPercent, Double vScanPercent)
        {
            var scanner = await ImageScanner.FromIdAsync(scannerDeviceId);

            if (scanner.IsScanSourceSupported(source))
            {
                ConfigureScanner(scanner, source, hScanPercent, vScanPercent);

                var scanResult = await scanner
                                 .ScanFilesToFolderAsync(source, destinationFolder);

                var results = new List <StorageFile>();
                // Caution - enumerating this list (foreach) will result in a
                // COM exception.  Instead, just walk the collection by index
                // and build a new list.
                for (var i = 0; i < scanResult.ScannedFiles.Count; i++)
                {
                    results.Add(scanResult.ScannedFiles[i]);
                }
                return(results);
            }
            return(null);
        }
Exemplo n.º 2
0
        public async Task <ScanSourceDetails> GetSupportedScanSources
            (String deviceId)
        {
            // Check whether each of the known scan sources is available
            // for the scanner with the provided ID
            var scanSources = new[]
            {
                ImageScannerScanSource.Flatbed,
                ImageScannerScanSource.Feeder,
                ImageScannerScanSource.AutoConfigured
            };

            var scanner = await ImageScanner.FromIdAsync(deviceId);

            var supportedSources = scanSources
                                   .Where(x => scanner.IsScanSourceSupported(x))
                                   .Select(x => new ScanSourceDetailsItem
            {
                SourceType      = x,
                SupportsPreview = scanner.IsPreviewSupported(x)
            })
                                   .ToList();

            var results = new ScanSourceDetails
            {
                SupportedScanSources = supportedSources,
                DefaultScanSource    = scanner.DefaultScanSource
            };

            return(results);
        }
Exemplo n.º 3
0
        // Update methods
        private async void UpdateScannerInfo()
        {
            while (SelectedScanner == null)
            {
                try
                {
                    SelectedScanner = await ImageScanner.FromIdAsync(_selectedDevice.Id);

                    ScannerSources      = ScannerHelper.GetSupportedScanSources(SelectedScanner);
                    UIIsScannerSelected = true;
                }
                catch (Exception ex)
                {
                    // https://stackoverflow.com/questions/15772373/error-code-when-trying-to-connect-to-a-scanner-using-wpf

                    Debug.WriteLine("MainViewModel - Scanner is busy");
                    Debug.WriteLine(ex);
                }
            }

            // Set the default properties
            var defaultScannerSource = SelectedScanner.DefaultScanSource;

            if (ScannerSources.Contains(defaultScannerSource))
            {
                SelectedScannerSource = defaultScannerSource;
            }
        }
Exemplo n.º 4
0
        private async void cmbxScanner_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            selectedDevice  = (DeviceInformation)cmbxScanner.SelectedItem;
            selectedScanner = await ImageScanner.FromIdAsync(selectedDevice.Id);

            scannerSources = ScannerHelper.GetSupportedScanSources(selectedScanner);
            cmbxScannerSource.ItemsSource = scannerSources;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Scans the images from the scanner with auto configured settings
        /// The scanning is allowed only if the selected scanner supports Device Auto-Configured Scanning.
        /// </summary>
        /// <param name="deviceId">scanner device id</param>
        /// <param name="folder">the folder that receives the scanned files</param>
        public async void ScanToFolder(string deviceId, StorageFolder folder)
        {
            try
            {
                // Get the scanner object for this device id
                ImageScanner myScanner = await ImageScanner.FromIdAsync(deviceId);

                // Check to see if the use has already cancelled the scenario
                if (ModelDataContext.ScenarioRunning)
                {
                    if (myScanner.IsScanSourceSupported(ImageScannerScanSource.AutoConfigured))
                    {
                        cancellationToken = new CancellationTokenSource();

                        // Set the scan file format to PNG, if available
                        if (myScanner.AutoConfiguration.IsFormatSupported(ImageScannerFormat.Png))
                        {
                            myScanner.AutoConfiguration.Format = ImageScannerFormat.Png;
                        }

                        rootPage.NotifyUser("Scanning", NotifyType.StatusMessage);
                        var progress = new Progress <UInt32>(ScanProgress);
                        // Scan API call to start scanning with Auto-Configured settings
                        var result = await myScanner.ScanFilesToFolderAsync(ImageScannerScanSource.AutoConfigured, folder).AsTask(cancellationToken.Token, progress);

                        ModelDataContext.ScenarioRunning = false;
                        if (result.ScannedFiles.Count > 0)
                        {
                            Utils.DisplayImageAndScanCompleteMessage(result.ScannedFiles, DisplayImage);
                            if (result.ScannedFiles.Count > 1)
                            {
                                Utils.UpdateFileListData(result.ScannedFiles, ModelDataContext);
                            }
                        }
                        else
                        {
                            rootPage.NotifyUser("There were no files scanned.", NotifyType.ErrorMessage);
                        }
                    }
                    else
                    {
                        ModelDataContext.ScenarioRunning = false;
                        rootPage.NotifyUser("The selected scanner does not support Device Auto-Configured Scanning.", NotifyType.ErrorMessage);
                    }
                }
            }
            catch (OperationCanceledException)
            {
                Utils.DisplayScanCancelationMessage();
            }
            catch (Exception ex)
            {
                Utils.OnScenarioException(ex, ModelDataContext);
            }
        }
        /// <summary>
        /// Scans image from the Flatbed source of the scanner
        /// The scanning is allowed only if the selected scanner is equipped with a Flatbed
        /// </summary>
        /// <param name="deviceId">scanner device id</param>
        /// <param name="folder">the folder that receives the scanned files</param>
        public async void ScanToFolder(string deviceId, StorageFolder folder)
        {
            try
            {
                // Get the scanner object for this device id
                ImageScanner myScanner = await ImageScanner.FromIdAsync(deviceId);

                // Check to see if the user has already canceled the scenario
                if (ModelDataContext.ScenarioRunning)
                {
                    if (myScanner.IsScanSourceSupported(ImageScannerScanSource.Flatbed))
                    {
                        // Set the scan file format to Device Independent Bitmap (DIB)
                        myScanner.FlatbedConfiguration.Format = ImageScannerFormat.DeviceIndependentBitmap;

                        cancellationToken = new CancellationTokenSource();

                        rootPage.NotifyUser("Scanning", NotifyType.StatusMessage);
                        // Scan API call to start scanning from the Flatbed source of the scanner.
                        var result = await myScanner.ScanFilesToFolderAsync(ImageScannerScanSource.Flatbed, folder).AsTask(cancellationToken.Token);

                        ModelDataContext.ScenarioRunning = false;
                        if (result.ScannedFiles.Count > 0)
                        {
                            Utils.DisplayImageAndScanCompleteMessage(result.ScannedFiles, DisplayImage);
                        }
                        else
                        {
                            rootPage.NotifyUser("There are no files scanned from the Flatbed.", NotifyType.ErrorMessage);
                        }
                    }
                    else
                    {
                        ModelDataContext.ScenarioRunning = false;
                        rootPage.NotifyUser("The selected scanner does not report to be equipped with a Flatbed.", NotifyType.ErrorMessage);
                    }
                }
            }
            catch (OperationCanceledException)
            {
                Utils.DisplayScanCancelationMessage();
            }
            catch (Exception ex)
            {
                Utils.OnScenarioException(ex, ModelDataContext);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Scans all the images from Feeder source of the scanner
        /// The scanning is allowed only if the selected scanner is equipped with a Feeder
        /// </summary>
        /// <param name="deviceId">scanner device id</param>
        /// <param name="folder">the folder that receives the scanned files</param>
        public async void ScanToFolder(string deviceId, StorageFolder folder)
        {
            try
            {
                // Get the scanner object for this device id
                ImageScanner myScanner = await ImageScanner.FromIdAsync(deviceId);

                // Check to see if the use has already cancelled the scenario
                if (model.ScenarioRunning)
                {
                    if (myScanner.IsScanSourceSupported(ImageScannerScanSource.Feeder))
                    {
                        // Set MaxNumberOfPages to zero to scanning all the pages that are present in the feeder
                        myScanner.FeederConfiguration.MaxNumberOfPages = 0;
                        myScanner.FeederConfiguration.Duplex           = tggDuplex.IsOn;
                        myScanner.FeederConfiguration.PageSize         = (Windows.Graphics.Printing.PrintMediaSize)model.ScannerDataContext.CurrentSize;

                        cancellationToken = new CancellationTokenSource();
                        rootPage.NotifyUser("Escaneando", NotifyType.StatusMessage);

                        // Scan API call to start scanning from the Feeder source of the scanner.
                        var operation = myScanner.ScanFilesToFolderAsync(ImageScannerScanSource.Feeder, folder);
                        operation.Progress = new AsyncOperationProgressHandler <ImageScannerScanResult, uint>(ScanProgress);
                        var result = await operation.AsTask <ImageScannerScanResult, UInt32>(cancellationToken.Token);

                        // Number of scanned files should be zero here since we already processed during scan progress notifications all the files that have been scanned
                        rootPage.NotifyUser("Escaneo completado.", NotifyType.StatusMessage);
                        model.ScenarioRunning = false;
                    }
                    else
                    {
                        model.ScenarioRunning = false;
                        rootPage.NotifyUser("El escáner seleccionado indica no tener Feeder.", NotifyType.ErrorMessage);
                    }
                }
            }
            catch (OperationCanceledException ex)
            {
                Utils.DisplayScanCancelationMessage();
            }
            catch (Exception ex)
            {
                Utils.OnScenarioException(ex, model);
            }
        }
        /// <summary>
        /// Scans all the images from Feeder source of the scanner
        /// The scanning is allowed only if the selected scanner is equipped with a Feeder
        /// </summary>
        /// <param name="deviceId">scanner device id</param>
        /// <param name="folder">the folder that receives the scanned files</param>
        public async void ScanToFolder(string deviceId, StorageFolder folder)
        {
            try
            {
                // Get the scanner object for this device id
                ImageScanner myScanner = await ImageScanner.FromIdAsync(deviceId);

                // Check to see if the use has already cancelled the scenario
                if (ModelDataContext.ScenarioRunning)
                {
                    if (myScanner.IsScanSourceSupported(ImageScannerScanSource.Feeder))
                    {
                        // Set MaxNumberOfPages to zero to scanning all the pages that are present in the feeder
                        myScanner.FeederConfiguration.MaxNumberOfPages = 0;
                        cancellationToken = new CancellationTokenSource();
                        rootPage.NotifyUser("Scanning", NotifyType.StatusMessage);

                        // Scan API call to start scanning from the Feeder source of the scanner.
                        var operation = myScanner.ScanFilesToFolderAsync(ImageScannerScanSource.Feeder, folder);
                        operation.Progress = new AsyncOperationProgressHandler <ImageScannerScanResult, uint>(ScanProgress);
                        var result = await operation.AsTask <ImageScannerScanResult, UInt32>(cancellationToken.Token);

                        // Number of scanned files should be zero here since we already processed during scan progress notifications all the files that have been scanned
                        rootPage.NotifyUser("Scanning is complete.", NotifyType.StatusMessage);
                        ModelDataContext.ScenarioRunning = false;
                    }
                    else
                    {
                        ModelDataContext.ScenarioRunning = false;
                        rootPage.NotifyUser("The selected scanner does not report to be equipped with a Feeder.", NotifyType.ErrorMessage);
                    }
                }
            }
            catch (OperationCanceledException)
            {
                Utils.DisplayScanCancelationMessage();
            }
            catch (Exception ex)
            {
                Utils.OnScenarioException(ex, ModelDataContext);
            }
        }
Exemplo n.º 9
0
        public async Task <BitmapImage> ScanPicturePreviewAsync
            (String scannerDeviceId, ImageScannerScanSource scanSource)
        {
            var scanner = await ImageScanner.FromIdAsync(scannerDeviceId);

            if (scanner.IsPreviewSupported(scanSource))
            {
                var stream = new InMemoryRandomAccessStream();
                var result =
                    await scanner.ScanPreviewToStreamAsync(scanSource, stream);

                if (result.Succeeded)
                {
                    var thumbnail = new BitmapImage();
                    thumbnail.SetSource(stream);
                    return(thumbnail);
                }
            }
            return(null);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Scans the images from the scanner with default settings
        /// </summary>
        /// <param name="deviceId">scanner device id</param>
        /// <param name="folder">the folder that receives the scanned files</param>
        public async void ScanToFolder(string deviceId, StorageFolder folder)
        {
            try
            {
                // Get the scanner object for this device id
                ImageScanner myScanner = await ImageScanner.FromIdAsync(deviceId);

                // Check to see if the use has already cancelled the scenario
                if (ModelDataContext.ScenarioRunning)
                {
                    cancellationToken = new CancellationTokenSource();

                    rootPage.NotifyUser("Scanning", NotifyType.StatusMessage);
                    var progress = new Progress <UInt32>(ScanProgress);
                    // Scan API call to start scanning
                    var result = await myScanner.ScanFilesToFolderAsync(ImageScannerScanSource.Default, folder).AsTask(cancellationToken.Token, progress);

                    ModelDataContext.ScenarioRunning = false;
                    if (result.ScannedFiles.Count > 0)
                    {
                        Utils.DisplayImageAndScanCompleteMessage(result.ScannedFiles, DisplayImage);
                        if (result.ScannedFiles.Count > 1)
                        {
                            Utils.UpdateFileListData(result.ScannedFiles, ModelDataContext);
                        }
                    }
                    else
                    {
                        rootPage.NotifyUser("There were no files scanned.", NotifyType.ErrorMessage);
                    }
                }
            }
            catch (OperationCanceledException)
            {
                Utils.DisplayScanCancelationMessage();
            }
            catch (Exception ex)
            {
                Utils.OnScenarioException(ex, ModelDataContext);
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Previews the image from the scanner with given device id
        /// The preview is allowed only if the selected scanner is equipped with a Flatbed and supports preview.
        /// </summary>
        /// <param name="deviceId">scanner device id</param>
        /// <param name="stream">RandomAccessStream in which preview given the by the scan runtime API is kept</param>
        public async void ScanPreview(string deviceId, IRandomAccessStream stream)
        {
            try
            {
                // Get the scanner object for this device id
                ImageScanner myScanner = await ImageScanner.FromIdAsync(deviceId);

                if (myScanner.IsScanSourceSupported(ImageScannerScanSource.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);

                        if (result.Succeeded)
                        {
                            Utils.SetImageSourceFromStream(stream, DisplayImage);
                            rootPage.NotifyUser("Preview scanning is complete. Below is the preview image.", NotifyType.StatusMessage);
                        }
                        else
                        {
                            rootPage.NotifyUser("Failed to preview from Flatbed.", NotifyType.ErrorMessage);
                        }
                    }
                    else
                    {
                        rootPage.NotifyUser("The selected scanner does not support preview from Flatbed.", NotifyType.ErrorMessage);
                    }
                }
                else
                {
                    rootPage.NotifyUser("The selected scanner does not report to be equipped with a Flatbed.", NotifyType.ErrorMessage);
                }
            }
            catch (Exception ex)
            {
                Utils.DisplayExceptionErrorMessage(ex);
            }
        }
Exemplo n.º 12
0
        private async void OnScannerAdded(DeviceWatcher sender, DeviceInformation deviceInfo)
        {
            await
            MainPage.Current.Dispatcher.RunAsync(
                Windows.UI.Core.CoreDispatcherPriority.Normal,
                () =>
            {
                string stuff = "test";

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

            //var library = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures);
            //var folders = library.Folders;

            StorageFolder testFolder = await StorageFolder.GetFolderFromPathAsync(@"D:\Pictures\Test");

            //ImageScanner myScanner = await ImageScanner.FromIdAsync(deviceId);

            //4D36E967-E325-11CE-BFC1-08002BE10318
            ImageScanner myScanner = await ImageScanner.FromIdAsync(deviceInfo.Id);

            var result = await myScanner.ScanFilesToFolderAsync(ImageScannerScanSource.Default, testFolder);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Scans all the images from Feeder source of the scanner
        /// The scanning of the image is allowed only if the selected scanner has Feeder source
        /// </summary>
        /// <param name="deviceId">scanner device id</param>
        /// <param name="folder">the folder that receives the scanned files</param>
        public async void ScanToFolder(string deviceId, StorageFolder folder)
        {
            try
            {
                // Get the scanner object for this device id
                ImageScanner myScanner = await ImageScanner.FromIdAsync(deviceId);

                // Check to see if the use has already cancelled the scenario
                if (ModelDataContext.ScenarioRunning)
                {
                    if (myScanner.IsScanSourceSupported(ImageScannerScanSource.Feeder))
                    {
                        // Update Feeder Configuration
                        // Set the scan file format to PNG, if available, if not to DIB
                        if (myScanner.FeederConfiguration.IsFormatSupported(ImageScannerFormat.Png))
                        {
                            myScanner.FeederConfiguration.Format = ImageScannerFormat.Png;
                        }
                        else
                        {
                            myScanner.FeederConfiguration.Format = ImageScannerFormat.DeviceIndependentBitmap;
                        }
                        // Set the color mode to Grayscale, if available
                        if (myScanner.FeederConfiguration.IsColorModeSupported(ImageScannerColorMode.Grayscale))
                        {
                            myScanner.FeederConfiguration.ColorMode = ImageScannerColorMode.Grayscale;
                        }
                        // Set feeder to scan duplex
                        myScanner.FeederConfiguration.Duplex = myScanner.FeederConfiguration.CanScanDuplex;
                        // Set MaxNumberOfPages to zero to scanning all the pages that are present in the feeder
                        myScanner.FeederConfiguration.MaxNumberOfPages = 0;

                        rootPage.NotifyUser("Scanning", NotifyType.StatusMessage);

                        var progress = new Progress <UInt32>(ScanProgress);
                        cancellationToken = new CancellationTokenSource();
                        // Scan API call to start scanning from the Feeder source of the scanner.
                        var result = await myScanner.ScanFilesToFolderAsync(ImageScannerScanSource.Feeder, folder).AsTask(cancellationToken.Token, progress);

                        ModelDataContext.ScenarioRunning = false;
                        if (result.ScannedFiles.Count > 0)
                        {
                            Utils.DisplayImageAndScanCompleteMessage(result.ScannedFiles, DisplayImage);
                            if (result.ScannedFiles.Count > 1)
                            {
                                Utils.UpdateFileListData(result.ScannedFiles, ModelDataContext);
                            }
                        }
                        else
                        {
                            rootPage.NotifyUser("There are no files scanned from the Feeder.", NotifyType.ErrorMessage);
                        }
                    }
                    else
                    {
                        ModelDataContext.ScenarioRunning = false;
                        rootPage.NotifyUser("The selected scanner does not report to be equipped with a Feeder.", NotifyType.ErrorMessage);
                    }
                }
            }
            catch (OperationCanceledException)
            {
                Utils.DisplayScanCancelationMessage();
            }
            catch (Exception ex)
            {
                Utils.OnScenarioException(ex, ModelDataContext);
            }
        }