Detect and Extract Barcodes - Windows C DLL

This tutorial shows how to read barcode information from a loaded image using the LEADTOOLS SDK in a Windows C/C++ API application.

Overview  
Summary This tutorial covers how to use the BARCODEDATA structure in a Windows C DLL Application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (19 KB)
Platform Windows C DLL Application
IDE Visual Studio 2017, 2019, 2022
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Before working on the Detect and Extract Barcodes - Windows C DLL tutorial, get familiar with the basic steps of creating a project and loading/displaying an image by reviewing the Add References and Set a License and Load, Display, and Save Images tutorials.

Create the Project and Add LEADTOOLS References

Start with a copy of the 64-bit Windows API project created in the Load, Display, and Save Images tutorial. If the project is not available, create it by following the steps in that tutorial.

To read barcodes using LEADTOOLS, add the required header and DLL files. Open the pre-compiled header file (either pch.h or stdafx.h, depending on the version of Visual Studio used) and add the following lines:

#include "C:\LEADTOOLS23\Include\Ltbar.h" 
#pragma comment (lib, "C:\\LEADTOOLS23\\Lib\\CDLL\\x64\\Ltbar_x.lib") // barcode support 

Note: For a complete list of Dlls that are required for your application, refer to Files to be Included with your Application - C API.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit functionality is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note: Adding LEADTOOLS references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the Barcode Reader Code

With the project created, the references added, the license set, and the load image code added, coding can begin.

The steps below are for Visual Studio 2019; they could be different for other versions of Visual Studio.

Go to the Solution Explorer and double-click the resources file (.rc).

Add a new &Barcode drop down menu next to the File menu. In the newly-added &Barcode menu, add a &Read Barcode(s) menu item. The new item's ID should be ID_BARCODE_READBARCODE.

Go to the WndProc function, and under the switch (wmId) statement that is below the WM_COMMAND case, add a new case:

switch (wmId) 
{ 
case ID_BARCODE_READBARCODE: 
   if (LEADBmp.Flags.Allocated) 
      ReadBarcodes(hWnd); 
   else 
      MessageBox(hWnd, TEXT("Cannot read barcodes. No image loaded"), TEXT("Barcode Demo"), MB_ICONERROR); 
   break; 
// keep rest of the code as is 

Add a new ReadBarcodes function, which can be placed above the WndProc function:

void ReadBarcodes(HWND hwnd) 
{ 
   if (L_BarCodeInit(BARCODES_1D) != SUCCESS) 
      return; 
   pBARCODEDATA pBarCodes = NULL; 
   BARCODE1D Bar1d = { 0 }; 
   Bar1d.uStructSize = sizeof BARCODE1D; 
   Bar1d.bErrorCheck = TRUE; 
   Bar1d.nGranularity = 9; 
   Bar1d.nDirection = BARCODE_DIR_HORIZONTAL; 
   BARCODECOLOR BarColor = { 0 }; 
   BarColor.uStructSize = sizeof BARCODECOLOR; 
   BarColor.dwColorBar = RGB(0, 0, 0); 
   BarColor.dwColorSpace = RGB(255, 255, 255); 
   // Search for all 1D barcodes in the enite bitmap 
   L_INT nRet = L_BarCodeRead(&LEADBmp, NULL, BARCODE_1D_READ_ANYTYPE, BARCODE_SCANLINES_PER_PIXELS, BARCODE_BLOCK_SEARCH, 0, &Bar1d, NULL, &BarColor, &pBarCodes, sizeof BARCODEDATA); 
   if (SUCCESS == nRet) 
   { 
      L_INT nCount = pBarCodes->nTotalCount; 
      L_CHAR * pszMessage = new L_CHAR[256 * nCount]; 
      ZeroMemory(pszMessage, 256 * nCount * sizeof(*pszMessage)); 
      for (L_INT i = 0; i < nCount; i++) 
      { 
         L_CHAR szBuffer[256] = ""; 
         wsprintfA(szBuffer, "Barcode # %d, Data: '%s', Left %d, Top %d, Right %d, Bottom %d\n---\n", i, 
            pBarCodes[i].pszBarCodeData, 
            pBarCodes[i].rcBarLocation.left, 
            pBarCodes[i].rcBarLocation.top, 
            pBarCodes[i].rcBarLocation.right, 
            pBarCodes[i].rcBarLocation.bottom); 
         lstrcatA(pszMessage, szBuffer); 
      } 
      MessageBoxA(hwnd, pszMessage, "Barcode(s) Found", MB_ICONINFORMATION); 
      delete[] pszMessage; 
   } 
   else 
      MessageBox(hwnd, TEXT("Could not read 1D Barcodes"), TEXT("Barcode Demo"), MB_ICONERROR); 
   if (pBarCodes) 
      L_BarCodeFree(&pBarCodes); 
   L_BarCodeExit(); 
} 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps are followed correctly, the application should run and enable the user to select File -> Open to load the image containing the barcode(s) data. Select Barcode -> Read Barcode(s) to have the application run barcode recognition, and to output all the relevant barcode information from the loaded image.

Note: If there's no barcode image available for testing, use the image at this file path: C:\LEADTOOLS23\Resources\Images\barcode1.tif

Barcode symbology recognition result dialog box

Wrap-up

This tutorial covered how to read barcode information using the LEADTOOLS SDK in a Windows API application. It also covered how to use the BARCODEDATA structure, with the L_BarCodeRead function.

See Also

Help Version 23.0.2024.5.22
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.