This tutorial shows how to detect and extract ID card information from a given image in a C# .NET 6 application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to find and extract ID Card features from an image in a C# .NET 6 Console application. |
Completion Time | 30 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 |
Get familiar with the basic steps of creating a project and loading an image by reviewing theAdd References and Set a LicenseandLoad and Save Imagestutorials, before working on theDetect and Extract ID Card Information - C# .NET 6tutorial.
Start with a copy of the project created in theLoad and Save Imagestutorial. 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 packages:
Leadtools.Document.Sdk
For a complete list of which DLL files are required for your application, refer toFiles 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 toSetting a Runtime License.
There are two types of runtime licenses:
Note
Adding LEADTOOLS NuGet references and setting a license are covered in more detail in theAdd References and Set a Licensetutorial.
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 theSolution Explorer, openProgram.cs
. Add the following statements to theusing
block at the top ofProgram.cs
.
usingSystem;
usingLeadtools;
usingLeadtools.Codecs;
usingLeadtools.Forms.Commands;
usingLeadtools.Ocr;
Add a new method to theProgram
class namedReadIDCard(RasterImage image)
, load the sample image with RasterCodecs and pass theRasterImage
class to the method.
For the purposes of this tutorial the following image is used:
staticvoidMain(string[] args)
{
if(!InitLEAD())
Console.WriteLine("Error setting license");
else
Console.WriteLine("License file set successfully");
using(RasterCodecs codecs =newRasterCodecs())
{
RasterImage image = codecs.Load(@"
\LEADTOOLS22\Resources\Images\License_SAMPLE.PNG" );ReadIDCard(image);
}
}
Add the code below to theReadIDCard()
method to process the given image and output the ID values if detected.
staticvoidReadIDCard(RasterImage image)
{
using(IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD))
{
varbuffer = Path.Combine(@"
\LEADTOOLS22\Bin\Common\OcrLEADRuntime" ,"LEAD.Binarize.bin");ocrEngine.Startup(null,null,null,null);
using(IDFrameReader idFrameReader =newIDFrameReader(File.ReadAllBytes(buffer), ocrEngine))
{
varresults = idFrameReader.Process(image);
if(results.Ready)
foreach(varresinidFrameReader.Results)
Console.WriteLine($"{res.Key} : {res.Value}");
else
Console.WriteLine("No ID found in this image");
}
}
}
If you would like to detect and extract the data using memory stream, then insert the following code into theMain(string[] args)
method:
stringfilename =@"C:\LEADTOOLS22\Resources\Images\License_SAMPLE.PNG";
using(RasterCodecs codecs =newRasterCodecs())
{// To load by memory stream and extract the data
byte[] bytes = File.ReadAllBytes(filename);
using(MemoryStream ms =newMemoryStream(bytes))
{
ms.Position = 0;
// codecs.Load(ms);
RasterImage image = codecs.Load(ms);
ReadIDCard(image);
}
}
Note
This will replace the existing code under the
SetLicense()
call.
Run the project by pressingF5, or by selectingDebug -> Start Debugging.
If the steps were followed correctly, the console appears and the application detects the ID information and displays it to the console.
This tutorial showed how to load an image and run license recognition using theIDFrameReader
class.