ukui-search/libsearch/index/ocrobject.cpp

120 lines
3.2 KiB
C++
Raw Normal View History

2023-04-11 10:19:35 +08:00
/*
*
* Copyright (C) 2023, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
2022-06-18 10:46:14 +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;
}
2022-10-26 18:01:40 +08:00
void OcrObject::getTxtContent(const QString &path, QString &textcontent)
2022-06-18 10:46:14 +08:00
{
// 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
// Pix *image = pixRead(path.toStdString().data());
// if (!image) {
// qDebug() << "path:" << path <<" pixRead error!";
// if (m_api) {
// m_api->End();
// delete m_api;
// m_api = nullptr;
// }
// return;
// }
// m_api->SetImage(image);
// textcontent = m_api->GetUTF8Text();
// qDebug() << "path:" << path << " Text:" << textcontent;
// pixDestroy(&image);
// m_api->Clear();
// if (m_api) {
// m_api->End();
// delete m_api;
// m_api = nullptr;
// }
//多进程版本
//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) {
2022-10-26 18:01:40 +08:00
// qDebug() << "path:" << path <<" pixRead error!";
2022-06-18 10:46:14 +08:00
if (api) {
api->End();
delete api;
api = nullptr;
}
return;
}
api->SetImage(image);
2022-10-26 18:01:40 +08:00
char *tmp = api->GetUTF8Text();
textcontent = QString::fromLocal8Bit(tmp);
delete [] tmp;
2022-06-18 10:46:14 +08:00
//qDebug() << " Text:" << textcontent;
pixDestroy(&image);
api->Clear();
if (api) {
api->End();
delete api;
api = nullptr;
}
}
OcrObject::OcrObject(QObject *parent) : QObject(parent)
{
// init();
}
OcrObject::~OcrObject()
{
// 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
}