A Simple Approach to Convert Image to TEXT in Selenium Using Java

Convert Image to Text in Selenium

While writing automation test cases many a time there arise some scenarios where you need the text which is embodied in the image. Most of the entry-level testers leave that part for manual testers to test that part. But, that’s not the case anymore. With a simple piece of code, you can now extract the text from the given image. And further, write that text in a file and use that text for your Data-Driven Test Cases

With Java OCR and Barcode Recognition SDK, you can process normal fonts of English, Spanish, Portuguese, German and French

A Java OCR SDK Library API will allow you to perform OCR and bar code recognition on images (JPEG, PNG, TIFF, PDF, etc.) and output as plain text, XML with full coordinate, searchable PDF or editable RTF

If you are building your project maven, then add the following dependency in your pom.xml file:

<dependency>
    <groupId>com.asprise.ocr</groupId>
    <artifactId>java-ocr-api</artifactId>
    <version>[15,)</version>
</dependency>

Else, follow below steps to install the library for converting image to text

Steps To Follow:

  1. Based on your OS, Download the Java – Aspire OCR library from here
  2. Unzip the downloaded folder and add the JAR file to your project directory.
  3. The best practice is to copy the file named “aocr.jar” from the folder unzipped folder and place it inside C:\Windows\System32
  4. Add the path in the environment variable and then double click on the jar
  5. Restart your system and you are all set to write the code for your tests

Sample code to convert image to text Selenium in Java

Below is the sample class which picks the image file from the local desktop and extract the text and prints that in console on execution

package com.test.ImageText;

import java.io.File;
import java.io.IOException;
import org.testng.annotations.Test;
import com.asprise.ocr.Ocr;

public class ReadTextFromImage  
{  
	/** The following method will process the image from local directory 
    * and OCR engine will get the text from image and store in a String
	*/
	@Test
	public void startConversion() throws IOException
	{
		// Create the instance of OCR Engine
		Ocr ocr = new Ocr();
		
		// Start OCR Engine
		ocr.startEngine("eng", Ocr.SPEED_FASTEST);
		
		// path of the image
        String getText = ocr.recognize(new File[] { new File("C:\\Users\\Yogesh\\Desktop\\OCRImage\\testImage.JPG") },
		Ocr.RECOGNIZE_TYPE_TEXT, Ocr.OUTPUT_FORMAT_PLAINTEXT);
		
		// Print the text
		System.out.println(getText);
		
		// Stop OCR engine
		ocr.stopEngine();
	}  
}

We have used the following sample image for execution of the above code

Image to Text
Sample Image for Image to Text test

We will get the following output on the execution of ReadTextFromImage.java

Console Output - Image to Text in Selenium
Console Output

As we all know, automation testing is growing today, and certainly, in the future, the demand for automation testers will increase. Let us know your requirements and we will help you in developing an automation framework for your product.

Contact Us Today!