WIA C# and VB.NET samples - ByteScout

WIA C# and VB.NET samples

WIA C# sample and WIA VB.NET sample will show you how to acquire images via WIA scanner to recognize barcodes. 

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. Find source code samples of using this class below.

C#

[vb]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.”);
}

}
}
}[/vb]

Visual Basic .NET

[vbnet]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
[/vbnet]

The demo video shows how to read Code39 barcode with ByteScout Barcode Reader SDK:

Tutorials:

prev
next