This tutorial shows how to create a C# Windows Console application that uses the LEADTOOLS SDK to perform Business Card detection and recognition.
Overview | |
---|---|
Summary | This tutorial covers how to use LEADTOOLS Business Card Recognition SDK technology in a C# Windows Console Application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (3 KB) |
Platform | C# Windows Console Application |
IDE | Visual Studio 2017, 2019 |
Development License | Download LEADTOOLS |
Try it in another language |
|
Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License and Load and Save Images tutorials, before working on the Extract Business Card Information - Console C# tutorial. Saving the image is not necessary for the tutorial, so saving portion can be commented out.
In Visual Studio, create a new C# Windows Console project, and add the following necessary LEADTOOLS references.
The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both). For this project, the following references are needed:
If using NuGet references, this tutorial requires the following NuGet package:
Leadtools.Document.Sdk
If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS21\Bin\Dotnet4\x64
:
Leadtools.dll
Leadtools.Barcode.dll
Leadtools.Barcode.QrRead.dll
Leadtools.Codecs.dll
Leadtools.Codecs.Cmp.dll
Leadtools.Document.Writer.dll
Leadtools.Forms.Commands.dll
Leadtools.Ocr.dll
Leadtools.Ocr.LEADEngine.dll
For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.
The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.
There are two types of runtime licenses:
Note
Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.
With the project created, the references added, and the license set, coding can begin.
In the Solution Explorer, open Program.cs
. Add the following statements to the using
block at the top.
// Using block at the top
using System;
using System.IO;
using Leadtools;
using Leadtools.Barcode;
using Leadtools.Codecs;
using Leadtools.Forms.Commands;
using Leadtools.Ocr;
Add a new method called ExtractBusinessCard(RasterImage image)
and call it inside the Main
method after LoadImage()
method. Add the below code to read the business card information from the loaded RasterImage.
static void Main(string[] args)
{
SetLicense();
RasterImage image = LoadImage(@"C:\LEADTOOLS21\Resources\Images\business_card_sample.jpg");
ExtractBusinessCard(image);
//SaveImage(image, @"C:\temp\output.jpg");
Console.WriteLine("Press any key to exit...");
Console.ReadKey(true);
}
static void ExtractBusinessCard(RasterImage image)
{
//Start up Ocr engine
using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD))
{
//Start up barcode
BarcodeEngine barcodeEngine = new BarcodeEngine();
ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS21\Bin\Common\OcrLEADRuntime");
// Initialize the BusinessCardReader class
BusinessCardReader BCReader = new BusinessCardReader(ocrEngine, barcodeEngine);
BCProcessStatus status = BCReader.Process(image);
if (status == BCProcessStatus.BlurDetected)
Console.WriteLine("Blur detected in image.");
else if (status == BCProcessStatus.GlareDetected)
Console.WriteLine("Glare detected in image.");
else if (status == BCProcessStatus.Failed)
Console.WriteLine("Failed to recognize image.");
else if (status == BCProcessStatus.Success)
{
if (BCReader.Results != null)
{
foreach (var res in BCReader.Results)
{
LeadRect bounds = res.Value.Bounds;
Console.WriteLine(string.Format("Field Name : {0}", res.Key));
Console.WriteLine(string.Format("Field Value : {0}", res.Value.Value));
Console.WriteLine(string.Format("Field Confidence : {0}", res.Value.Confidence));
Console.WriteLine(string.Format("Field Bounds : {0},{0},{0},{0}", bounds.X.ToString(), bounds.Y.ToString(), bounds.Width.ToString(), bounds.Height.ToString()));
Console.WriteLine("************************************");
}
}
}
}
}
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the application runs and displays the fields from the business card inside the loaded RasterImage. This tutorial uses this sample image.
The expected output should look like this:
This tutorial showed how to extract business card information from an image. Also it covered how to use the BusinessCardReader
class.