VS2015+Tesseract4配置與示例(修改版)全套庫類實例

聲明本文的資源及內容引用這位仁兄(https://blog.csdn.net/andylanzhiyong/article/details/81807425)的文章

我只不過是增加了一個50.2MB的最新識別庫罷了(識別率更高了些)

增加了最新中文識別庫 50.2MB的那個! 

整套資源下載地址:https://download.csdn.net/download/blackangelboy/12255296

 

 

附上代碼

附上下載修改版VS2015+Tesseract4全套編譯庫地址

#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;
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章