The 4Test OCR Functions

This functionality is supported only if you are using the Classic Agent.

OCR.inc includes the following 4Test functions for OCR:
Function Description Parameters Syntax
OcrGetTextFromBmp Converts a bitmap file into text. The conversion uses the pre-configured pattern file, which is specified in the Database Path setting in the textract.ini file.
iRet
Result length. If conversion fails, then an E_OCR exception will be raised. INTEGER.
sOcrText
The result of converting the bitmap to text. NULL if conversion failed. OUT. STRING.
sBitmapFile
The bitmap (.bmp) file to convert. STRING.
iRet = OcrGetTextFromBmp (sOcrText, sBitmapFile)
OcrGetTextFromWnd Converts a bitmap of a window, or an area within a window, into text. The conversion uses the pre-configured pattern file, which is specified in the Database Path setting in the textract.ini file.
iRet
Result length. If conversion fails, then an E_OCR exception will be raised. INTEGER.
sText
The result of converting a bitmap of the window to text. NULL if conversion failed. OUT. STRING.
wWindow
The window that will be the source of the bitmap to be converted to text. WINDOW.
rCapture
The capture region. OPTIONAL. RECT.
iRet = OcrGetTextFromWnd (sText, wWindow[, rCapture])

Example

The sample test script (ocrtest.t) includes a test case (shown below) that extracts the text from a Microsoft Word document. Microsoft Office controls are recognized as custom windows (CustomWin) by Silk Test Classic, so you cannot use the 4Test GetText() method to get the text. However, you can use the OcrGetTextFromWnd() function to capture a bitmap of the document window and convert it to text. Notice that, if necessary, the test case will scroll through the document and capture multiple bitmaps.

testcase GetOcrAPIDocText (STRING sDocument)
  MSWord.SetActive ()

  // Open the specified document.
  MSWord.OpenDoc (sDocument)

  // Capture each page in succession.
  // Start at the top and page down until the bottom is reached
  LIST OF STRING lsResults = {}
  STRING sResult = NULL
  INTEGER iMaxPos, iCurPos, iLastPos = -1
  INTEGER iResLen, iTotalLen = 0

  withoptions
    BindAgentOption (OPT_REQUIRE_ACTIVE, FALSE)
    BindAgentOption (OPT_VERIFY_ACTIVE, FALSE)
    TheDoc.ScrollBarV.ScrollToMin ()
    iCurPos = TheDoc.ScrollBarV.GetPosition ()
    iMaxPos = TheDoc.ScrollBarV.GetRange ().iMax

  while TRUE
    // If we are capturing the first page, then eliminate the flashing cursor
    // by highlighting the current character. Otherwise, page down to capture
    // the next page.
    MSWord.SetActive ()
    if sResult == NULL
      // First page
      MSWord.TypeKeys ("<Shift-Right>")
    else
      // Page down
      withoptions
        BindAgentOption (OPT_REQUIRE_ACTIVE, FALSE)
        BindAgentOption (OPT_VERIFY_ACTIVE, FALSE)
        TheDoc.ScrollBarV.ScrollByPage (1)
        iLastPos = iCurPos
        iCurPos = TheDoc.ScrollBarV.GetPosition ()
      // If scrolling did not change the scrollbar position, then
      // we have reached the bottom. Also, if we scrolled up instead
      // of down by paging down, then we have reached the bottom.
      if iCurPos <= iLastPos
        break

    // Convert the bitmap for the current view.
    iResLen = OcrGetTextFromWnd (sResult, TheDoc.CurrentView)
    if sResult != NULL
      ListAppend (lsResults, sResult)
      iTotalLen = iTotalLen + Len (sResult)
  
  ResPrintList ("Document text ({iTotalLen} chars)", lsResults)

If neither of the 4Test functions in OCR.inc provides the functionality that you need, you can call the DLL functions in the Borland DLL, SgOcrLib.dll, through directly using the function declarations in SgOcrLib.inc. The DLL functions are documented at the top of SgOcrLib.inc. The functions in OCR.inc can serve as an example of how to use the DLL functions.