Reading Barcodes

 

The purpose of this tutorial is to provide you with a quick and easy way to generate a Barcode program. Take the following steps to create and run a program that reads barcodes.

 

1.

Create a new directory in the \Examples\API directory called BarcodeTutor

2.

Copy everything in the SimpleLoad directory into the BarcodeTutor directory.

3.

Compile the project as it is and run SimpleLoad.exe to familiarize yourself with the basic program.

4.

In the Imports.cpp, add the following lines:

 

#if defined(WIN64)

   …

   …

   #pragma comment(lib, "..\\..\\..\\Lib\\API\\x64\\Ltbar_x.lib")

#else

   …

   …

   #pragma comment(lib, "..\\..\\..\\Lib\\API\\Win32\\Ltbar_u.lib")

#endif // #if defined(WIN64)

 

5.

Add the following line in StdAfx.h in the BarcodeTutor directory:

 

#include "..\..\..\include\ltbar.h"

 

6.

Define the following global variables in Ezfunc.h in the BarcodeTutor directory:

pBARCODEDATA pBarCodes;

 

   Also, add the following defines:

 

#define IDM_READ_BARCODES        200

 

7.

Add the following statements in the WinMain function as follows:

 

a.

Add the following before calling InitInstance() statement

   /* Note that this is a sample key, which will not work in your toolkit. You should replace the sample key */ 
   L_UnlockSupport
(L_SUPPORT_OCR, TEXT("TestKey"));
   if (L_BarCodeInit(BARCODES_1D) != SUCCESS)

      return 0;
   pBarCodes = NULL;

 

b.

Add the following after the GetMessage() loop

   if (pBarCodes)

      L_BarCodeFree(&pBarCodes);
   L_BarCodeExit();

 

 

8.

Edit EZFUNC.RC file in the BarcodeTutor directory and add the following lines:

 

#include "EZFUNC.H"

 

MAIN_MENU   MENU

BEGIN

   MENUITEM "Read Barcodes"   IDM_READ_BARCODES

END

 

9.

In InitApplication, change the line:

wcWindowClass.lpszMenuName = NULL;

   To:

wcWindowClass.lpszMenuName = TEXT("MAIN_MENU");

 

10.

In MainWndProc, change the line:

 

Demos_CombinePath(szImagesDirectory, L_TEXT("Image1.cmp"), szFilename, _countof(szFilename));

 

   To:

 

Demos_CombinePath(szImagesDirectory, L_TEXT("barcode1.tif"), szFilename, _countof(szFilename));

 

11.

In InitInstance, change the menu parameter in CreateWindow from NULL to

LoadMenu(hInstance, MAKEINTRESOURCE("MAIN_MENU"))

 

12.

In the MainWndProc procedure, add the following code to the switch statement:

   case WM_COMMAND: 
      switch(LOWORD(wParam)) 
      {
      case IDM_READ_BARCODES: 
         {
            BARCODE1D Bar1d;

            memset(&Bar1d, 0, sizeof(BARCODE1D));

            Bar1d.uStructSize = sizeof(BARCODE1D);

            Bar1d.bErrorCheck = TRUE;

            Bar1d.nGranularity = 9;

            Bar1d.nDirection = BARCODE_DIR_HORIZONTAL;

 

            BARCODECOLOR BarColor;

            memset(&BarColor, 0, sizeof(BARCODECOLOR));

            BarColor.uStructSize = sizeof(BARCODECOLOR);

            BarColor.dwColorBar = RGB(0, 0, 0);

            BarColor.dwColorSpace = RGB(255, 255, 255);

 

            // The Read method will search for all linear barcodes in the whole bitmap

            L_INT nRet = L_BarCodeRead(&LeadBitmap,

                                       NULL,

                                       BARCODE_1D_READ_ANYTYPE,

                                       BARCODE_SCANLINES_PER_PIXELS,

                                       BARCODE_BLOCK_SEARCH,

                                       0, &Bar1d, NULL, &BarColor,

                                       &pBarCodes, sizeof(BARCODEDATA));

 

            if (nRet == SUCCESS)

            {

               L_INT nCount = pBarCodes->nTotalCount;

               L_CHAR szBuffer[MAX_PATH];

               for (L_INT i=0; i<nCount; i++)

               {

                  memset(szBuffer, 0, MAX_PATH);

                  wsprintfA(szBuffer,

                            "Barcode # %d\nData %s\nLeft %d\nTop %d\nRight %d\nBottom %d\n",

                            i,

                            pBarCodes[i].pszBarCodeData,

                            pBarCodes[i].rcBarLocation.left,

                            pBarCodes[i].rcBarLocation.top,

                            pBarCodes[i].rcBarLocation.right,

                            pBarCodes[i].rcBarLocation.bottom);

 

                  MessageBoxA(hWnd, szBuffer, ("Barcode Data"), MB_OK);

               }

            }

            else

               MessageBox(hWnd, TEXT("Failure during reading linear barcodes"), TEXT("Error!"), MB_OK);         }
         break; 
      }

      return 0;

 

13.

On the Build menu, select Build SimpleLoad.exe.

14.

On the Build menu, select Execute SimpleLoad.exe.

15.

Save this project to use for testing other code samples.