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

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

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

升級(jí)源圖片
改變圖片路徑。我將會(huì)用一個(gè)例子圖片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.

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

查看代碼
現(xiàn)在,讓我們快速瀏覽代碼。 首先,我們定義包括圖像路徑的變量。 然后我們配置許可證信息。 使用此片段,我們初始化條形碼閱讀選項(xiàng),如條形碼類型,以及每頁(yè)讀取多少條形碼。 調(diào)用DecodeFile方法來(lái)解碼條形碼。 如果找到多個(gè)條形碼,我們使用循環(huán)逐個(gè)打印出結(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。 請(qǐng)注意這一步很重要。
//Finally release BarcodeResultArray
CBarcodeReader::FreeBarcodeResults(&paryResult);

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

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