OCR Image to Text and Spelling Correction in Java

We can perform OCR on images or scanned documents to recognize and extract text from images programmatically using Java. We can then run a spell checker to correct spelling errors in the recognized text. In this article, we will learn how to convert an image to text with spelling correction in Java.

The following topics shall be covered in this article:

  1. Java Image to Text Converter with Spelling Correction
  2. OCR Image to Text and Get Spelling Mistakes
  3. Convert Image to Text with Spelling Correction
  4. Save Image to Text with Corrected Spellings
  5. Spell Check Custom Text

Java Image to Text Converter with Spelling Correction

To convert an image to text with spelling corrections, we will be using the Aspose.OCR for Java API. It allows performing OCR on scanned images, smartphone photos, screenshots, areas of an image, and scanned PDFs. It enables us to save the recognized text results in popular document formats. The API also allows converting images to text and autocorrecting spelling mistakes in the recognized text.

The AsposeOCR class is the primary API to perform OCR operations. The RecognizePage(String fullPath, RecognitionSettings settings) method of this class recognizes text on the images of the supported formats. The CorrectSpelling(String text, SpellCheckLanguage language) method of the API replaces misspelled words with correct words in the text. The RecognitionResult class represents the results of image recognition. The saveSpellCheckCorrectedText(string, Format, SpellCheckLanguage, string) method of this class saves the document as plain text, PDF, or Microsoft Word Document. All the supported languages for spell check are defined in the SpellCheckLanguage enumeration.

Please either download the JAR of the API or add the following pom.xml configuration in a Maven-based Java application.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-ocr</artifactId>
    <version>22.6</version>
</dependency>

OCR Image to Text and Get Spelling Mistakes in Java

We can perform OCR on images and get a list of spelling errors in the recognized text by following the steps given below:

  1. Firstly, create an instance of the AsposeOCR class.
  2. Next, initialize an object of the RecognitionSettings class.
  3. Then, get RecognitionResult by calling the RecognizePage() method. It takes the image path and RecognitionSettings object as arguments.
  4. After that, call the getSpellCheckErrorList() method to get a list of misspelled words with suggestions.
  5. Finally, loop through the SpellCheckError list and show the results.

The following sample code shows how to get a list of spelling mistakes from a recognized text in Java.

OCR Image to Text and Get Spelling Mistakes in Java

Get a list of spelling mistakes in Java

Convert Image to Text with Spelling Correction in Java

We can convert an image to text and autocorrect spelling errors by following the steps given below:

  1. Firstly, create an instance of the AsposeOCR class.
  2. Next, initialize an object of the RecognitionSettings class.
  3. Then, get RecognitionResult by calling the RecognizePage() method. It takes the image path and RecognitionSettings object as arguments.
  4. After that, call the getSpellCheckCorrectedText() method to get text with spelling correction.
  5. Finally, show the results.

The following sample code shows how to autocorrect spelling errors from a recognized text in Java.

Convert Image to Text with Spelling Correction in Java

Convert Image to Text with Spelling Correction in Java

Save Image to Text with Corrected Spellings in Java

We can save the recognized text from an image after spelling correction by following the steps given below:

  1. Firstly, create an instance of the AsposeOCR class.
  2. Next, initialize an object of the RecognitionSettings class.
  3. After that, get RecognitionResult by calling the RecognizePage() method. It takes the image path and RecognitionSettings object as arguments.
  4. Finally, call the saveSpellCheckCorrectedText() method to save the text.

The following sample code shows how to save the text with spelling correction in Java.

Spell Check Text in Java

We can also run spell check on a custom text by following the steps given below:

  1. Firstly, create an instance of the AsposeOCR class.
  2. Next, call the CorrectSpelling() method. It takes the text to correct and SpellCheckLanguage as arguments.
  3. Finally, show the results.

The following sample code shows how to spell-check custom text in Java.

This is sample text with errors

Get a Free Evaluation License

You can get a free temporary license to try Aspose.OCR for Java without evaluation limitations.

Conclusion

In this article, we have learned how to:

  • perform OCR and recognize text on images;
  • convert images to text;
  • get a list of the misspelled words along with correct word suggestions;
  • autocorrect spelling mistakes;
  • save the corrected text using Java.

Besides converting an image to text with spelling corrections, you can learn more about Aspose.OCR for Java API using the documentation. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also