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

C++实现DotCode扫码_第1张图片

源码

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

你可能感兴趣的:(Barcode)