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.

  3. Save this image to x64\Debug directory of the project under the name source.png:
    Source image

    If this folder does not exist, run the application.

Coding

  1. Include the required header files to the project:
    #include <iostream>
    #include <stdio.h>
    #include <fcntl.h>
    #include <io.h>
    #include <aspose_ocr.h>
    
  2. Provide the path to the recognized image:
    std::string image_path = "source.png";
    
  3. Prepare the buffer for recognition results (in characters):
    const size_t len = 4096;
    wchar_t buffer[len] = { 0 };
    
  4. Extract text from the image:
    size_t size = aspose::ocr::page(image_path.c_str(), buffer, len);
    
  5. Output the recognized text:
    std::wcout << buffer << L"\n";
    

Full code

#include <iostream>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <aspose_ocr.h>

int main()
{
	// Set output console mode
	_setmode(_fileno(stdout), _O_U16TEXT);
	// Provide the path to the recognized image
	std::string image_path = "source.png";
	// Prepare the buffer for recognition results
	const size_t len = 4096;
	wchar_t buffer[len] = { 0 };
	// Extract text from the image
	size_t size = aspose::ocr::page(image_path.c_str(), buffer, len);
	// Print result
	std::wcout << buffer << L"\n";
	// Exit the program
	std::wcout << "\nRecognition complete. Press any key to exit...";
}

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 extracted the text from the image. Read the Developer reference and API reference for details on developing advanced applications with Aspose.OCR for C++.