Getting Images from Scanner, Web Camera or from Other Still Imaging Devices using WIA - ByteScout
Announcement
Our ByteScout SDK products are sunsetting as we focus on expanding new solutions.
Learn More Open modal
Close modal
Announcement Important Update
ByteScout SDK Sunsetting Notice
Our ByteScout SDK products are sunsetting as we focus on our new & improved solutions. Thank you for being part of our journey, and we look forward to supporting you in this next chapter!
  • Home
  • /
  • Blog
  • /
  • Getting Images from Scanner, Web Camera or from Other Still Imaging Devices using WIA

Getting Images from Scanner, Web Camera or from Other Still Imaging Devices using WIA

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

There are 2 ways to acquire an 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 an 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 a new WIAImageScanner class that allows us 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 the 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 + "rnrn" + E.InnerException.Message;
MessageBox.Show("Error while acquiring images:rnrn" + 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] + "rnrnCould 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 + "rnrn" + E.InnerException.Message
MessageBox.Show("Error while acquiring images:rnrn" + 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
   

About the Author

ByteScout Team ByteScout Team of Writers ByteScout has a team of professional writers proficient in different technical topics. We select the best writers to cover interesting and trending topics for our readers. We love developers and we hope our articles help you learn about programming and programmers.  
prev
next