User Guide - C++
Table of Contents
Requirements
- Windows
- Windows 7, 8, 10, 2003, 2008, 2008 R2, 2012.
- Visual Studio 2008 or above
- Linux
- Linux x64: Ubuntu 14.04.4+ LTS, Debian 8+, etc
- GCC 4.2+
Installation
If you don’t have SDK yet, please go to Dynamsoft website to get it. Once the folder is decompressed, the root directory of the DLR installation package is DynamsoftLabelRecognizer, which we will refer to as [INSTALLATION FOLDER] throughout this guide.
Build Your First Application
Let’s start by creating a console application which demonstrates the minimum code needed to recognize text from an image file.
You can download the complete source code referenced in this guide from here.
Create A New Project
For Windows
-
Open Visual Studio. Go to File > New > Project, select Empty App and enter
DLRCppSamplein thenametext box. -
Add a new source file named
DLRCppSample.cppinto the project.
For Linux
-
Create a new source file named
DLRCppSample.cppand place it into the folder[INSTALLATION FOLDER]\Samples\DLRCppSample. -
Create a file named
Makefileand put it in the same directory as the fileDLRCppSample.cpp. The content ofMakefileis as follows:CC=gcc CCFLAGS=-c DLRLIB_PATH=../../Lib/Linux LDFLAGS=-L $(DLRLIB_PATH) -Wl,-rpath=$(DLRLIB_PATH) -Wl,-rpath=./ DLRLIB=-lDynamsoftLabelRecognizer STDLIB=-lstdc++ TARGET=DLRCppSample OBJECT=DLRCppSample.o SOURCE=DLRCppSample.cpp # build rule for target. $(TARGET): $(OBJECT) $(CC) -o $(TARGET) $(OBJECT) $(STDLIB) $(DLRLIB) $(LDFLAGS) # target to build an object file $(OBJECT): $(SOURCE) $(CC) $(CCFLAGS) $(SOURCE) # the clean target .PHONY : clean clean: rm -f $(OBJECT) $(TARGET)Note: The
DLRLIB_PATHvariable should be set to the correct directory where the DLR library files are located. The files and character models directory can be found in[INSTALLATION FOLDER]/Lib/Linux.
Include the Label Recognizer Library
-
Add headers and libs in
DLRCppSample.cpp.#include <stdio.h> #include "<relative path>/Include/DynamsoftLabelRecognizer.h" using namespace dynamsoft::dlr; // The following code only applies to Windows. #if defined(_WIN64) || defined(_WIN32) #ifdef _WIN64 #pragma comment(lib, "<relative path>/Lib/Windows/x64/DynamsoftLabelRecognizerx64.lib") #else #pragma comment(lib, "<relative path>/Lib/Windows/x86/DynamsoftLabelRecognizerx86.lib") #endif #endifPlease replace
<relative path>in the above code with the relative path to theDLRCppSample.cppfile. TheDynamsoftLabelRecognizer.hfile can be found in[INSTALLATION FOLDER]/Include/. The import lib files (only for Windows) can be found in[INSTALLATION FOLDER]/Lib/.
Initialize the Label Recognizer
-
Initialize the license key
char error[512]; // 1.Initialize license. dlr.InitLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInByb2R1Y3RzIjoyfQ==", error, 512);Note:
- An internet connection is required for the license to work.
- “DLS2***” is a default free public trial license used in the sample.
- You can request a 30-day trial license via the Request a Trial License link.
-
Create an instance of Dynamsoft Label Recognizer
// 2.Create an instance of Label Recognizer. CLabelRecognizer dlr;
Recognition Process and How to Use the Results
-
Recognizing text in an image
// 3.Recognize text from an image file. errorcode = dlr.RecognizeByFile("../../SampleImages/dlr-sample-vin.png", ""); if(errorcode != DLR_OK) printf("%s\n", DLR_GetErrorString(errorcode));You can download the image dlr-sample-vin.png for testing. In addition, you can replace it with the full path of the image you want to recognize.
For the error handling mechanism, the SDK returns Error Code for each function and provides a function
DLR_GetErrorStringto get the readable message. You should add codes for error handling based on your needs. Check out Error Code for the full list of supported error codes. -
Get and output the recognition results
DLRResultArray *pDLRResults = NULL; DLRResult* result = NULL; int lCount, rCount, li, ri; // 4. Get all recognized results. dlr.GetAllResults(&pDLRResults); if (pDLRResults != NULL && pDLRResults->resultsCount > 0) { rCount = pDLRResults->resultsCount; printf("Recognized %d results\n", rCount); for (ri = 0; ri < rCount; ++ri) { // Get result of each text area (also called label). result = pDLRResults->results[ri]; lCount = result->lineResultsCount; for (li = 0; li < lCount; ++li) { // Get the result of each text line in the label. printf("Line result %d: %s\n", li, result->lineResults[li]->text); } } } else { printf("No data detected.\n"); }The recognition results of SDK are organized into a four-tier structure:
DLR_ResultArraycorresponds to the results of animageDLR_Resultcorresponds to the result of aTextArea(also calledLabel)DLR_LineResultcorresponds to the result of eachTextLinein theLabelDLR_CharacterResultcorresponds to the result of eachCharacterin theTextLine
The structure is shown in the figure below:
Figure 1 – DLR Result Structure
Release Allocated Memory
Release the allocated memory for the recognition results
```cpp
if(pDLRResults != NULL)
CLabelRecognizer::FreeResults(&pDLRResults);
```
You can download the similar complete source code from Here.
Build and Run the Project
For windows
-
Build the application through Visual Studio and copy the related DLL files and character models directory to the same folder as the EXE file. The DLL files and character models directory can be found in
[INSTALLATION FOLDER]\Lib\Windows\[platforms].Note: Select the corresponding folder (x86 or x64) based on your project’s platform setting.
-
Run the program
DLRCppSample.exe.
For Linux
-
Open a terminal and change to the target directory where
Makefilelocated in. Build the sample:>make -
Run the program
DLRCppSample.>./DLRCppSample