Using Dynamsoft Barcode Reader C++ API in Windows

Dynamsoft's C++ Barcode Reader library for Windows allows you to almost instantly embed 1D and 2D barcode reading functionality in your Windows Desktop applications.

In this video, I am going to demonstrate how to use the C++ barcode reading API for Windows to build a Win32 Console Application for barcode recognition.

Start a new file

  • New a console application magnify

    New a console application

    First, let's open Visual Studio.

    Create an empty Win32 Console Application project through Visual C++>Win32.
    Let's name it BarcodeReaderC++API

    New a console application close
    New a console application
  • Add references magnify

    Add references

    Following this guide, first, reference the .H and .LIB files. To make it easier, let's copy the Include folder and the Lib folder from the installation directory to the project.

    Change the relative path here.

    #include <stdio.h>
    #include "<relative path>/DynamsoftBarcodeReader.h"
    #ifdef _WIN64
    #pragma comment ( lib, "<relative path>/x64/DBRx64.lib" )
    #else
    #pragma comment ( lib, "<relative path>/x86/DBRx86.lib" )
    #endif
                    
    Add references close
    Add references
  • Copy the main function magnify

    Copy the main function

    Next, insert the following code to the main function.

    //Define variables
    const char * pszImageFile = "<your image file full path>";
    int iIndex = 0;
    int iRet = -1;
            
    //Initialize license prior to any decoding
    CBarcodeReader reader;
    reader.InitLicense("<your license key here>");
    
    reader.SetBarcodeFormats(BF_OneD);	//Expected barcode types to read.
    reader.SetMaxBarcodesNumPerPage(100);	//Expected barcode numbers to read.
    
    //Start decoding
    iRet = reader.DecodeFile(pszImageFile);
    
    //If not DBR_OK
    if (iRet != DBR_OK)
    {
      printf("Failed to read barcode: %d\r\n%s\r\n",iRet, GetErrorString(iRet));
      return iRet;
    }
    
    //If DBR_OK
    SBarcodeResultArray* paryResult = NULL;
    reader.GetBarcodes(&paryResult);
    printf("%d total barcodes found. \r\n", paryResult->iBarcodeCount);
    for (iIndex = 0; iIndex < paryResult->iBarcodeCount; iIndex++)
    {
      printf("Result %d\r\n", iIndex + 1);
      printf("PageNum: %d\r\n", paryResult->ppBarcodes[iIndex]->iPageNum);
      printf("BarcodeFormat: %d\r\n", paryResult->ppBarcodes[iIndex]->emBarcodeFormat);
      printf("Text read: %s\r\n", paryResult->ppBarcodes[iIndex]->pBarcodeData);
    }
    
    //Finally release BarcodeResultArray
    CBarcodeReader::FreeBarcodeResults(&paryResult);
    
                    
    Copy the main function close
    Copy the main function
  • Update source image magnify

    Update source image

    Change the image path. I am going to use a sample image from the installation folder. Copy the file path, add the escape character, and then the file name. Change the path to "C:\\Program Files (x86)\\Dynamsoft\\Barcode Reader 5.2\\Images\\AllSupportedBarcodeTypes.tif".

    const char * pszImageFile = "C:\\Program Files (x86)\\Dynamsoft\\Barcode
     Reader 5.2\\Images\\AllSupportedBarcodeTypes.tif";
                  

    Build the project. Build Succeeded.

    Update source image close
    Update source image
  • Copy barcode DLLs magnify

    Copy barcode DLLs

    Go to the installation directory, under the Components\C_C++\Redist folder, copy the two DLLs - DynamsoftBarcodeReaderx86.dll and DynamsoftBarcodeReaderx64.dll. Paste them to the same folder as BarcodeReaderC++API.exe, which is under the Debug folder of the solution by default.

    Press Ctrl+F5 to run the project. OK. We've got all the barcodes recognized.

    Copy barcode DLLs close
    Copy barcode DLLs
  • Review the code magnify

    Review the code

    Now, let's go over the code quickly. First, we define variables including the image path. And then we configure the license info. With this snippet, we initialize the barcode reading options, such as barcode types, and how many barcodes to read per page. Call the DecodeFile method to decode the barcodes. If there are multiple barcodes found, we use a loop to print out the results one by one.

    //Start decoding
    iRet = reader.DecodeFile(pszImageFile);
    
    //If not DBR_OK
    if (iRet != DBR_OK)
    {
      printf("Failed to read barcode: %d\r\n%s\r\n",iRet, GetErrorString(iRet));
      return iRet;
    }
    
    //If DBR_OK
    SBarcodeResultArray* paryResult = NULL;
    reader.GetBarcodes(&paryResult);
    printf("%d total barcodes found. \r\n", paryResult->iBarcodeCount);
    for (iIndex = 0; iIndex < paryResult->iBarcodeCount; iIndex++)
    {
      printf("Result %d\r\n", iIndex + 1);
      printf("PageNum: %d\r\n", paryResult->ppBarcodes[iIndex]->iPageNum);
      printf("BarcodeFormat: %d\r\n", paryResult->ppBarcodes[iIndex]->emBarcodeFormat);
      printf("Text read: %s\r\n", paryResult->ppBarcodes[iIndex]->pBarcodeData);
    }
                  
    Review the code close
    Review the code
  • Rememeber to release BarcodeResultArray magnify

    Remember to release BarcodeResultArray

    Last but not the least, we need to release BarcodeResultArray. Please note this step is very important.

    //Finally release BarcodeResultArray
    CBarcodeReader::FreeBarcodeResults(&paryResult);
                    
    Rememeber to release BarcodeResultArray close
    Rememeber to release BarcodeResultArray