Working with the New Scanning APIs in Windows 8.1

Introduction

With Windows 8.1, Microsoft has introduced a lot of new programmatic capabilities. One of the features announced as part of Build 2013 was the introduction of APIs that would allow Windows Store applications to work with scanners.

The scanning APIs are located in the Windows.Devices.Scanners namespace. The scanning APIs are built on top of native Windows Image Acquisition (WIA) APIs. Only scanners that are installed with WIA drivers can be used through the scanning APIs.

To build support for scanning in a Windows Store application, you need to list all available scanners by retrieving all devices with a DeviceClass type of ImageScanner.

Hands On

Let us build a simple Windows Store application that works with scanners.

Create a new Windows Store application in the Visual Studio 2013 Express Preview for Windows, titled Windows8.1ScanningDemo.

New Project
New Project

Next, on MainPage.xaml, add two buttons titled “Get Scanner” and “Start Scanning”.

Open the code behind for MainPage.xaml.cs, and include the namespace for Scanners: Windows.Devices.Scanners

using Windows.Devices.Scanners;

Now, we will add an instance ImageScanner as a class variable.

public sealed partial class MainPage : Page
    {
        ImageScanner myImageScanner;
        public MainPage()
        {
            this.InitializeComponent();
        }
    }

We will now retrieve a scanner object. Note that only scanners with a WIA driver can be accessed with the APIs.

Next we will implement the event handler for the click event for the “Get Scanner” button.

For the click event handler, we will retrieve the device selector string for scanners.

Then, we will list all devices that match the device selector. If there is more than one device, we will list the first scanner, else we will report that no devices are present.

private async void buttonGetScannerObject_Click(object sender, RoutedEventArgs e)
        {
            string deviceSelector = ImageScanner.GetDeviceSelector();
            var deviceInfoCollection =await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(deviceSelector, null);
            if (deviceInfoCollection.Count != 0)
            {
                myImageScanner = await ImageScanner.FromIdAsync(deviceInfoCollection[0].Id);
            }
            else
            {
                Windows.UI.Popups.MessageDialog md = new Windows.UI.Popups.MessageDialog("No scanners found")
                await md.ShowAsync();
            }
        }
 

Next, we will implement the event handler, which will actually scan and store the scanned image as part of the click event for “Start scanning” button.

For this example, we will store the image in the Pictures library.

private async void buttonStartScanning_Click(object sender, RoutedEventArgs e)
        {
            Windows.Storage.StorageFolder sf = Windows.Storage.KnownFolders.PicturesLibrary;
            var result = await myImageScanner.ScanFilesToFolderAsync(ImageScannerScanSource.Default, sf);
        }
 

If you want to build the functionality to stop scanning while in progress, you can use a CancellationTokenSource class in your application. The logic is to get a token instance when you start scanning, and when cancelation of scanning is invoked, you can call the Cancel() property on the token instance.

You can now build and compile the application. To test the application, you will need a scanner connected to the Windows 8.1 device on which you want to run the application. If you have trouble following along, you can download a sample of the code below.

Summary

In this article, we learned how to build a simple Windows Store application that can work with scanners. To keep the topic simple, we used the system defaults. Readers are encouraged to try out the other combinations of the APIs to test advanced scenarios.

About the author

Vipul Patel is a Program Manager currently working at Amazon Corporation. He has formerly worked at Microsoft in the Lync team and in the .NET team (in the Base Class libraries and the Debugging and Profiling team). He can be reached at vipul.patel@hotmail.com



Related Articles

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • When individual departments procure cloud service for their own use, they usually don't consider the hazardous organization-wide implications. Read this paper to learn best practices for setting up an internal, IT-based cloud brokerage function that service the entire organization. Find out how this approach enables you to retain top-down visibility and control of network security and manage the impact of cloud traffic on your WAN.

  • U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there is simply not enough quality talent to go around. Tiempo Development is a nearshore software development company. Our headquarters are in AZ, but we are a pioneer and leader in outsourcing to Mexico, based on our three software development centers there. We have a proven process and we are experts at providing our customers with powerful solutions. We transform ideas into reality.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date