C#
Install Inlite.BarcodeReader.IP NuGet packages
This package installs ClearImageNet.70.dll assembly, which contains two namespaces.
- Inlite.ClearImageNet namespace implements high-level functions recommended to use in new applications. Code examples use only this namespace.
- Inlite.ClearImage namespace is substantially compatible with the COM API and is specifically designed to ease the migration from ClearImage COM API applications to .NET. It also supports a few low-level functions.
ClearImage Project Development
Use Visual Studio C# ClearImage example project or follow the code examples below.
Extend functionality
Combine code examples on this page with methods described in ClearImage API documentation in developing your project. Contact [email protected] if you have questions.
Exception handling
ClearImage .NET API reports errors as an Exception object.
try {
// Call ClearImage .NET API
}
catch(Exception ex) {
Console.WriteLine("ERROR: " + ex.Message);
}Barcode Reading
Read 1D barcodes from a page
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()); }
}Read 2D Barcode from a page
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;
// 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()); }
}Read Postal Barcodes from a page
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()); }
}Read Driver's License barcode
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()); }
}Read from a multi-page image file
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()); }
}Read from a stream
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()); }
}Image Processing
Repair image page
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()); }
}
Repair all images in a multi-page file
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()); }
}Repair all images in a stream
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; }
}