1. 程式人生 > >Tesseract4.0在VS2015 X86 debug中編譯

Tesseract4.0在VS2015 X86 debug中編譯

轉自:https://blog.csdn.net/yph001/article/details/78762518

第一步編譯採用上個連結中的方法,避免了不少坑。

然後把在C盤生成目錄放在了自己的F盤,如圖(tessdata為所用字型檔)

 

 

include目錄根據上個連結部落格最終的目錄,leptonia為自己建的目錄,在C:\Users\13267\.cppan目錄下搜尋把allheader.h所在所有標頭檔案複製進去,config_zuto.h和endianness.h也拷貝當當前目錄下,如圖:

 

程式碼也採用上鍊接博主所用,根據專案需要自己有所改動

#include<iostream>
#include <stdio.h>
#include<windows.h>
#include "leptonica/allheaders.h"
#include "tesseract/capi.h"
using namespace std;
 
 
void die(const char *errstr) {
	fputs(errstr, stderr);
	exit(1);
}
 
 
void ConvertUtf8ToGBK(char **amp, char *strUtf8)  //轉碼
{
	int len = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUtf8, -1, NULL, 0);
	unsigned short * wszGBK = new unsigned short[len + 1];
	memset(wszGBK, 0, len * 2 + 2);
	MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUtf8, -1, (LPWSTR)wszGBK, len);
	len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wszGBK, -1, NULL, 0, NULL, NULL);
	//char *szGBK=new char[len + 1]; 
	*amp = new char[len + 1];
	memset(*amp, 0, len + 1);
	WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wszGBK, -1, *amp, len, NULL, NULL);
}
 
int main(int argc, char *argv[]) {
	TessBaseAPI *handle;
	PIX *img;
	char *text = NULL;
	//讀取圖片,原影象的路徑 
	if ((img = pixRead("test.jpg")) == NULL)
		die("Error reading image\n");
 
	handle = TessBaseAPICreate();
	//載入字型檔及設定語言
	if (TessBaseAPIInit3(handle,"../tessdata", "eng+chi_sim") != 0)
		die("Error initialising tesseract\n");
 
	//設定圖片及識別
	TessBaseAPISetImage2(handle, img); 
	if (TessBaseAPIRecognize(handle, NULL) != 0)
		die("Error in Tesseract recognition\n");
 
	if ((text = TessBaseAPIGetUTF8Text(handle)) == NULL)
		die("Error getting text\n");
 
	char *pResult = NULL;
	ConvertUtf8ToGBK(&pResult, text); //對結果轉碼
	cout << pResult << endl;   //輸出OCR識別的文字資訊
	delete pResult;
 
	system("pause");
	TessDeleteText(text);
	TessBaseAPIEnd(handle);
	TessBaseAPIDelete(handle);
	pixDestroy(&img);
 
	return 0;
}

 

遇到的坑是tessdata語言包路徑問題,最後在生成的exe上層檔案下新建tesserdata目錄,因為VS專案預設路徑為vcxproj所在目錄,const char* datapath = "tessdata";

TessBaseAPIInit3(handle, datapath, "eng") != 0

最終打包生成exe檔案如下圖所示: