复制主函数
接下来,将以下代码插入主函数。
//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: %drn%srn",iRet, GetErrorString(iRet));
return iRet;
}
//If DBR_OK
pBarcodeResultArray paryResult = NULL;
reader.GetBarcodes(&paryResult);
printf("%d total barcodes found. rn", paryResult->iBarcodeCount);
for (iIndex = 0; iIndex < paryResult->iBarcodeCount; iIndex++)
{
printf("Result %drn", iIndex + 1);
printf("PageNum: %drn", paryResult->ppBarcodes[iIndex]->iPageNum);
printf("BarcodeFormat: %lldrn", paryResult->ppBarcodes[iIndex]->llFormat);
printf("Text read: %srn", 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
转到安装目录,在Components C_C ++ Redist文件夹下,复制这两个DLL - DynamsoftBarcodeReaderx86.dll和DynamsoftBarcodeReaderx64.dll。 将其粘贴到与BarcodeReaderC ++ API.exe相同的文件夹中,默认情况下,该文件位于解决方案的Debug文件夹下。
按Ctrl + F5运行项目。 好。 我们已经识别所有的条形码。
查看代码
现在,让我们快速浏览代码。 首先,我们定义包括图像路径的变量。 然后我们配置许可证信息。 使用此片段,我们初始化条形码阅读选项,如条形码类型,以及每页读取多少条形码。 调用DecodeFile方法来解码条形码。 如果找到多个条形码,我们使用循环逐个打印出结果。
//Start decoding
iRet = reader.DecodeFile(pszImageFile);
//If not DBR_OK
if (iRet != DBR_OK)
{
printf("Failed to read barcode: %drn%srn",iRet, GetErrorString(iRet));
return iRet;
}
//If DBR_OK
pBarcodeResultArray paryResult = NULL;
reader.GetBarcodes(&paryResult);
printf("%d total barcodes found. rn", paryResult->iBarcodeCount);
for (iIndex = 0; iIndex < paryResult->iBarcodeCount; iIndex++)
{
printf("Result %drn", iIndex + 1);
printf("PageNum: %drn", paryResult->ppBarcodes[iIndex]->iPageNum);
printf("BarcodeFormat: %lldrn", paryResult->ppBarcodes[iIndex]->llFormat);
printf("Text read: %srn", paryResult->ppBarcodes[iIndex]->pBarcodeData);
}
记住要释放BarcodeResultArray
最后但也同样重要,我们需要释放BarcodeResultArray。 请注意这一步很重要。
//Finally release BarcodeResultArray
CBarcodeReader::FreeBarcodeResults(&paryResult);
其他条形码阅读功能
最后,如果要从图像的特定区域解码条形码,则有一个DecodeFileRect方法。 Dynamsoft的条形码读取器SDK还支持从设备无关位图(也称为DIB),缓冲区和base64字符串读取条形码。