This tutorial shows how to perform business card detection and recognition in a C# .NET 6 application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to perform Business Card recognition in a C# .NET 6 Console Application using the LEADTOOLS Business Card Recognition SDK. |
Completion Time | 15 minutes |
Visual Studio Project | Download tutorial project (1 KB) |
Platform | C# .NET 6 Console Application |
IDE | Visual Studio 2022 |
Runtime Target | .NET 6 or Higher |
Development License | Download LEADTOOLS |
Try it in another language |
|
Get familiar with the basic steps of creating a project and loading an image by reviewing the Add References and Set a License and Load and Save Images tutorials, before working on this tutorial.
Start with a copy of the project created in the Load and Save Images tutorial. If you do not have that project, follow the steps in that tutorial to create it.
The references needed depend upon the purpose of the project. References can be added via NuGet packages.
This tutorial requires the following NuGet package:
Leadtools.Document.Sdk
For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.
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:
With the project created, the references added, the license set, and the load image code added, coding can begin. The image save code is not necessary for this tutorial, so that code can be commented out or deleted.
In the Solution Explorer, open Program.cs
. Add the following statements to the using
block at the top of Program.cs
.
using System;
using System.IO;
using Leadtools;
using Leadtools.Barcode;
using Leadtools.Codecs;
using Leadtools.Forms.Commands;
using Leadtools.Ocr;
Add a new method to the Program
class named ExtractBusinessCard(RasterImage image)
. Call this method after the call to the LoadImage()
method inside the Main()
method, as shown below.
static void Main(string[] args)
{
if (!InitLEAD())
Console.WriteLine("Error setting license");
else
Console.WriteLine("License file set successfully");
RasterImage image = LoadImage(@"C:\LEADTOOLS23\Resources\Images\business_card_sample.jpg");
ExtractBusinessCard(image);
}
Add the below code to the ExtractBusinessCard()
method to read the business card information from the loaded RasterImage and display it to the console.
static void ExtractBusinessCard(RasterImage image)
{
// Start up the OCR engine
using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD))
{
// Start up barcode
BarcodeEngine barcodeEngine = new BarcodeEngine();
ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS23\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("************************************");
}
}
}
}
}
To handle the files using MemoryStream
, replace the existing code in the Main()
method with the following:
static void Main(string[] args)
{
if (!InitLEAD())
Console.WriteLine("Error setting license");
else
Console.WriteLine("License file set successfully");
using(RasterCodecs codecs = new RasterCodecs())
{
string filepath = @"C:\LEADTOOLS23\Resources\Images\business_card_sample.jpg";
byte[] data = File.ReadAllBytes(filepath);
using (MemoryStream ms = new MemoryStream(data))
{
RasterImage image = codecs.Load(ms);
ExtractBusinessCard(image);
}
}
//SaveImage(image, @"C:\temp\output.jpg");
}
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the console appears and the application displays the fields from the business card inside the loaded RasterImage. This tutorial uses the sample from the following file path: C:\LEADTOOLS23\Resources\Images\business_card_sample.jpg
This tutorial showed how to extract business card information from an image using the BusinessCardReader
class.