字元識別Google開源Tesseract-ocr的DLL調用方法

來源:互聯網
上載者:User

Tesseract-ocr的dll tesseract-3.02.02-win32-lib-include-dirs:
Google-Tesseract-OCR
      解壓之後包括 include檔案夾和Lib檔案夾,配置標頭檔和庫檔案,建立工程進行測試。
#include "baseapi.h"
#include "strngs.h"

#pragma  comment(lib,"libtesseract302.lib")

char * str = "test.jpg";tesseract::TessBaseAPI  api;  api.Init(NULL, "chi_sim", tesseract::OEM_DEFAULT);  //初始化,設定語言套件,中文簡體:chi_sim;英文:eng;也可以自己訓練語言套件//api.SetVariable( "tessedit_char_whitelist", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" );  STRING text_out;  if (!api.ProcessPages(str, NULL, 0, &text_out))  {  return 0;  }  

上邊輸出text_out.string(),結果為utf-8編碼格式,因此需要轉碼,寫了一個轉成GBK的代碼:

string UTF8ToGBK(const std::string& strUTF8){int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);unsigned short * wszGBK = new unsigned short[len + 1];memset(wszGBK, 0, len * 2 + 2);MultiByteToWideChar(CP_UTF8, 0,LPCSTR(strUTF8.c_str()), -1, LPWSTR(wszGBK), len);len = WideCharToMultiByte(CP_ACP, 0,LPCTSTR(wszGBK), -1, NULL, 0, NULL, NULL);char *szGBK = new char[len + 1];memset(szGBK, 0, len + 1);WideCharToMultiByte(CP_ACP,0, LPCTSTR(wszGBK), -1, szGBK, len, NULL, NULL);//strUTF8 = szGBK;std::string strTemp(szGBK);delete[]szGBK;delete[]wszGBK;return strTemp;}

由於是在mfc中應用,因此又寫了一段mfc裡邊轉成unicode的代碼:

wchar_t * result_str;CString result;//utf-8轉換成unicodeint len = MultiByteToWideChar(CP_UTF8,0,text_out.string(),-1,NULL,0);result_str = new wchar_t[len + 1];memset(result_str,0,len + 1);MultiByteToWideChar(CP_UTF8,0,text_out.string(),-1,result_str,len);//識別結果result = result_str;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.