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. 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
First, let's open Visual Studio.
Create an empty Win32 Console Application project through Visual C++>Win32.
Let's name it BarcodeReaderC++API
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
#include "/If_DBRP.h"
#ifdef _WIN64
#pragma comment ( lib, "/x64/DBRx64.lib" )
#else
#pragma comment ( lib, "/x86/DBRx86.lib" )
#endif
Copy the main function
Next, insert the following code to the main function.
//Define variables
const char * pszImageFile = "";
int iIndex = 0;
int iRet = -1;
//Initialize license prior to any decoding
CBarcodeReader reader;
reader.InitLicense("");
//Initialize ReaderOptions
ReaderOptions ro = {0};
ro.llBarcodeFormat = OneD; //Expected barcode types to read.
ro.iMaxBarcodesNumPerPage = 100; //Expected barcode numbers to read.
reader.SetReaderOptions(ro);
//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
pBarcodeResultArray 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: %lld\r\n", paryResult->ppBarcodes[iIndex]->llFormat);
printf("Text read: %s\r\n", paryResult->ppBarcodes[iIndex]->pBarcodeData);
}
//Finally release BarcodeResultArray
CBarcodeReader::FreeBarcodeResults(&paryResult);
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 4.1\\Images\\AllSupportedBarcodeTypes.tif".
const char * pszImageFile = "C:\\Program Files (x86)\\Dynamsoft\\Barcode
Reader 4.1\\Images\\AllSupportedBarcodeTypes.tif";
Build the project. Build Succeeded.
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.
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
pBarcodeResultArray 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: %lld\r\n", paryResult->ppBarcodes[iIndex]->llFormat);
printf("Text read: %s\r\n", paryResult->ppBarcodes[iIndex]->pBarcodeData);
}
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);
Other barcode reading functionality
Finally, if you want to decode barcodes from a specific area of an image, there is a DecodeFileRect Method. Dynamsoft's Barcode Reader SDK also supports reading barcodes from device-independent bitmap (a.k.a., DIB), buffer and base64 string.
© Copyright 2000-2023 COGITO SOFTWARE CO.,LTD. All rights reserved