QR-Code Reading

This sample illustrates the following

Explanation

The function F_VN_ReadPharmaCodeExp searches for QR codes in the specified image and reads them. In contrast to the function F_VN_ReadQRCode, the contour of the code that is found is also returned.

If there are areas in the image that look similar to codes, or if there is no code at all in the image, this can have a negative effect on the execution time. To prevent cycle time overruns, in this sample the function F_VN_ReadQRCodeExp is monitored with the aid of a watchdog (see the sample on Blob Detection with watchdog monitoring).

Variables

hr                  :    HRESULT;

ipImageIn           :    ITcVnImage;
ipImageInDisp       :    ITcVnDisplayableImage;

ipImageRes          :    ITcVnImage;
ipImageResDisp      :    ITcVnDisplayableImage;

// QR Code
ipCodeDecodedList   :    ITcVnContainer;
ipCodeContourList   :    ITcVnContainer;
sCodeAsString       :    STRING(255);

// Watchdog
hrWD                :    HRESULT;
tStop               :    DINT := 20000;
tRest               :    DINT;

// Output
sText               :    STRING;

// Color
aColorRed           :    TcVnVector4_LREAL := [255, 0, 0];

Code

// Execute the QR Code Reading Function monitored by the Watchdog-Function 
// ---------------------------------------------------------------
hrWD := F_VN_StartRelWatchdog(tStop, S_OK);
    hr := F_VN_ReadQRCodeExp(
            ipSrcImage      :=  ipImageIn,
            ipDecodedData   :=  ipCodeDecodedList,
            ipContours      :=  ipCodeContourList,
            nCodeNumber     :=  1,
            eSearchStrategy :=  TCVN_CSS_ONLY_NOT_INVERTED + TCVN_CSS_ONLY_NOT_FLIPPED,
            hrPrev          :=  hr
    );

hrWD := F_VN_StopWatchdog(hrWD, tRest => tRest);

// Check if the function was executed successfully
IF hr = S_OK THEN
    // Export Code into String
    hr := F_VN_ExportSubContainer_String(ipCodeDecodedList, 0, sCodeAsString, 255, hr);

    // Write Code into Result Image
    hr := F_VN_PutTextExp(sCodeAsString, ipImageRes, 50, 100, ETcVnFontType.TCVN_FT_HERSHEY_PLAIN, 5, aColorRed,3, TCVN_LT_4_CONNECTED, FALSE, hr);

    // Draw Code Contour into Result Image
    hr := F_VN_DrawContours(ipCodeContourList, 0, ipImageRes, aColorRed, 3, hr);
ELSE
    // Write HRESULT into Result Image
    sText := CONCAT('Returncode ', DINT_TO_STRING(hr));
    hr := F_VN_PutTextExp(sText, ipImageRes, 50, 100, ETcVnFontType.TCVN_FT_HERSHEY_PLAIN, 5, aColorRed,3, TCVN_LT_4_CONNECTED, FALSE, hr);
END_IF

// Write Code Reading proceeded time into Result Image
sText := CONCAT(CONCAT('Time: ', DINT_TO_STRING(tStop - tRest)), 'us');
hr := F_VN_PutTextExp(sText, ipImageRes, 50, 200, ETcVnFontType.TCVN_FT_HERSHEY_PLAIN, 5, aColorRed,3, TCVN_LT_4_CONNECTED, FALSE, hr);

Result

QR-Code Reading 1:

Similar samples