翻譯|使用教程|編輯:莫成敏|2020-06-01 15:32:49.577|閱讀 672 次
概述:Dynamsoft Barcode Reader SDK一款多功能的條碼讀取控件,只需要幾行代碼就可以將條碼讀取功能嵌入到Web或桌面應(yīng)用程序。在本文中,我將分享一個(gè)新的用C ++實(shí)現(xiàn)的網(wǎng)絡(luò)攝像頭掃描器示例。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Dynamsoft Barcode Reader SDK一款多功能的條碼讀取控件,只需要幾行代碼就可以將條碼讀取功能嵌入到Web或桌面應(yīng)用程序。這可以節(jié)省數(shù)月的開發(fā)時(shí)間和成本。能支持多種圖像文件格式以及從攝像機(jī)或掃描儀獲取的DIB格式。使用Dynamsoft Barcode Reader SDK,你可以創(chuàng)建強(qiáng)大且實(shí)用的條形碼掃描儀軟件,以滿足你的業(yè)務(wù)需求。
點(diǎn)擊下載Dynamsoft Barcode Reader試用版
在本文中,我將分享一個(gè)新的用C ++實(shí)現(xiàn)的網(wǎng)絡(luò)攝像頭掃描器示例。
使用Dynamsoft Barcode SDK讀取DotCode
下載Dynamsoft Barcode Reader v7.4。
OpenCV始終是獲取網(wǎng)絡(luò)攝像頭視頻流的首選。我的版本是OpenCV 3.4.7。如果要使用最新的OpenCV庫,則必須更新CMakeLists.txt文件中的相關(guān)鏈接選項(xiàng)。這是我的:
target_link_libraries (BarcodeReader "DBRx64" "opencv_core347d.lib" "opencv_highgui347d.lib" "opencv_videoio347d.lib" "opencv_imgcodecs347d.lib" "opencv_imgproc347d.lib")
您需要了解的Dynamsoft Barcode SDK API
Dynamsoft提供了靈活的API,用于使條形碼算法適應(yīng)不同的情況。對于從攝像頭視頻幀解碼條形碼的情況,您有兩種選擇:
使用視頻解碼API更為簡單,因?yàn)樗?已經(jīng)實(shí)現(xiàn)了線程和幀篩選算法。您剩下的工作是處理回調(diào)函數(shù)中返回的數(shù)據(jù)。
從視頻流中讀取DotCode
實(shí)例化條形碼讀取器并配置參數(shù):
// Get license from //www.dynamsoft.com/CustomerPortal/Portal/Triallicense.aspx
CBarcodeReader reader = reader.InitLicense("LICENSE-LEY");
PublicRuntimeSettings runtimeSettings;
char szErrorMsg[256];
reader.InitRuntimeSettingsWithString("{\"ImageParameter\":{\"Name\":\"BestCoverage\",\"DeblurLevel\":9,\"ExpectedBarcodesCount\":512,\"ScaleDownThreshold\":100000,\"LocalizationModes\":[{\"Mode\":\"LM_CONNECTED_BLOCKS\"},{\"Mode\":\"LM_SCAN_DIRECTLY\"},{\"Mode\":\"LM_STATISTICS\"},{\"Mode\":\"LM_LINES\"},{\"Mode\":\"LM_STATISTICS_MARKS\"}],\"GrayscaleTransformationModes\":[{\"Mode\":\"GTM_ORIGINAL\"},{\"Mode\":\"GTM_INVERTED\"}]}}", CM_OVERWRITE, szErrorMsg, 256);
reader.GetRuntimeSettings(&runtimeSettings);
runtimeSettings.barcodeFormatIds = BF_ALL;
runtimeSettings.barcodeFormatIds_2 = BF2_POSTALCODE | BF2_DOTCODE;
runtimeSettings.intermediateResultTypes = IRT_ORIGINAL_IMAGE;
reader.UpdateRuntimeSettings(&runtimeSettings,szErrorMsg,256);
reader.SetTextResultCallback(textResultCallback,NULL);
reader.SetIntermediateResultCallback(intermediateResultCallback, NULL);
reader.SetErrorCallback(errorcb, NULL);
初始化并啟動(dòng)視頻解碼線程:
reader.StartFrameDecoding(10, 10, width, height, frame.step.p[0], IPF_RGB_888, "");
在視頻捕獲循環(huán)中添加幀:
for (;;)
{
int key = waitKey(10);
if ((key & 0xff) == 27/*ESC*/) break;
capture >> frame; // read the next frame from camera
if (frame.empty())
{
cerr << "ERROR: Can't grab camera frame." << endl;
break;
}
reader.AppendFrame(frame.data);
imshow("Dynamsoft Barcode Reader", frame);
}
在textResultCallback()函數(shù)中獲取結(jié)果:
void textResultCallback(int frameId, TextResultArray *pResults, void * pUser)
{
char * pszTemp = NULL;
char * pszTemp1 = NULL;
char * pszTemp2 = NULL;
pszTemp = (char*)malloc(4096);
for (int iIndex = 0; iIndex < pResults->resultsCount; iIndex++)
{
snprintf(pszTemp, 4096, "Barcode %d:\r\n", iIndex + 1);
printf(pszTemp);
snprintf(pszTemp, 4096, " Type: %s\r\n", pResults->results[iIndex]->barcodeFormatString_2);
printf(pszTemp);
snprintf(pszTemp, 4096, " Value: %s\r\n", pResults->results[iIndex]->barcodeText);
printf(pszTemp);
pszTemp1 = (char*)malloc(pResults->results[iIndex]->barcodeBytesLength * 3 + 1);
pszTemp2 = (char*)malloc(pResults->results[iIndex]->barcodeBytesLength*3 + 100);
ToHexString(pResults->results[iIndex]->barcodeBytes, pResults->results[iIndex]->barcodeBytesLength, pszTemp1);
snprintf(pszTemp2, pResults->results[iIndex]->barcodeBytesLength*3 + 100, " Hex Data: %s\r\n", pszTemp1);
printf(pszTemp2);
free(pszTemp1);
free(pszTemp2);
}
free(pszTemp);
}
您可以將幀ID分配給全局變量,并在intermediateResultCallback()函數(shù)中獲取對應(yīng)的圖像:
void intermediateResultCallback(int frameId, IntermediateResultArray *pResults, void * pUser)
{
if (id == frameId)
{
}
}
獲取圖像指針:
ImageData* tempImageData = (ImageData*)(pResults->results[0]->results[0]);
將字節(jié)轉(zhuǎn)換為Mat類型:
Mat resultImage = Mat(tempImageData->height, tempImageData->width, CV_8UC3, tempImageData->bytes);
按點(diǎn)繪制條形碼位置:
TextResult *barcode = results->results[i];
int x1 = barcode->localizationResult->x1;
int y1 = barcode->localizationResult->y1;
int x2 = barcode->localizationResult->x2;
int y2 = barcode->localizationResult->y2;
int x3 = barcode->localizationResult->x3;
int y3 = barcode->localizationResult->y3;
int x4 = barcode->localizationResult->x4;
int y4 = barcode->localizationResult->y4;
line( resultImage, Point(x1, y1), Point(x2, y2), color, thickness);
line( resultImage, Point(x2, y2), Point(x3, y3), color, thickness);
line( resultImage, Point(x3, y3), Point(x4, y4), color, thickness);
line( resultImage, Point(x4, y4), Point(x1, y1), color, thickness);
我們不能立即顯示圖像,因?yàn)楸仨氃谥骶€程中調(diào)用imshow()函數(shù)。使Mat數(shù)據(jù)可全局訪問,并在視頻捕獲循環(huán)中進(jìn)行渲染:
for (;;)
{
int key = waitKey(10);
if ((key & 0xff) == 27/*ESC*/) break;
if (!isVideoRunning)
{
if (isResultReady)
{
imshow("Dynamsoft Barcode Reader", resultImage);
break;
}
continue;
}
capture >> frame; // read the next frame from camera
if (frame.empty())
{
cerr << "ERROR: Can't grab camera frame." << endl;
break;
}
reader.AppendFrame(frame.data);
imshow("Dynamsoft Barcode Reader", frame);
}
最后,停止條形碼讀取線程,然后退出程序:
reader.StopFrameDecoding();
生成并運(yùn)行應(yīng)用
在cmd.exe中執(zhí)行以下命令以生成并運(yùn)行該程序:
mkdir build
cd build
cmake -G"Visual Studio 15 2017 Win64" ..
cmake --build .
.\debug\BarcodeReader.exe
獲取條形碼結(jié)果后,按任意鍵退出該應(yīng)用程序。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自: