In these examples 1D barcodes (Code39 and Code128) are read. To see how to read 2D and postal barcode click here.
Read barcodes from a multi-page file
The following examples demonstrate how to read all the barcodes of a selected type from each page of a multi-page TIF or PDF file. See examples above for details of reader configuration and result processing.
The .NET API includes functions that process all pages in the entire file with one invocation, while the COM API requires the application to iterate explicitly through each page of the file.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using Inlite.ClearImageNet; // . . . void ReadBarcode1DfromFile (string fileName) { try { BarcodeReader reader = new BarcodeReader(); // Select barcode type(s) to read reader.Code39 = true; reader.Code128 = true; Barcode[] barcodes = reader.Read(fileName); // Process results foreach (Barcode bc in barcodes) {Console.WriteLine(bc.Text + " in file:" + bc.File + " page: " + bc.Page.ToString());}} catch (Exception ex) {ProcessException (ex);} } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Imports Inlite.ClearImageNet ' . . . Sub ReadBarcode1DformFile(ByVal fileName As String) 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) ' Process results For Each bc As Barcode In barcodes Console.Write(bc.Text + _ " in file:" + bc.File & " page: " & bc.Page.ToString()) Next Catch ex As Exception ProcessException(ex) End Try End Sub |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
void ReadBarcode1DfromFile (const char *fileName) { try { // configure reader ICiBarcodeProPtr reader = Ci->CreateBarcodePro (); reader->Type = (FBarcodeType) (cibfCode39 | cibfCode128); int page = 1; while (true) { // open file page reader->Image->Open (_bstr_t(fileName), page); // read barcodes reader->Find(0); // process results for (int i = 1 ; i <= reader->Barcodes->Count ; i++) ProcessBarcode (reader->Barcodes->Item[i]); // got to the next page page++; if (page > reader->Image->PageCount) break; } } catch (_com_error &ex) {ProcessException(ex);} } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
void ReadBarcode1DfromFile(String fileName) { try { // Create and configure barcode reader ICiBarcodePro reader = Ci.CreateBarcodePro(); reader.setType(new FBarcodeType(FBarcodeType.cibfCode39, FBarcodeType.cibfCode128)); // Multiple types int page = 1; while (true) { // 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 } page++; if (page > reader.getImage().getPageCount()) break; } } catch (Exception ex) { ProcessException(ex); } finally { // Collect garbage periodically to release JNI-managed objects System.gc(); } } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
function ReadBarcode1DfromFile ($fileName) { // 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; $page=1; while(true) { // 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); $Bc = $reader->BarCodes($i); echo "$Bc->Text<br>"; } $page++; if ($page > $reader->Image->PageCount) break; } } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
procedure ReadBarcode1DfromFile(const fileName: string); var Ci: ICiServer; reader: ICiBarcodePro; i: integer; page: integer; begin try try begin //Create and configure reader Ci:=CoCiServer.Create; reader:=Ci.CreateBarcodePro; reader.Type_:=cibCode39 or cibCode128; page:=1; while true do; begin 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; page := page + 1; if (page > reader.Image.PageCount) then Break; end; end except on E:Exception do //Process errors ShowMessage(Format('Error:%s.File:%s', [E.Message,FileName])); end finally end end; |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Sub ReadBarcode1DfromFile(fileName) 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 page = 1 do ' 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 page = page + 1 if (page > reader.Image.PageCount) Then Exit Do Loop End Sub |
Read barcodes from stream object
These .NET examples demonstrate how to read a barcode from an image in a stream. Using streams allow the application to directly process database-stored images without using the windows file system as an intermediate storage facility. The COM API does NOT support streams.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using Inlite.ClearImageNet; // . . . void ReadBarcode1DfromStream (Stream stream) { try { BarcodeReader reader = new BarcodeReader(); // Select barcode type(s) to read reader.Code39 = true; reader.Code128 = true; Barcode[] barcodes = reader.Read(stream); // Process results foreach (Barcode bc in barcodes) {Console.Write(bc.Text + " page: " + bc.Page.ToString());} } catch (Exception ex) {ProcessException (ex);} } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Imports Inlite.ClearImageNet ' . . . Private Sub ReadBarcode1DfromStream(stream As Stream) Try Dim reader As New BarcodeReader() ' Select barcode type(s) to read reader.Code39 = True reader.Code128 = True Dim barcodes As Barcode() = reader.Read(stream) ' Process results For Each bc As Barcode In barcodes Console.Write(bc.Text + " page: " & bc.Page.ToString()) Next Catch ex As Exception ProcessException(ex) End Try End Sub |
Read barcodes from multiple files using threads
Executing the barcode recognition activities on multiple threads allows the application to utilize greater computational power, on the basis of one thread per available core. This sophisticated and more difficult technique is particularly valuable in server-based systems, which process a large volume of image files. See the code examples in the “Working with Threads” page.
