Hello, world!
Contents
[
Hide
]
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++.
We assume that you already have a basic knowledge of Microsoft Visual Studio and 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
-
Create a new C++ project in Visual Studio. You can use a very basic project template, such as Console App.
-
Install Aspose.OCR.Cpp (CPU-based) NuGet package to the project.
-
Save this image to x64\Debug directory of the project under the name
source.png
:
If this folder does not exist, run the application.
Coding
- Include the required header files to the project:
#include <iostream> #include <stdio.h> #include <fcntl.h> #include <io.h> #include <aspose_ocr.h>
- Provide the path to the recognized image:
std::string image_path = "source.png";
- Prepare the buffer for recognition results (in characters):
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);
- 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.
If you use your own image, consider the trial restrictions.
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++.