OCR code samples (Java SDK)
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(); }
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(); }
See also: