Have Fun with the WPF Demo of .NET TWAIN 5.0
Dynamic .NET TWAIN 5.0 has been released for a while. To help users to quickly grasp APIs, a brand-new API demo, written in WPF with C# & VB.NET, is included. In this tutorial, we would like to show the anatomy of this application. Let’s glance at the screenshot as a warm up. As you can see, the functionalities of the demo include scanner control, image loading, barcode recognition, OCR, image manipulation and processing.
How to install Dynamic .NET TWAIN 5.0?
Visit Dynamic .NET TWAIN page and get the installation package by clicking “_Download“_ button. Follow the InstallShield Wizard step by step.
Where is the API demo?
The demo source code is located at “…\Dynamsoft\Dynamic .NET TWAIN 5.0 Trial\Samples\C# Samples\VS 12” and “…\Dynamsoft\Dynamic .NET TWAIN 5.0 Trial\Samples\VB .NET Samples\VS 12\WpfControlsDemo”. You can choose your preferred programming language, C# or VB.NET.
What does the project look like in Visual Studio?
The reference DynamicDotNetTWAIN.Wpf.dll is located at “…\Dynamsoft\Dynamic .NET TWAIN 5.0 Trial\Samples\Bin”. It will be copied to your project folder when you run the Visual Studio solution file “WpfControlsDemo.sln”. If you want to create your own project, don’t forget to add it. In this project, we have created three windows. The main window is “Window1.xaml”.
How to use the relevant APIs to implement following functions?
All implementation logics are same no matter which language you choose. Let’s illustrate with C#.
Initialize
Set the paths of barcode library, OCR library and OCR language package.
dynamicDotNetTwainThum.BarcodeDllPath = dynamicDotNetTwainDirectory + @"Redistributable\BarcodeResources\"; // barcode library
dynamicDotNetTwainThum.OCRDllPath = dynamicDotNetTwainDirectory + @"Redistributable\OCRResources\"; // OCR library
dynamicDotNetTwainThum.OCRTessDataPath = dynamicDotNetTwainDirectory + @"Redistributable\"; // OCR language package
Scan
The “ScanWindow.xaml” is used for scan window.
To run this window from the main window:
ScanWindow scanWnd = new ScanWindow();
scanWnd.Owner = this;
scanWnd.TWAIN = dynamicDotNetTwainThum;
scanWnd.ShowDialog();
To acquire the TWAIN sources, and scan the image:
for (int i = 0; i < twain.SourceCount; i ++)
{
cbxSources.Items.Add(twain.SourceNameItems((short)i)); // get & add source names
}
twain.ScanInNewThread = true;
twain.SelectSourceByIndex(cbxSources.SelectedIndex);
twain.OpenSource(); // open selected source
twain.IfShowUI = ckbShowUI.IsChecked.Value;
twain.IfFeederEnabled = ckbADF.IsChecked.Value;
twain.IfDuplexEnabled = ckbDuplex.IsChecked.Value;
if (rbBW.IsChecked.Value)
{
twain.PixelType = Dynamsoft.DotNet.TWAIN.Enums.TWICapPixelType.TWPT\_BW;
twain.BitDepth = 1;
}
else if (rbGrey.IsChecked.Value)
{
twain.PixelType = Dynamsoft.DotNet.TWAIN.Enums.TWICapPixelType.TWPT\_GRAY;
twain.BitDepth = 8;
}
else if (rbColorful.IsChecked.Value)
{
twain.PixelType = Dynamsoft.DotNet.TWAIN.Enums.TWICapPixelType.TWPT\_RGB;
twain.BitDepth = 24;
}
twain.AcquireImage(); // acquire image from scanner
Load
When loading an image from the disk, we can get the thumbnail from the original image.
To get the thumbnail:
dynamicDotNetTwainThum.LoadImage(strFileName);
To display the large image:
Clipboard.Clear();
dynamicDotNetTwainThum.CopyToClipboard(dynamicDotNetTwainThum.CurrentImageIndexInBuffer);
dynamicDotNetTwainView.RemoveAllImages();
dynamicDotNetTwainView.LoadDibFromClipboard(); // load the large image
Clipboard.Clear();
OCR
Let’s load an image file with text, and try OCR.
dynamicDotNetTwainThum.IfShowCancelDialogWhenBarcodeOrOCR = true;
dynamicDotNetTwainThum.OCRResultFormat = Dynamsoft.DotNet.TWAIN.OCR.ResultFormat.Text;
dynamicDotNetTwainThum.OCRLanguage = "eng";
byte[] bytes = dynamicDotNetTwainThum.OCR(dynamicDotNetTwainThum.CurrentSelectedImageIndicesInBuffer); // recognize the buffered image
if (bytes != null)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "Text File(\*.txt)|\*.txt";
if (dlg.ShowDialog().Value)
{
System.IO.File.WriteAllBytes(dlg.FileName, bytes); // write recognized text to text file
}
}
Barcode
We choose QR code for testing.
dynamicDotNetTwainThum.IfShowCancelDialogWhenBarcodeOrOCR = true;
Result[] results = dynamicDotNetTwainThum.ReadBarcode(dynamicDotNetTwainThum.CurrentImageIndexInBuffer, Dynamsoft.DotNet.TWAIN.Enums.Barcode.BarcodeFormat.All);
string strResult = results.Length + " total barcode found." + "\r\n";
for (int i = 0; i < results.Length; i++)
{
strResult += "Result " + (i + 1) + "\r\n " + "Barcode Format: " + results[i].BarcodeFormat + " Barcode Text: " + results[i].Text + "\r\n";
}
[](./img/2014/01/load.png)
To experience more interesting functions, just download the Dynamic .NET TWAIN 5.0 and have fun. Hopefully everyone can enjoy it. Please follow our Twitter, Facebook and Google+ if you have something to share with us.