Reading Barcodes

Take the following steps to create and run a program that demonstrates how to read barcodes. Remember that the purpose of the tutorials is to provide you with a quick and easy way to generate a Barcode program.

1.

Start Microsoft Visual Studio 2005

2.

Select the File->New menu option, click the "Project" menu.

3.

From "Project Types" select "Other Languages" to expand it, then select "Visual C++" to expand it, then select "MFC". From the right window select "MFC Application".

4.

In the Project Name dialog box, enter " BarcodeTutor".

5.

In the Location dialog box, use the "Examples\ClassLibrary\MSVC" directory of your LEAD installation. For example, if you installed LEADTOOLS in "C:\Program Files\LEAD Technologies\LEADTOOLS 16\", enter "C:\Program Files\LEAD Technologies\LEADTOOLS 16\ Examples\ClassLibrary\MSVC", then Click OK. Then click "Next".

6.

Choose "Dialog based" and click "Finish".

7.

Click on the "Solution Explorer" tab, and then click on the "BarcodeTutor" project to expand it. Click on the Header files, then Open "BarcodeTutor.h".

8.

Add the following line immediately before the class CBarcodeTutorApp declaration, (keep in mind, you may have to change the path to where the header files reside):

 

#include "..\..\..\..\include\ClassLib\ltwrappr.h"

9.

Click on the "Class View" tab.

10.

Click to open the "BarcodeTutor" classes branch.

11.

Click "CBarcodeTutorApp", and then double click the CBarcodeTutorApp(void) constructor.

12.

Add the following lines after //TODO: add construction code here:

 

   LBase::LoadLibraries(LT_ALL_LEADLIB); 
   LSettings::UnlockSupport(L_SUPPORT_BARCODES_1D, L_KEY_BARCODES_1D); 

13.

Create a new file called Imports.cpp in place it beside your project files.

 

a.

In the Project Workspace, click the Solution Explorer tab.

 

b.

Double-click the BarcodeTutor folder to open it.

 

c.

Right-click the Source files folder and select Add à New item.

 

d.

Expand Visual C++ tree, if it is not already expanded.

 

e.

Select Code from the sub tree.

 

f.

Select C++ File (.cpp) from the right window.

 

g.

In the name text box, specify Imports.cpp

 

h.

Click the Add button.

 

i.

Double-click the imports.cpp in the solution Explorer and add the following lines:

#include "StdAfx.h"

#if defined(WIN64) 
   #pragma comment(lib, "..\\..\\..\\..\\Lib\\CDLL\\x64\\Ltwvc_x.lib")
#else
   #pragma comment(lib, "..\\..\\..\\..\\Lib\\CDLL\\Win32\\Ltwvc_u.lib")
#endif // #if defined(WIN64) 
 

14.

Click on the "Class View" tab.

15.

Right click "CBarcodeTutorDlg" and select Add à "Add Variable..."

16.

For Variable Type enter LBarCode, and for Variable Declaration put m_Barcode. Leave Access as "Public" and click OK.

17.

Right click "CBarcodeTutorDlg" and select Add à "Add Variable..."

18.

For Variable Type enter LBitmapBase, and for Variable Declaration put m_Bitmap. Leave Access as "Public" and click OK.

19.

Click on the "Solution Explorer" tab.

20.

Double-click the "BarcodeTutor" folder to open it.

21.

Double-click the "Resource Files" folder to open it. Then double click "BarcodeTutor.rc" file to open it, then double click "Dialog", and then double click "IDD_BARCODETUTOR_DIALOG"

22.

Now, drag and drop 2 buttons, and change their properties as follows:

 

 

ID

Caption

 

Button1

IDC_LOAD

Load Image

 

Button2

IDC_READ_BARCODE

Read Barcode

23.

From View menu, select "Other Windows" menu, then select "Resource View" menu, then select Dialog, and select "IDD_BARCODETUTOR_DIALOG"

24.

Double click on "Load Image" button, and add the following code:

   m_Bitmap.SetFileName(L"C:\\Program Files\\LEAD Technologies\\LEADTOOLS 16\\Images\\barcode1.tif");
   m_Bitmap.Load();

25.

From View menu, select "Other Windows" menu, then select "Resource View" menu, then select Dialog, and select "IDD_BARCODETUTOR_DIALOG"

26.

Double click on "Read Barcode" button, and add the following code:

 

   BARCODE1D Bar1d; 
   memset(&Bar1d, 0, sizeof(BARCODE1D)); 
   Bar1d.uStructSize = sizeof(BARCODE1D); 
   Bar1d.bErrorCheck = TRUE; 
   Bar1d.nGranularity = 9; 
   Bar1d.nDirection = BARCODE_DIR_HORIZONTAL; 

   BARCODEREADOPT BarReadOpts; 
   memset(&BarReadOpts, 0, sizeof(BARCODEREADOPT)); 

   BarReadOpts.ulSearchType = BARCODE_1D_READ_ANYTYPE; 
   BarReadOpts.nUnits = BARCODE_SCANLINES_PER_PIXELS; 
   BarReadOpts.ulFlags = BARCODE_BLOCK_SEARCH; 
   BarReadOpts.nMultipleMax = 0; 
   SetRect(&BarReadOpts.rcSearch, 0, 0, 0, 0); 
   BarReadOpts.bUseRgn = FALSE; 
   BarReadOpts.BarColor.uStructSize = sizeof(BARCODECOLOR); 
   BarReadOpts.BarColor.dwColorBar = RGB(0, 0, 0); 
   BarReadOpts.BarColor.dwColorSpace = RGB(255, 255, 255); 

   m_Barcode.SetReadOptions(&BarReadOpts); 
   m_Barcode.SetBitmap(&m_Bitmap); 

   // The Read method will search for all linear barcodes in the whole bitmap
   L_INT nRet = m_Barcode.Read(BARCODES_1D, &Bar1d, NULL); 
   if (nRet > 0) 
   {
      pBARCODEDATA pBarData = NULL; 
      L_INT nCount = nRet; 
      L_CHAR szBuffer[MAX_PATH]; 

      for (L_INT i=0; i<nCount; i++)
      {
         pBarData = m_Barcode.GetBarCodeDataItem(i); 

         memset(szBuffer, 0, MAX_PATH); 
         wsprintfA(szBuffer, 
                   "Barcode # %d\nData %s\nLeft %d\nTop %d\nRight %d\nBottom %d\n",
                   i+1, 
                   pBarData->pszBarCodeData, 
                   pBarData->rcBarLocation.left, 
                   pBarData->rcBarLocation.top, 
                   pBarData->rcBarLocation.right, 
                   pBarData->rcBarLocation.bottom); 

         MessageBoxA(m_hWnd, szBuffer, ("Barcode Data"), MB_OK); 
      }
   }
   else
      AfxMessageBox(TEXT("Failure during reading linear barcodes"));

27.

Compile and test the program.