In these examples 1D barcodes (Code39 and Code128) are read. To see how to read 2D and postal barcode click here.
Table of contents
Zone reading and other optional parameters
ClearImage Barcode readers are designed to simplify application development. Thus only the general barcode symbology needs to be specified. A few optional parameters add flexibility to the barcode reading:
- If the expected 1D barcode type is not known in advance set reader.Auto1D=true (.NET) or reader.AutoDetect1D=ciTrue (COM) to detect all the most popular barcode types. If the type is known, then do not use this technique because it slows down the recognition process.
- The readers search for barcodes in any orientation (360 degree rotation). If the orientation of the barcode is known in advance, then limit the choice of directions to improve recognition speed. To limit the direction of search do:
- .NET API: Set reader.Horizonal, reader.Vertical, or reader.Diagonal properties to FALSE. At least one property must remain TRUE.
- COM API: Set reader.Directions only to the desired subset of cibHorz, cibVert, cibDiag
- Barcodes are searched on the whole page. To limit the search to a specific area:
- .NET API: Set reader.Zone to desired rectangle.
- COM API: Assign image zone to reader.Image property.
NOTE: When defining the zone you should take into account the possibility that the document was scanned upside-down, so you really need to set two mirror image zones.
C#
1234567891011121314151617181920212223
using Inlite.ClearImageNet;// . . .void ReadBarcode1Dparams(string fileName, int page){ try { BarcodeReader reader = new BarcodeReader(); // Find the most popular 1D Barcodes reader.Auto1D = true; // Look only for Horizontal barcodes reader.Vertical = reader.Diagonal = false; // Set search zone to upper left corner ImageIO io = new ImageIO(); ImageInfo info = io.Info(fileName, page); reader.Zone = new Rectangle(0, 0, info.Width / 2, info.Height / 2); // Read barcodes Barcode[] barcodes = reader.Read(fileName, page); // Process results // ... } catch (Exception ex) { ProcessException(ex); }}
VB
12345678910111213141516171819202122
Imports Inlite.ClearImageNet' . . .Private Sub ReadBarcode1Dparams(fileName As String, page As Integer) Try Dim reader As New BarcodeReader() ' Find the most popular 1D Barcodes reader.Auto1D = True ' Look only for Horizontal barcodes reader.Vertical = False reader.Diagonal = False ' Set search zone to upper left corner Dim io As New ImageIO() Dim info As ImageInfo = io.Info(fileName, page) reader.Zone = New Rectangle(0, 0, info.Width / 2, info.Height / 2) ' Read barcodes Dim barcodes As Barcode() = reader.Read(fileName, page) ' Process results ' ... Catch ex As Exception ProcessException(ex) End TryEnd Sub
C++
123456789101112131415161718192021
void ReadBarcode1Dparams (const char *fileName, const long page) { try { ICiBarcodeProPtr reader; reader = Ci->CreateBarcodePro (); // Find the most popular 1D Barcodes reader->AutoDetect1D = ciTrue; // Look only for Horizontal barcodes reader->Directions = cibHorz; // Open an image reader->Image->Open (_bstr_t(fileName), page); // Set search zone to upper left corner reader->Image = reader->Image->CreateZone(0, 0, reader->Image->Width / 2, reader->Image->Height / 2); reader->Find(0); for (int i = 1 ; i <= reader->Barcodes->Count ; i++) ProcessBarcode (reader->Barcodes->Item[i]); } catch (_com_error &ex) {ProcessException(ex);} }
Java
12345678910111213141516171819202122232425262728
void ReadBarcode1Dparams(String fileName, int page) { try { // Create and configure barcode reader ICiBarcodePro reader = Ci.CreateBarcodePro(); // Find the most popular 1D Barcodes reader.setAutoDetect1D(EBoolean.ciTrue); // Look only for Horizontal barcodes reader.setDirections(FBarcodeDirections.cibHorz); // Open an image reader.getImage().Open(fileName, page); // Set search zone to upper left corner reader.setImage(reader.getImage().CreateZone(0, 0, reader.getImage().getWidth() / 2, reader.getImage().getHeight() / 2)); // 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
123456789101112131415161718192021222324
function ReadBarcode1Dparams ($fileName, $page){ // Create ClearImage COM Server $Ci = new COM("ClearImage.ClearImage"); // Creare and configure barcode reader $reader = $Ci->CreateBarcodePro(); // Find the most popular 1D Barcodes $reader->AutoDetect1D = 65535; // Look only for Horizontal barcodes $cibHorz = 1; $reader->Directions = $cibHorz; // Open image file $reader->Image->Open($fileName, $page); // Set search zone to upper left corner $reader->Image = $reader->Image->CreateZone(0, 0, $reader->Image->Width / 2, $reader->Image->Height / 2); // Read barcodes $BCcount = $reader->Find(0); // Process Results for ($i=1;$i<=$BCcount;$i++) { $Bc = $reader->BarCodes($i); echo "$Bc->Text<br>"; }}
Delphi
12345678910111213141516171819202122232425262728293031323334353637
procedure ReadBarcode1Dparams(const fileName: string; const page: integer); var Ci: ICiServer; reader: ICiBarcodePro; i, w, h: Integer; begin try try begin //Create and configure reader Ci:=CoCiServer.Create; reader:=Ci.CreateBarcodePro; // Read the most popular types reader.AutoDetect1D := ciTrue; // Read only Horizontal barcode reader.Directions := cibHorz; // Open image page reader.Image.Open(fileName, page); // Read only in the upper left corner w := reader.Image.Width; h := reader.Image.Height; reader.Image := reader.Image.CreateZone(0, 0, Trunc(w/2), Trunc(h/2)); // 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
12345678910111213141516171819202122232425
Sub ReadBarcode1Dparams(fileName As String, page As Integer) 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 ' Find the most popular 1D Barcodes reader.AutoDetect1D = 65535 ' Look only for Horizontal barcodes only cibHorz = 1 reader.Directions = cibHorz ' Set search zone to upper left corner reader.Image.Open fileName, page If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub reader.Image = reader.Image.CreateZone(0, 0, _ reader.Image.Width / 2, reader.Image.Height / 2) ' Read barcodes reader.Find 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 one barcode at a time
The examples on this site retrieve all the found barcodes from an image page or an image file.In some applications it may be advantageous to retrieve one barcode at a time and to terminate the search once the desired barcode is identified. The following examples demonstrate how to read one barcode a time.
- .NET API: Assign handler for reader.BarcodeFoundEvent. Then set e.cancel = true in handler when desired barcode is found
- COM API: Use reader.FirstBarcode then reader.NextBarcode to iterate through barcodes
C#
1234567891011121314151617181920
using Inlite.ClearImageNet;// . . .void OnBarcodeFound(object sender, BarcodeFoundEventArgs e){ ProcessBarcode(e.Barcode); e.cancel = true; // Signal to stop reading } void ReadBarcode1D_one(string fileName, int page){ try { BarcodeReader reader = new BarcodeReader(); reader.Code39 = true; reader.Code128 = true; reader.BarcodeFoundEvent += new BarcodeReader.BarcodeFoundEventHandler(OnBarcodeFound); reader.Read(fileName, page); } catch (Exception ex) { ProcessException(ex); }}
VB
123456789101112131415161718
Imports Inlite.ClearImageNet' . . .Private Sub OnBarcodeFound(sender As Object, e As BarcodeFoundEventArgs) ProcessBarcode(e.Barcode) e.cancel = True ' Signal to stop reading End Sub Private Sub ReadBarcode1D_one(fileName As String, page As Integer) Try Dim reader As New BarcodeReader() reader.Code39 = True reader.Code128 = True reader.BarcodeFoundEvent += New BarcodeReader.BarcodeFoundEventHandler(AddressOf OnBarcodeFound) reader.Read(fileName, page) Catch ex As Exception ProcessException(ex) End TryEnd Sub
C++
123456789101112131415161718
void ReadBarcode1D_one (const char *fileName, const long page) { try { ICiBarcodeProPtr reader; reader = Ci->CreateBarcodePro (); reader->Type = (FBarcodeType) (cibfCode39 | cibfCode128); reader->Image->Open (_bstr_t(fileName), page); ICiBarcodePtr bc = reader->FirstBarcode(); if (bc != NULL) ProcessBarcode(bc); #if 0 // To process more barcodes implement loop while (bc != NULL) { ProcessBarcode(bc); bc = reader->NextBarcode(); } #endif } catch (_com_error &ex) {ProcessException(ex);} }
Java
12345678910111213141516171819202122
void ReadBarcode1D_one(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 ICiBarcode Bc = reader.FirstBarcode(); if (Bc != null) System.out.println(Bc.getText()); // Optionally read r=the rest of barcodes while (Bc != null) { System.out.println(Bc.getText()); Bc = reader.NextBarcode(); } } catch (Exception ex) { ProcessException(ex); } finally { // Collect garbage periodically to release JNI-managed objects System.gc(); }}
PHP
12345678910111213141516171819
function ReadBarcode1D_one ($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 one barcode $Bc = $reader->FirstBarcode(); echo "$Bc->Text<br>"; // Optionally read the rest of barcodes while ($Bc != null) { echo "$Bc->Text<br>"; $Bc = $reader->NextBarcode(); }}
Delphi
12345678910111213141516171819202122232425262728293031323334
uses ...,ClearImage_TLB, comobj; // . . . procedure ReadBarcode1D_one(const fileName: string; const page: integer);varCi: ICiServer;reader: ICiBarcodePro;bc: ICiBarcode;begin try try begin //Create and configure reader Ci:=CoCiServer.Create; reader:=Ci.CreateBarcodePro; reader.Type_:=cibCode39 or cibCode128; reader.Image.Open(fileName, page); // Read barcodes bc := reader.FirstBarcode(); ShowMessage(bc.Text); // Optionally read the rest of barcodes while (bc <> nil) do begin ShowMessage(bc.Text); bc := reader.NextBarcode(); end; end except on E:Exception do //Process errors ShowMessage(Format('Error:%s.File:%s', [E.Message,FileName])); end finally endend;
VBScript/ASP
12345678910111213141516171819202122
Sub ReadBarcode1D_one(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 Set Barcode = reader.FirstBarcode If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub WScript.Echo Barcode.Text ' Optionaly read the rest of barcodes Do while Barcode <> Nothing WScript.Echo Barcode.Text Set Barcode = reader.NextBarcode Loop End Sub