C++實現DotCode掃碼

Dynamsoft Barcode SDK v7.4支持DotCode類型。我用C++寫了一個簡單的示例。

DotCode桌面掃碼程序

Dynamsoft SDK中提供了兩種類型的接口:

  • 一種是直接掃碼的接口,比如decodeFile, decodeBuffer
  • 一種是爲視頻流設計的接口StartFrameDecoding()StopFrameDecoding()AppendFrame()

在視頻流的場景中,如果使用直接掃碼的接口,需要自己去創建線程,並設計幀過濾的策略。所以使用視頻流接口會方便很多,只需要在回調函數裏對得到的數據進行處理。

創建barcode reader實例,並配置參數:

// Get license from https://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);

啓動視頻解碼線程:

reader.StartFrameDecoding(10, 10, width, height, frame.step.p[0], IPF_RGB_888, "");

在循環中獲取視頻幀,放如到掃碼隊列中:

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()獲取結果:

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():

void intermediateResultCallback(int frameId, IntermediateResultArray *pResults, void * pUser)
{
    if (id == frameId)
    {
    }
}

這個回調函數是在結果回調之後觸發的,可以返回圖像數據。比較麻煩的是需要通過全局變量id來判斷。

獲得圖像指針:

ImageData* tempImageData = (ImageData*)(pResults->results[0]->results[0]);

bytes轉換成Mat類型用於OpenCV顯示:

Mat resultImage = Mat(tempImageData->height, tempImageData->width, CV_8UC3, tempImageData->bytes);  

通過座標點畫線:

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);

這時候不能立刻調用imshow來顯示結果,因爲在線程的回調中。處理方法同樣是要用到全局變量,然後在主線程中畫出來:

for (;;)
{
    int key = waitKey(10);
    if ((key &amp; 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(); 

運行編譯程序:

mkdir build
cd build
cmake -G"Visual Studio 15 2017 Win64" ..
cmake --build .
.\debug\BarcodeReader.exe

在這裏插入圖片描述

源碼

https://github.com/yushulx/dotcode-webcam-scanner

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章