C++ Implementation of DotCode Scavenging

Dynamsoft Barcode SDK v7.4 supports the DotCode type.I wrote a simple example in C++.

DotCode Desktop Scavenger

Two types of interfaces are provided in Dynamsoft SDK:

  • One is a direct sweep interface, such as decodeFile, decodeBuffer.
  • One is the interface StartFrameDecoding(), StopFrameDecoding(), and AppendFrame(), designed for video streaming.

In video streaming scenarios, if you use a direct sweep interface, you need to create your own threads and design strategies for frame filtering.So using the video stream interface is a lot easier, just need to process the data in the callback function.

Create a barcode reader instance and configure the parameters:

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

Start the video decoding thread:

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

Get the video frame in a loop and put it in the sweep queue:

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

The result is obtained by the callback function 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);
}

This callback function contains no images and only one image id.So to draw the image, you need to use another callback function, intermediateResultCallback():

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

This callback function is triggered after the result callback and returns image data.The trouble is that it needs to be judged by the global variable id.

Get the image pointer:

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

Convert bytes to Mat type for OpenCV display:

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

Draw lines through coordinate points:

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 cannot be called immediately to display the result at this time because it is in a callback from the thread.The process also uses global variables and draws them in the main thread:

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

End the thread and exit the program:

reader.StopFrameDecoding(); 

Run the compiler:

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

Source code

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

Keywords: Programming SDK cmake OpenCV github

Added by lisa3711 on Tue, 12 May 2020 10:01:48 +0300