Hello, world!

In this article, you will learn how to build a bare minimum console application for extracting text from an image with with Aspose.OCR for C++.

You will need

  • A compatible system with Microsoft Visual Studio installed. As an individual developer, you can use a free Visual Studio Community Edition.
  • Some image containing a text. You can simply download the one from the article.
  • 5 minutes of spare time.

Preparing

  1. Create a new C++ project in Visual Studio. You can use a very basic project template, such as Console App.

  2. Install Aspose.OCR.Cpp (CPU-based) NuGet package to the project. This may take up to several minutes depending on your internet connection.

  3. Download this sample image somewhere to your computer under the name source.png:
    Source image

Coding

  1. Include the required header files to the project:

    #include <iostream> #include <string> #include <vector> #include <aspose_ocr.h> 
  2. Declare the following statement to use names for objects and variables from the standard library without mentioning std:: every time:

    using namespace std; 
  3. Provide the recognized image by file path:

    string file = "source.png"; AsposeOCRInput source; source.url = file.c_str(); vector<AsposeOCRInput> content = {source}; 
  4. Specify the recognition language

    RecognitionSettings settings; settings.language_alphabet = language::eng; 
  5. Extract text from the image:

    AsposeOCRRecognitionResult result = asposeocr_recognize(content.data(), content.size(), settings); 
  6. Output the recognized text:

    size_t size = 0; wchar_t* buffer = asposeocr_serialize_result(result, size); wcout << wstring(buffer) << endl; 
  7. Release the resources

    asposeocr_free_result(result); 

Full code

#include <iostream> #include <string> #include <vector> #include <aspose_ocr.h>  using namespace std;  int main() { // Provide the image for recognition string file = "source.png"; AsposeOCRInput source; source.url = file.c_str(); vector<AsposeOCRInput> content = {source}; // Set recognition language RecognitionSettings settings; settings.language_alphabet = language::eng; // Extract text from the image AsposeOCRRecognitionResult result = asposeocr_recognize(content.data(), content.size(), settings); // Output the recognized text size_t size = 0; wchar_t* buffer = asposeocr_serialize_result(result, size); wcout << wstring(buffer) << endl; // Release the resources asposeocr_free_result(result); } 

Running

Run the program. You will see extracted text in the console output:

Hello. World! I can read this text 

What’s next

Congratulations! You have performed OCR on an image and extracted the machine-readable text from it. Read the Developer reference and API reference for details on developing advanced applications with Aspose.OCR for C++.