VS2015+Tesseract4配置与示例(修改版)全套库类实例

声明本文的资源及内容引用这位仁兄(https://blog.****.net/andylanzhiyong/article/details/81807425)的文章

我只不过是增加了一个50.2MB的最新识别库罢了(识别率更高了些)

增加了最新中文识别库 50.2MB的那个! 

整套资源下载地址:https://download.****.net/download/blackangelboy/12255296

 

VS2015+Tesseract4配置与示例(修改版)全套库类实例

VS2015+Tesseract4配置与示例(修改版)全套库类实例

VS2015+Tesseract4配置与示例(修改版)全套库类实例VS2015+Tesseract4配置与示例(修改版)全套库类实例VS2015+Tesseract4配置与示例(修改版)全套库类实例

 

VS2015+Tesseract4配置与示例(修改版)全套库类实例

附上代码

附上下载修改版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;
}