Monday, March 04, 2013

Getting images from scanner, web camera or from other still imaging devices using WIA

Lot of our customers asking us about a way to acquire images from scanner and other imaging devices.

There are 2 ways to acquire image in Windows:

  • via TWAIN interface (used since 1992);
  • via WIA (currently is recommended by Microsoft over TWAIN/STI because of better user experience)
To acquire images using TWAIN we have open-source SDK called Bytescout Scan SDK which is available for free download at http://bytescoutscansdk.codeplex.com/

Scan SDK is free for use in both commercial and non-commercial apps

What about WIA? In the latest version of BarCode Reader SDK we have implemented new WIAImageScanner class that allows to acquire images and works on Windows XP, Vista, 7, Windows 8 for both x86 and x64 platforms.

The latest evaluation version of BarCode Reader SDK is available for download here
Registered users should download using their "secret" link instead (to get full version)

How to use this new WIAImageScanner class:


C# code sample:

using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Bytescout.BarCodeReader;

// This example demonstrates camera image acquiring using Windows Image Acquisition (WIA).
// using built-in WIAImageScanner class from barCode Reader SDK

namespace BarcodeFromWebCam
{
 static class Program
 {
  static void Main()
  {
            // create WIA scanner object
            WIAImageScanner wiaScanner = new WIAImageScanner();

            wiaScanner.OutputFileNameTemplate = "BarCodeReader-scanned";
            wiaScanner.OutputImageFormat = WiaImageFormatType.PNG;
            wiaScanner.ImageQuality = WiaImageBias.MaximizeQuality;
            wiaScanner.ImageIntent = WiaImageIntent.UnspecifiedIntent;
            wiaScanner.ShowDeviceSelectionDialog = true;

            try
            {
                // run acquire and exit if canceled or zero images
                if (!wiaScanner.Acquire())
                    return;

            }
            catch (Exception E)
            {
                string message = E.Message;
                if (E.InnerException != null)
                    message = message + "\r\n\r\n" + E.InnerException.Message;
                MessageBox.Show("Error while acquiring images:\r\n\r\n" + message);
                return;
            }

            // Read barcode:
            Reader barcodeReader = new Reader();
            FoundBarcode[] barcodes = barcodeReader.ReadFrom(wiaScanner.OutputFiles[0]);

            if (barcodes.Length > 0)
            {
                StringBuilder builder = new StringBuilder();

                foreach (FoundBarcode barcode in barcodes)
                    builder.AppendLine(String.Format("Found barcode with type '{0}' and value '{1}'", barcode.Type, barcode.Value));

                MessageBox.Show(builder.ToString());
            }
            else
            {
                MessageBox.Show(wiaScanner.OutputFiles[0] + "\r\n\r\nCould not find any barcode.");
            }


   }
 }
}


Visual Basic .NET sample code:

Imports System.IO
Imports System.Text
Imports System.Windows.Forms
Imports Bytescout.BarCodeReader

' This example demonstrates camera image acquiring using Windows Image Acquisition (WIA).
' using built-in WIAImageScanner class from barCode Reader SDK

NotInheritable Class Program
 Private Sub New()
 End Sub
    Friend Shared Sub Main()

        ' use WIA barcode reader scanner
        Dim wiaScanner As WIAImageScanner = New WIAImageScanner

        wiaScanner.OutputFileNameTemplate = "BarCodeReader-scanned"
        wiaScanner.OutputImageFormat = WiaImageFormatType.PNG
        wiaScanner.ImageQuality = WiaImageBias.MaximizeQuality
        wiaScanner.ImageIntent = WiaImageIntent.UnspecifiedIntent
        wiaScanner.ShowDeviceSelectionDialog = True

        Try
            ' run acquire and exit if canceled or zero images
            If Not wiaScanner.Acquire() Then
                Return
            End If

        Catch E As Exception
            Dim message As String = E.Message
            If E.InnerException IsNot Nothing Then
                message = message + "\r\n\r\n" + E.InnerException.Message
                MessageBox.Show("Error while acquiring images:\r\n\r\n" + message)
                Return
            End If
        End Try

        ' Read barcode:
        Dim barcodeReader As New Reader()
        Dim barcodes As FoundBarcode() = barcodeReader.ReadFrom(wiaScanner.OutputFiles(0))

        If barcodes.Length > 0 Then
            Dim builder As New StringBuilder()

            For Each barcode As FoundBarcode In barcodes
                builder.AppendLine([String].Format("Found barcode with type '{0}' and value '{1}'", barcode.Type, barcode.Value))
            Next

            MessageBox.Show(builder.ToString())
        Else
            MessageBox.Show(wiaScanner.OutputFiles(0) + "" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & "Could not find any barcode.")


        End If

    End Sub
End Class