Data Matrix Code Reading

This sample illustrates the following

Explanation

The function F_VN_ReadDataMatrixCodeExp searches for Data Matrix Codes in the specified image and reads them. In contrast to the F_VN_ReadDataMatrixCode function, the number of codes to be searched for each image can be specified. In this sample, it is one code per image. In addition, the contour of the code that is found is returned. TCVN_CSS_ONLY_NOT_FLIPPED is selected as the search strategy, since the image is not mirrored.

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

// Data Matrix 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 DMC Code Reading Function monitored by the Watchdog function
// ---------------------------------------------------------------
hrWD := F_VN_StartRelWatchdog(tStop, S_OK);
    hr := F_VN_ReadDataMatrixCodeExp(
        ipSrcImage      :=  ipImageIn,
        ipDecodedData   :=  ipCodeDecodedList,
        ipContours      :=  ipCodeContourList,
        nCodeNumber     :=  1,
        eSearchStrategy :=  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_PutText(sCodeAsString, ipImageRes, 25, 50, ETcVnFontType.TCVN_FT_HERSHEY_PLAIN, 2, aColorRed, 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_PutLabelExp(sText, ipImageRes, 25, 50, 2, 2, ETcVnFontType.TCVN_FT_HERSHEY_PLAIN, aColorRed, aColorWhite, ETcVnLineType.TCVN_LT_8_CONNECTED, hr);
END_IF

// Write Code Reading proceeded time into Result Image
sText := CONCAT(CONCAT('Time: ', DINT_TO_STRING(tStop - tRest)), 'us');
hr := F_VN_PutText(sText, ipImageRes, 25, 80, ETcVnFontType.TCVN_FT_HERSHEY_PLAIN, 2, aColorRed, hr);

Result

Data Matrix Code Reading 1:

Similar samples