在Windows里使用Dynamsoft Barcode Reader C++ API
在Windows桌面應用程序Dynamsoft的C ++條形碼閱讀器庫為Windows允許你幾乎立即嵌入一維和二維條碼閱讀功能。我將演示如何使用C ++條形碼閱讀API的Windows構(gòu)建一個Win32控制臺應用程序條形碼識別。
開始一個新文件
新的控制臺應用程序
首先,讓我們打開Visual Studio。
通過Visual C ++> Win32創(chuàng)建一個空的Win32控制臺應用程序項目。
讓我們把它命名為BarcodeReaderC ++ API

添加引用
按照本指南,首先,引用.H和.LIB文件。 為了更容易,讓我們從包含文件夾和lib文件夾從安裝目錄復制到項目。
在此處更改相對路徑。
#include
#include "/If_DBRP.h"
#ifdef _WIN64
#pragma comment ( lib, "/x64/DBRx64.lib" )
#else
#pragma comment ( lib, "/x86/DBRx86.lib" )
#endif

復制主函數(shù)
接下來,將以下代碼插入主函數(shù)。
//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);

升級源圖片
改變圖片路徑。我將會用一個例子圖片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.

復制條碼DLL
轉(zhuǎn)到安裝目錄,在Components \ C_C ++ \ Redist文件夾下,復制這兩個DLL - DynamsoftBarcodeReaderx86.dll和DynamsoftBarcodeReaderx64.dll。 將其粘貼到與BarcodeReaderC ++ API.exe相同的文件夾中,默認情況下,該文件位于解決方案的Debug文件夾下。
按Ctrl + F5運行項目。 好。 我們已經(jīng)識別所有的條形碼。

查看代碼
現(xiàn)在,讓我們快速瀏覽代碼。 首先,我們定義包括圖像路徑的變量。 然后我們配置許可證信息。 使用此片段,我們初始化條形碼閱讀選項,如條形碼類型,以及每頁讀取多少條形碼。 調(diào)用DecodeFile方法來解碼條形碼。 如果找到多個條形碼,我們使用循環(huán)逐個打印出結(jié)果。
//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);
}

記住要釋放BarcodeResultArray
最后但也同樣重要,我們需要釋放BarcodeResultArray。 請注意這一步很重要。
//Finally release BarcodeResultArray
CBarcodeReader::FreeBarcodeResults(&paryResult);

其他條形碼閱讀功能
最后,如果要從圖像的特定區(qū)域解碼條形碼,則有一個DecodeFileRect方法。 Dynamsoft的條形碼讀取器SDK還支持從設備無關(guān)位圖(也稱為DIB),緩沖區(qū)和base64字符串讀取條形碼。

京ICP備09015132號-996 | 違法和不良信息舉報電話:4006561155
© Copyright 2000-2026 北京哲想軟件有限公司版權(quán)所有 | 地址:北京市海淀區(qū)西三環(huán)北路50號豪柏大廈C2座11層1105室
北京哲想軟件集團旗下網(wǎng)站:哲想軟件 | 哲想動畫