ukui-search/libsearch/index/ocrobject.cpp

100 lines
2.4 KiB
C++
Raw Normal View History

#include "ocrobject.h"
OcrObject *OcrObject::m_instance = nullptr;
once_flag g_instanceFlag;
OcrObject *OcrObject::getInstance()
{
std::call_once(g_instanceFlag, [] () {
m_instance = new OcrObject;
});
return m_instance;
}
void OcrObject::getTxtContent(QString &path, QString &textcontent)
{
2022-04-13 13:46:30 +08:00
// m_api = new tesseract::TessBaseAPI();
// if (m_api->Init(NULL, "chi_sim")) {
// qDebug() << "Could not initialize tesseract.\n";
// return;
// }
2022-04-13 13:46:30 +08:00
// m_api->SetVariable("user_defined_dpi", "1080");//图片中未标明分辨率的默认设置为1080
// Pix *image = pixRead(path.toStdString().data());
// if (!image) {
// qDebug() << "path:" << path <<" pixRead error!";
2022-04-13 13:46:30 +08:00
// if (m_api) {
// m_api->End();
// delete m_api;
// m_api = nullptr;
// }
// return;
// }
2022-04-13 13:46:30 +08:00
// m_api->SetImage(image);
// textcontent = m_api->GetUTF8Text();
// qDebug() << "path:" << path << " Text:" << textcontent;
// pixDestroy(&image);
2022-04-13 13:46:30 +08:00
// m_api->Clear();
2022-04-13 13:46:30 +08:00
// if (m_api) {
// m_api->End();
// delete m_api;
// m_api = nullptr;
// }
2022-04-13 13:46:30 +08:00
//多进程版本
//qDebug() << "path:" << path;
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
if (api->Init(NULL, "chi_sim")) {
qDebug() << "Could not initialize tesseract.\n";
return;
}
api->SetVariable("user_defined_dpi", "1080");//图片中未标明分辨率的默认设置为1080
Pix *image = pixRead(path.toStdString().data());
if (!image) {
qDebug() << "path:" << path <<" pixRead error!";
if (api) {
api->End();
delete api;
api = nullptr;
}
return;
}
api->SetImage(image);
textcontent = api->GetUTF8Text();
//qDebug() << " Text:" << textcontent;
pixDestroy(&image);
api->Clear();
if (api) {
api->End();
delete api;
api = nullptr;
}
}
OcrObject::OcrObject(QObject *parent) : QObject(parent)
{
2022-04-13 13:46:30 +08:00
// init();
}
OcrObject::~OcrObject()
{
2022-04-13 13:46:30 +08:00
// if (m_api) {
// m_api->End();
// delete m_api;
// m_api = nullptr;
// }
}
void OcrObject::init()
{
m_api = new tesseract::TessBaseAPI();
if (m_api->Init(NULL, "chi_sim")) {
qDebug() << "Could not initialize tesseract.\n";
return;
}
m_api->SetVariable("user_defined_dpi", "1080");//图片中未标明分辨率的默认设置为1080
}