Share this page

# OCR code samples (Java SDK)

In this topic:

## Recognize text using OCR

The following example shows how to use OCR to get text on a web page using the GetVisibleText method.

    @Test
    public void testGetVisibleTextExample() throws Exception {
    	// Launch the Chrome browser and navigate to the web site.
    	Browser browser = BrowserFactory.launch(BrowserType.CHROME);
    	browser.navigate("http://www.google.com");

    	// Pause to ensure the browser has navigated to the wanted page.
    	Thread.sleep(4 * 1000);

    	// Get the visible text in the browser's entire window area
    	String textInBrowser = browser.getVisibleText();
    	Verify.isNotNullOrEmpty(textInBrowser, "Verify Visible Text", "Verify that the browser's window contains some text."); // Verify we have some text

    	// Close the browser
    	browser.close();
    }

Back to top

## Get text location using OCR

The following example shows how to use OCR to get a text location on a web page using the GetTextLocations method.

    @Test
    public void testObjectGetTextLocationsExample() throws Exception {
    	// Launch the Chrome browser and navigate to the web site.
    	Browser browser = BrowserFactory.launch(BrowserType.CHROME);
    	browser.navigate("http://www.google.com");

    	// Pause to ensure the browser has navigated to the wanted page.
    	Thread.sleep(4 * 1000);

    	// Get the rectangles where the text "google" exists in the browser's entire window area.
    	Rectangle[] rectangles = browser.getTextLocations("Google"); // Verify the "Google" text was found in the browser's window, and that there is at least one rectangle with this text
    	Verify.areNotEqual(0, rectangles.length, "Verify Google String Location", "Verify that there is at least one Google string in the browser.");

    	browser.close();
    }

Back to top

See also:

- UFT Developer Java SDK Reference
- UFT Developer Java SDK code samples

Tips to improve your search results... UFT Developer Java SDK Reference Version 15.0-15.0.2 UFT Developer SDK Reference All View Local Help Center View Online Help Center back Go back in browser history forward Go forward in browser history Remove search highlights next topic Go to next topic previous topic Go to previous topic Print the current topic Version: