Read 1D barcodes (Code 39, Code 128, etc.) and 2D barcodes (PDF417, DataMatrix, QR) from image and PDF files. Decode and extract user information form US and Canada driver licenses. Automatically repair images: auto-deskew, auto-rotate, etc.
$ dotnet add package ClearImage.BarcodeReader.IPClearImage SDK a development toolkit to add barcode recognition to your application. Develop a Web Service, console, or desktop application in a language of your choice, including C#, C++, Java, PHP, and others. Deploy your application to your preferred target or device, including a physical computer, Cloud VM, container, or microservice.
This example reads code 39 and code 128 barcodes. Set barcode types used in your application. Setting reader.Auto1D=true; automatically finds barcode type, but it is slower and is not recommended for production.
using Inlite.ClearImageNet;
void ReadBarcode1D_page(string fileName, int page) {
try {
using(BarcodeReader reader = new BarcodeReader()) {
reader.Code39 = true; reader.Code128 = true;
// reader.Auto1D = true;
Barcode[] barcodes = reader.Read(fileName, page);
foreach(Barcode barcode in barcodes)
Console.WriteLine($"Barcode type: {barcode.Type} Text: {barcode.Text}");
}
}
catch(Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); }
}
This example reads PDF417, DataMatrix, and QR code. 2D barcodes might contain binary data available in Barcode.Data property.
using Inlite.ClearImageNet;
void ReadBarcode2D_page(string fileName, int page) {
try {
using(BarcodeReader reader = new BarcodeReader()) {
reader.Pdf417 = true; // <= Read PDF417 barcodes
// reader.DataMatrix = true;
// reader.QR = true;
Barcode[] barcodes = reader.Read(fileName, page);
foreach(Barcode barcode in barcodes)
Console.WriteLine($"Barcode type: {barcode.Type} Text: {barcode.Text}");
}
}
catch(Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); }
}
This example reads Postal Barcodes from an image file page.
using Inlite.ClearImageNet;
void ReadPostal_page(string fileName, int page) {
try {
using(BarcodeReader reader = new BarcodeReader()) {
reader.FourState = true; // <= Read postal barcodes
Barcode[] barcodes = reader.Read(fileName, page);
foreach(Barcode barcode in barcodes) {
switch(barcode.Type) {
case BarcodeType.UspsIntelligentMail:
Console.Write(" US Intelligent Mail"); break;
case BarcodeType.BpoPostcode:
Console.Write("UK Royal Mail"); break;
case BarcodeType.AustralianPost:
Console.Write("Australian Mail"); break;
case BarcodeType.SingaporePost:
Console.Write("Singapore Mail"); break;
case BarcodeType.FourState:
Console.Write("ForState "); break;
}
Console.WriteLine(" Text: " + barcode.Text);
}
}
}
catch(Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); }
}
This example reads the Driver's License barcode from an image file page.
using Inlite.ClearImageNet;
void ReadDriverLicBarcode_page(string fileName, int page) {
try {
using(BarcodeReader reader = new BarcodeReader()) {
reader.DrvLicID = true;
Barcode[] barcodes = reader.Read(fileName, page);
foreach(Barcode barcode in barcodes) {
Inlite.Data.DLDecoder decoder = new Inlite.Data.DLDecoder();
if(decoder.Decode(text) != "") {
Console.WriteLine("last: " + decoder.last);
Console.WriteLine("first: " + decoder.first);
Console.WriteLine("middle: " + decoder.middle);
Console.WriteLine("dob: " + decoder.dob);
Console.WriteLine("eyes: " + decoder.eyes);
Console.WriteLine("hair: " + decoder.hair);
Console.WriteLine("sex: " + decoder.sex);
Console.WriteLine("height: " + decoder.height);
Console.WriteLine("street: " + decoder.street);
Console.WriteLine("city: " + decoder.city);
Console.WriteLine("state: " + decoder.state);
Console.WriteLine("postal: " + decoder.postal);
Console.WriteLine("country: " + decoder.country);
Console.WriteLine("id: " + decoder.id);
Console.WriteLine("issued: " + decoder.issued);
Console.WriteLine("expires: " + decoder.expires);
}
}
}
}
catch(Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); }
}This example reads barcodes from a single-page file or all pages of a multi-page image file.
using Inlite.ClearImageNet;
void ReadBarcode1D_file(string fileName) {
try {
using(BarcodeReader reader = new BarcodeReader()) {
reader.Code39 = true; reader.Code128 = true;
Barcode[] barcodes = reader.Read(fileName); // <== Read from all pages
foreach(Barcode barcode in barcodes)
Console.WriteLine($"Barcode type: {barcode.Type} Barcode page: {barcode.Page} Text: {barcode.Text}");
}
}
catch(Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); }
}This example reads barcodes from a stream. The stream contains the content of a single image file. For example, if a file is stored in a database.
using Inlite.ClearImageNet;
void ReadBarcode1D_stream(Stream stream) {
try {
using(BarcodeReader reader = new BarcodeReader()) {
reader.Code39 = true; reader.Code128 = true;
Barcode[] barcodes = reader.Read(stream); // <== read from a stream
foreach(Barcode barcode in barcodes)
Console.WriteLine($"Barcode type: {barcode.Type} Barcode page: {barcode.Page} Text: {barcode.Text}");
}
}
catch(Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); }
}This example uses some of the most popular image-processing functions. In a production application, the specific sequence of functions should be selected based on specific application needs.
using Inlite.ClearImageNet;
static void RepairPage(ImageEditor editor) {
editor.AutoDeskew();
editor.AutoRotate();
editor.AutoCrop(10, 10, 10, 10); // Crop to 10 pixels on each side
// editor.AdvancedBinarize(); // Convert to bi-tonal an image with complex background patterns
editor.ToBitonal();
editor.BorderExtract(BorderExtractMode.deskewCrop); // Deskew and crop based on black border
editor.RemovePunchHoles();
editor.SmoothCharacters();
editor.CleanNoise(3); // Clean black noise of 3 pixels
// editor.CleanNoise // Clean black and white noise
// (CleanNoiseFlags.black | CleanNoiseFlags.white, 3, 3, 10);
editor.ReconstructLines(LineDirection.horzAndVert);
}
void Repair_page(string fileName, int page, string fileOut) {
try {
using(ImageEditor editor = new ImageEditor()) {
editor.Image.Open(fileName, page);
// Do image repair
RepairPage(editor);
// Save results
editor.Image.SaveAs(fileOut, Inlite.ClearImage.EFileFormat.ciEXT);
}
}
catch(Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); }
}
This example repairs all images in a multi-page file and saves the result in the fileOut file.
using Inlite.ClearImageNet;
private static void _OnEditPage(object sender, EditPageEventArgs e) {
// e.cancel = true; // Abort file processing
// e.skipPage = true; // Remove this page from the output
RepairPage(e.Editor); // See this method in a single page example
}
void Repair_file(string fileName, string fileOut) {
try {
using(ImageEditor editor = new ImageEditor()) {
bool ret = editor.Edit(fileName, _OnEditPage, fileOut, ImageFileFormat.outputFileExtension, true);
}
}
catch(Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); }
}This example repairs all images in an input stream and saves the output stream using a specified file format. Streams contain the content of a single image file. For example, if a file is stored in a database.
using Inlite.ClearImageNet;
private static void _OnEditPage(object sender, EditPageEventArgs e) {
RepairPage(e.Editor); // See this method in a single page example
}
Stream Repair_stream(Stream stream, ImageFileFormat output_format) {
try {
using(ImageEditor editor = new ImageEditor()) {
MemoryStream msOut = editor.Edit(stream, _OnEditPage, output_format);
return msOut;
}
}
catch(Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); return null; }
}