[WinRT] How to scan documents in a Windows 8.1 application ?

May 13th, 2015 | Posted by Tom in .NET | Article | Windows 8 | WinRT | Read: 2,765

WinRT introduced a set of new APIs that developers can use to manipulate scanners (compatible with WIA, Windows Imaging Acquisition) on Windows 8.1 applications. As you’ll see, the API is pretty straightforward so let’s take a look at how to implement this feature in an app.

First, you need to create and start a watcher, used to listen for any changes on the specify devices’ kind (VideoCapture, AudioCapture, ImageScanner, etc.):

this._watcher = DeviceInformation.CreateWatcher(DeviceClass.ImageScanner);
this._watcher.Added += OnScannerAdded;
this._watcher.Start();

Here, I subscribe to the event Added to be notified when a new ImageScanner is detected by the watcher but I can also work with the events Removed, Stopped, Updated, etc.

From the event handler, I’m able to retrieve the instance of the ImageScanner that was just detected:

private async void OnScannerAdded(DeviceWatcher sender, DeviceInformation args)
{
    if (args.Name.Contains("Dell"))
    {
        this._watcher.Stop();

        var scanner = await ImageScanner.FromIdAsync(args.Id);
    }
}

Then, from the scanner, I can launch the scan of the documents, using an automatic configuration on which it’s possible to change some properties (portrait/landscape, Dpi, etc.) or by specifying which kind of scanner is used: Flatbed or Feeder:

if (scanner.IsScanSourceSupported(ImageScannerScanSource.Flatbed))
{
    // Set the scan file format to PNG, if available
    if (scanner.FlatbedConfiguration != null)
    {
        if (scanner.FlatbedConfiguration.IsFormatSupported(ImageScannerFormat.Png))
        {
            scanner.FlatbedConfiguration.Format = ImageScannerFormat.Png;
        }

        scanner.FlatbedConfiguration.DesiredResolution = new ImageScannerResolution { DpiX = 200, DpiY = 200 };
    }

    this._cancellationToken = new CancellationTokenSource();

    var scanTask = scanner.ScanFilesToFolderAsync(ImageScannerScanSource.Flatbed, Windows.Storage.ApplicationData.Current.LocalFolder);
    scanTask.Progress = (info, progressInfo) => Debug.WriteLine("Page {0}", progressInfo);

    var scanResults = await scanTask.AsTask(this._cancellationToken.Token);
}

As you can see, you can also be notified of the progression by using the Progress event of the Task!

On the scan is over, you’ll get, as a result, a collection of StorageFile corresponding to all the files that has been scanned:

for (int i = 0; i < scanResults.ScannedFiles.Count; i++)
{
    var file = scanResults.ScannedFiles[i];
}

 

As you can see, performing scanning’s operations is really simple and a lot of options can be configured. For now, I’ve used this on a flatbad scanner (and I’ve planned to use it with a different kind) and I’ve not been disappointed by the results!

 

Happy coding!

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

Add Comment Register



Leave a Reply