Read 1D barcodes from an image page
The following examples demonstrate the basic technique to read 1D barcodes from an image file page.
Code39 and Code 128 are the expected barcode types in these examples. If other types (the real term is symbologies) are required, then select from the supported types from the .NET API or COM API lists. ClearImage supports the capability of Automatic symbology recognition, however, this capability is beneficial primarily in the devlopment phase of the project. Once projects enter production, they typically utilize one or two symbologies. Selecting for only those symbologies makes the recognition faster, and reduces the validation effort in your application.
C#
123456789101112131415161718
using Inlite.ClearImageNet;// . . .void ReadBarcodes1D(string fileName, int page){ BarcodeReader reader = null; try { reader = new BarcodeReader(); // Create and configure reader reader.Code39 = true; reader.Code128 = true; Barcode[] barcodes = reader.Read(fileName, page); // Read barcodes foreach (Barcode barcode in barcodes) // Process results { Console.WriteLine("Barcode type: " + barcode.Type.ToString() + " Text: " + Environment.NewLine + barcode.Text); } } catch (Exception ex) { Console.WriteLine("Exception: " + ex.ToString()); } finally { if (reader != null) reader.Dispose(); } // ClearImage 9 and latter. Free image memory.}
VB
123456789101112131415161718
Imports Inlite.ClearImageNet' . . .Sub ReadBarcode1D(ByVal fileName As String, ByVal page As Integer) Try Dim reader As New BarcodeReader() ' Select barcode type(s) to read reader.Code128 = True reader.Code39 = True Dim barcodes As Barcode() = reader.Read(fileName, page) ' Process results For Each bc As Barcode In barcodes Console.Write(bc.Text) ' Use barcode text OR ProcessBarcode(bc) ' do other processing Next Catch ex As Exception ProcessException(ex) End TryEnd Sub
C++
123456789101112131415161718
void ReadBarcode1D (const char *fileName, const long page) { try { // configure reader ICiBarcodeProPtr reader = Ci->CreateBarcodePro (); reader->Type = (FBarcodeType) (cibfCode39 | cibfCode128); // open image page reader->Image->Open (_bstr_t(fileName), page); // read barcodes reader->Find(0); // process results for (int i = 1 ; i <= reader->Barcodes->Count ; i++) { printf (reader->Barcodes->Item[i]->Text); // Use barcode text OR ProcessBarcode (reader->Barcodes->Item[i]);} // do other processing } catch (_com_error &ex) {ProcessException(ex);} }
Java
12345678910111213141516171819202122
void ReadBarcode1D(String fileName, int page) { try { // Create and configure barcode reader ICiBarcodePro reader = Ci.CreateBarcodePro(); reader.setType(new FBarcodeType(FBarcodeType.cibfCode39, FBarcodeType.cibfCode128)); // Multiple types // Open Image file reader.getImage().Open(fileName, page); // Read barcodes reader.Find(0); // Process results for (int i = 1; i <= reader.getBarcodes().getCount(); i++) { ICiBarcode Bc = reader.getBarcodes().getItem(i); System.out.println(Bc.getText()); // Use text value OR ProcessBarcode(Bc); // do other processing } } catch (Exception ex) { ProcessException(ex); } finally { // Collect garbage periodically to release JNI-managed objects System.gc(); }}
PHP
123456789101112131415161718
function ReadBarcode1D ($fileName, $page){ // Create ClearImage COM Server $Ci = new COM("ClearImage.ClearImage"); // Creare and configure barcode reader $reader = $Ci->CreateBarcodePro(); $cibfCode39 = 2; $cibfCode128 = 4; $reader->Type = $cibfCode39 + $cibfCode128; // Open image file $reader->Image->Open($fileName, $page); // Read barcodes $BCcount = $reader->Find(0); // Process Results for ($i=1;$i<=$BCcount;$i++) { $Bc = $reader->BarCodes($i); echo "$Bc->Text<br>"; }}
Delphi
1234567891011121314151617181920212223242526272829303132
uses ...,ClearImage_TLB, comobj; // . . . procedure ReadBarcode1D(const fileName: string; const page: integer); var Ci: ICiServer; reader: ICiBarcodePro; i: integer; begin try try begin //Create and configure reader Ci:=CoCiServer.Create; reader:=Ci.CreateBarcodePro; reader.Image.Open(fileName, page); reader.Type_:=cibCode39 or cibCode128; // Read barcodes reader.Find(0); // Process results for i := 1 to reader.Barcodes.Count do begin ShowMessage(reader.Barcodes.Item[i].Text); end; end except on E:Exception do //Process errors ShowMessage(Format('Error:%s.File:%s', [E.Message,FileName])); end finally end end;
VBScript/ASP
1234567891011121314151617181920
Sub ReadBarcode1D(fileName, page) On Error Resume Next 'Create CiServer object: Set Ci = CreateObject("ClearImage.ClearImage") ' Create and configure reader Set reader = Ci.CreateBarcodePro If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub cibfCode39 = 2: cibfCode128 = 4: reader.Type = cibfCode39 + cibfCode128 ' Open Image reader.Image.Open fileName, page If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub ' Read barcodes reader.Find 0 If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub ' Process results For Each Barcode In reader.Barcodes WScript.Echo Barcode.Text Next End Sub
Read 2D barcodes from an image page
The following examples demonstrate the basic technique to read 2D barcodes from an image file page.
ClearImage supports PDF417, DataMatrix and QR code. In this example, we look only at PDF417. Datamatrix and QR recognition are commented out, but work in the same way.
2D barcodes can contain binary data, compressed text or text encoded in other than ASCII. Text from non western languages is typically encoded in UTF-8 or Unicode, or other code system. See the use of barcodes page to understand how to use Data property to decode barcode data.
C#
12345678910111213141516171819202122
using Inlite.ClearImageNet;// . . .void ReadBarcode2D(string fileName, int page){ try { BarcodeReader reader = new BarcodeReader(); // Select barcode type(s) to read reader.Pdf417 = true; // reader.DataMatrix = true; // reader.QR = true; Barcode[] barcodes = reader.Read(fileName, page); // Process results foreach (Barcode bc in barcodes) { Console.Write(bc.Text); // Use barcode text OR ProcessBarcode(bc); } // do other processing } catch (Exception ex) { ProcessException(ex); }}
VB
12345678910111213141516171819
Imports Inlite.ClearImageNet' . . .Sub ReadBarcode2D(ByVal fileName As String, ByVal page As Integer) Try Dim reader As New BarcodeReader() ' Select barcode type(s) to read reader.Pdf417 = True ' reader.DataMatrix = True ' reader.QR = True Dim barcodes As Barcode() = reader.Read(fileName, page) ' Process results For Each bc As Barcode In barcodes Console.Write(bc.Text) ' Use barcode text OR ProcessBarcode(bc) ' do other processing Next Catch ex As Exception ProcessException(ex) End TryEnd Sub
C++
12345678910111213141516171819
void ReadBarcode2D (const char *fileName, const long page) { try { // Create reader ICiPdf417Ptr reader = Ci->CreatePdf417(); // ICiDataMatrixPtr reader = Ci->CreateDataMatrix(); // ICiQRPtr reader = Ci->CreateQR(); // Open image page reader->Image->Open (_bstr_t(fileName), page); // read barcodes reader->Find(0); // process results for (int i = 1 ; i <= reader->Barcodes->Count ; i++) { printf (reader->Barcodes->Item[i]->Text); // Use barcode text OR ProcessBarcode (reader->Barcodes->Item[i]);} // do other processing } catch (_com_error &ex) {ProcessException(ex);} }
Java
12345678910111213141516171819202122
void ReadBarcode2D(String fileName, int page) { try { // Create and configure barcode reader ICiPdf417 reader = Ci.CreatePdf417(); // ICiDataMatrix reader = Ci.CreateDataMatrix(); // ICiQR reader = Ci.CreateQR(); // Open Image file reader.getImage().Open(fileName, page); // Read barcodes reader.Find(0); // Process results for (int i = 1; i <= reader.getBarcodes().getCount(); i++) { ICiBarcode Bc = reader.getBarcodes().getItem(i); System.out.println(Bc.getText()); // Use text value OR ProcessBarcode(Bc); // do other processing } } catch (Exception ex) { ProcessException(ex); } finally { // Collect garbage periodically to release JNI-managed objects System.gc(); }}
PHP
123456789101112131415161718
function ReadBarcode2D ($fileName, $page){ // Create ClearImage COM Server $Ci = new COM("ClearImage.ClearImage"); // Creare and configure barcode reader $reader = $Ci->CreatePdf417(); // $reader = $Ci->CreateDataMatrix(); // $reader = $Ci->CreateQR(); // Open image file $reader->Image->Open($fileName, $page); // Read barcodes $BCcount = $reader->Find(0); // Process Results for ($i=1;$i<=$BCcount;$i++) { $Bc = $reader->BarCodes($i); echo "$Bc->Text<br>"; }}
Delphi
123456789101112131415161718192021222324252627282930313233343536
uses ...,ClearImage_TLB, comobj; // . . . procedure ReadBarcode2D(const fileName: string; const page: integer); var Ci: ICiServer; reader: ICiPdf417; // reader: ICiDataMatrix; // reader: ICiQR; i: integer; begin try try begin //Create and configure reader Ci:=CoCiServer.Create; reader:=Ci.CreatePdf417; // reader:=Ci.CreateDataMatrix; // reader:=Ci.CreateQR; // Open image page reader.Image.Open(fileName, page); // Read barcodes reader.Find(0); // Process results for i := 1 to reader.Barcodes.Count do begin ShowMessage(reader.Barcodes.Item[i].Text) ; end; end except on E:Exception do //Process errors ShowMessage(Format('Error:%s.File:%s', [E.Message,FileName])); end finally end end;
VBScript/ASP
1234567891011121314151617181920
Sub ReadBarcode2D(fileName, page) On Error Resume Next 'Create CiServer object: Set Ci = CreateObject("ClearImage.ClearImage") ' Create and configure reader Set reader = Ci.CreatePdf417 ' Set reader = Ci.CreateDataMatrix ' Set reader = Ci.CreateQR If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub ' Open Image reader.Image.Open fileName, page If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub ' Read barcodes reader.Find 0 If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub ' Process results For Each Barcode In reader.Barcodes WScript.Echo Barcode.Text Next End Sub
Read postal barcodes from an image page
The following examples demonstrate the basic technique to read postal barcodes from an image file page.
To read the latest generation of postal barcodes (US Intelligent Mail, UK Royal Mail, Australian Mail) select reader.FourState=true (.NET) or reader.Type=cibf4State (COM)
C#
12345678910111213141516171819202122232425262728293031323334353637
using Inlite.ClearImageNet;// . . .void ReadBarcodePostal(string fileName, int page){ try { BarcodeReader reader = new BarcodeReader(); // Enable Postnet and Planet barcodes reading reader.Postnet = true; // Enable US Intelligent Mail, UK Royal Mail, Australian Mail barcodes reading reader.FourState = true; Barcode[] barcodes = reader.Read(fileName, page); // Process results foreach (Barcode bc in barcodes) { switch (bc.Type) { case BarcodeType.Postnet: Console.Write("Postnet "); break; case BarcodeType.Planet: Console.Write("Planet "); break; 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; } Console.Write(bc.Text); // Use barcode text OR ProcessBarcode(bc); } // do other processing } catch (Exception ex) { ProcessException(ex); }}
VB
123456789101112131415161718192021222324252627282930313233343536373839
Imports Inlite.ClearImageNet' . . .Private Sub ReadBarcodePostal(fileName As String, page As Integer) Try Dim reader As New BarcodeReader() ' Enable Postnet and Planet barcodes reading reader.Postnet = True ' Enable US Intelligent Mail, UK Royal Mail, Australian Mail barcodes reading reader.FourState = True Dim barcodes As Barcode() = reader.Read(fileName, page) ' Process results For Each bc As Barcode In barcodes Select Case bc.Type Case BarcodeType.Postnet Console.Write("Postnet ") Exit Select Case BarcodeType.Planet Console.Write("Planet ") Exit Select Case BarcodeType.UspsIntelligentMail Console.Write(" US Intelligent Mail ") Exit Select Case BarcodeType.BpoPostcode Console.Write("UK Royal Mail ") Exit Select Case BarcodeType.AustralianPost Console.Write("Australian Mail ") Exit Select Case BarcodeType.SingaporePost Console.Write("Singapore Mail ") Exit Select End Select Console.Write(bc.Text) ' Use barcode text OR ProcessBarcode(bc) ' do other processing Next Catch ex As Exception ProcessException(ex) End TryEnd Sub
C++
123456789101112131415161718192021222324252627282930313233
void ReadBarcodePostal (const char *fileName, const long page) { try { ICiBarcodeProPtr reader; reader = Ci->CreateBarcodePro (); // Enable Postnet and Planet barcodes reading // Enable US Intelligent Mail, UK Royal Mail, Australian Mail barcodes reading reader->Type = (FBarcodeType) (cibfPostnet | cibf4State); reader->Image->Open (_bstr_t(fileName), page); reader->Find(0); for (int i = 1 ; i <= reader->Barcodes->Count ; i++) { // print type switch (reader->Barcodes->Item[i]->Type) { case cibPostnet: printf("Postnet "); break; case cibPlanet: printf("Planet "); break; case cibUspsIntelligentMail: printf(" US Intelligent Mail "); break; case cibBpoPostcode: printf("UK Royal Mail "); break; case cibAustralianPost: printf("Australian Mail "); break; case cibSingaporePost: printf("Singapore Mail "); break; } ProcessBarcode (reader->Barcodes->Item[i]); } } catch (_com_error &ex) {ProcessException(ex);} }
Java
1234567891011121314151617181920212223242526272829303132333435363738394041
void ReadBarcodePostal(String fileName, int page) { try { // Create and configure barcode reader ICiBarcodePro reader = Ci.CreateBarcodePro(); // Enable Postnet and Planet barcodes reading // Enable US Intelligent Mail, UK Royal Mail, Australian Mail barcodes reading reader.setType(new FBarcodeType(FBarcodeType.cibfPostnet, FBarcodeType.cibf4State)); // Open Image file reader.getImage().Open(fileName, page); // Read barcodes reader.Find(0); // Process results for (int i = 1; i <= reader.getBarcodes().getCount(); i++) { ICiBarcode Bc = reader.getBarcodes().getItem(i); EBarcodeType type = Bc.getType(); if (type == EBarcodeType.cibPostnet) System.out.println("Postnet "); if (type == EBarcodeType.cibPlanet) System.out.println("Planet "); if (type == EBarcodeType.cibUspsIntelligentMail) System.out.println(" US Intelligent Mail "); if (type == EBarcodeType.cibBpoPostcode) System.out.println("UK Royal Mail "); if (type == EBarcodeType.cibAustralianPost) System.out.println("Australian Mail "); if (type == EBarcodeType.cibSingaporePost) System.out.println("Singapore Mail "); System.out.println(Bc.getText()); // Use text value OR ProcessBarcode(Bc); // do other processing } } catch (Exception ex) { ProcessException(ex); } finally { // Collect garbage periodically to release JNI-managed // objects System.gc(); }}
PHP
12345678910111213141516171819202122232425
function ReadBarcodePostal ($fileName, $page){ // Create ClearImage COM Server $Ci = new COM("ClearImage.ClearImage"); // Creare and configure barcode reader $reader = $Ci->CreateBarcodePro(); $cibfPostnet = 0x1000000; $cibf4State = 0x2000000; $reader->Type = $cibfPostnet + $cibf4State; // Open image file $reader->Image->Open($fileName, $page); // Read barcodes $BCcount = $reader->Find(0); // Process Results for ($i=1;$i<=$BCcount;$i++) { $Bc = $reader->BarCodes($i); $type1 =$Bc->Type; if ($type1 == 19) echo "Postnet "; // cibPostnet = 19 if ($type1 == 20) echo "Planet "; // cibPlanet = 20 if ($type1 == 33) echo " US Intelligent Mail "; // cibUspsIntelligentMail = 33 if ($type1 == 34) echo "UK Royal Mail "; // cibBpoPostcode = 34 if ($type1 == 35) echo "Australian Mail "; // cibAustralianPost = 35 if ($type1 == 37) echo "Singapore Mail "; // cibSingaporePost = 37 echo "$Bc->Text<br>"; }}
Delphi
12345678910111213141516171819202122232425262728293031323334353637383940414243
uses ...,ClearImage_TLB, comobj; // . . . procedure ReadBarcodePostal(const fileName: string; const page: integer); var Ci: ICiServer; reader: ICiBarcodePro; i: integer; t: string; begin try try begin //Create and configure reader Ci:=CoCiServer.Create; reader:=Ci.CreateBarcodePro; // Enable Postnet and Planet barcodes reading // Enable US Intelligent Mail, UK Royal Mail, Australian Mail barcodes reading reader.Type_:=cibfPostnet or cibf4State; reader.Image.Open(fileName, page); // Read barcodes reader.Find(0); // Process results for i := 1 to reader.Barcodes.Count do begin Case reader.Barcodes.Item[i].Type_ of cibPostnet: t := 'Postnet '; cibPlanet: t := 'Planet '; cibUspsIntelligentMail: t := ' US Intelligent Mail '; cibBpoPostcode: t := 'UK Royal Mail '; cibAustralianPost: t := 'Australian Mail '; cibSingaporePost: t := 'Singapore Mail '; end; ShowMessage(Format('Type:%s Value:%s', [t, reader.Barcodes.Item[i].Text])); end; end except on E:Exception do //Process errors ShowMessage(Format('Error:%s.File:%s', [E.Message,FileName])); end finally end end;
VBScript/ASP
123456789101112131415161718192021222324252627
Sub ReadBarcodePostal(fileName, page) On Error Resume Next 'Create CiServer object: Set Ci = CreateObject("ClearImage.ClearImage") ' Create and configure reader Set reader = Ci.CreateBarcodePro If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub cibfPostnet = &H1000000: cibf4State = &H2000000: reader.Type = cibfPostnet + cibf4State ' Open Image reader.Image.Open fileName, page If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub ' Read barcodes reader.Find 0 If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub ' Process results For Each Barcode In reader.Barcodes type1 = Barcode.Type if (type1 = 19) Then WScript.Echo "Postnet " ' cibPostnet = 19 if (type1 = 20) Then WScript.Echo "Planet " ' cibPlanet = 20 if (type1 = 33) Then WScript.Echo " US Intelligent Mail " ' cibUspsIntelligentMail = 33 if (type1 = 34) Then WScript.Echo "UK Royal Mail " ' cibBpoPostcode = 34 if (type1 = 35) Then WScript.Echo "Australian Mail " ' cibAustralianPost = 35 if (type1 = 37) Then WScript.Echo "Singapore Mail " ' cibSingaporePost = 37 WScript.Echo Barcode.Text Next End Sub