2022-01-21 16:53:19 +08:00
|
|
|
#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")) {
|
2022-01-21 16:53:19 +08:00
|
|
|
// qDebug() << "Could not initialize tesseract.\n";
|
|
|
|
// return;
|
|
|
|
// }
|
2022-04-13 13:46:30 +08:00
|
|
|
// m_api->SetVariable("user_defined_dpi", "1080");//图片中未标明分辨率的默认设置为1080
|
2022-01-21 16:53:19 +08:00
|
|
|
|
|
|
|
// 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;
|
2022-01-21 16:53:19 +08:00
|
|
|
// }
|
|
|
|
// return;
|
|
|
|
// }
|
2022-04-13 13:46:30 +08:00
|
|
|
// m_api->SetImage(image);
|
|
|
|
// textcontent = m_api->GetUTF8Text();
|
2022-01-21 16:53:19 +08:00
|
|
|
// qDebug() << "path:" << path << " Text:" << textcontent;
|
|
|
|
// pixDestroy(&image);
|
2022-04-13 13:46:30 +08:00
|
|
|
// m_api->Clear();
|
2022-01-21 16:53:19 +08:00
|
|
|
|
2022-04-13 13:46:30 +08:00
|
|
|
// if (m_api) {
|
|
|
|
// m_api->End();
|
|
|
|
// delete m_api;
|
|
|
|
// m_api = nullptr;
|
2022-01-21 16:53:19 +08:00
|
|
|
// }
|
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;
|
|
|
|
}
|
2022-01-21 16:53:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
OcrObject::OcrObject(QObject *parent) : QObject(parent)
|
|
|
|
{
|
2022-04-13 13:46:30 +08:00
|
|
|
// init();
|
2022-01-21 16:53:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
OcrObject::~OcrObject()
|
|
|
|
{
|
2022-04-13 13:46:30 +08:00
|
|
|
// if (m_api) {
|
|
|
|
// m_api->End();
|
|
|
|
// delete m_api;
|
|
|
|
// m_api = nullptr;
|
|
|
|
// }
|
2022-01-21 16:53:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|