1
+ // /////////////////////////////////////////////////////////////////////
2
+ // File: apiexample.cpp
3
+ // Description: Api Example for Tesseract.
4
+ // Author: ShreeDevi Kumar
5
+ //
6
+ // Licensed under the Apache License, Version 2.0 (the "License");
7
+ // you may not use this file except in compliance with the License.
8
+ // You may obtain a copy of the License at
9
+ // http://www.apache.org/licenses/LICENSE-2.0
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ // /////////////////////////////////////////////////////////////////////
16
+ #include " gtest/gtest.h"
17
+ #include " tesseract/baseapi.h"
18
+ #include " leptonica/allheaders.h"
19
+ #include < iostream>
20
+ #include < string>
21
+ #include < fstream>
22
+ #include < locale>
23
+
24
+ TEST (TesseractTest, ApiExample)
25
+ {
26
+ const char * imagefile = " ../testing/phototest.tif" ;
27
+ const char * groundtruth = " testfiles/phototest.txt" ;
28
+ char *outText;
29
+ std::locale loc (" en_US.UTF-8" );
30
+ std::ifstream file (groundtruth);
31
+ file.imbue (loc);
32
+ std::string gtText ((std::istreambuf_iterator<char >(file)),
33
+ std::istreambuf_iterator<char >());
34
+ tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI ();
35
+ if (api->Init (NULL , " eng" )) {
36
+ fprintf (stderr, " Could not initialize tesseract.\n " );
37
+ exit (1 );
38
+ }
39
+ Pix *image = pixRead (imagefile);
40
+ api->SetImage (image);
41
+ api->SetPageSegMode (tesseract::PSM_AUTO_OSD);
42
+ outText = api->GetUTF8Text ();
43
+ ASSERT_EQ (gtText,outText) << " Phototest.tif with default values OCR does not match ground truth" ;
44
+ api->End ();
45
+ delete [] outText;
46
+ pixDestroy (&image);
47
+ }
48
+
49
+ int main (int argc, char **argv) {
50
+ ::testing::InitGoogleTest (&argc, argv);
51
+ return RUN_ALL_TESTS ();
52
+ }
0 commit comments