fix: 修复打开多窗口识别崩溃问题
Description: 采用仓库中旧版本的识别库,正在识别过程中不响应启动另外一个识别 Log: 识别过程中不打开多个识别窗口进行识别 Change-Id: Ic75933f6d016c8c045ec5252cd92172219ca9cd5
This commit is contained in:
@ -21,6 +21,7 @@
|
||||
|
||||
#include "ocrapplication.h"
|
||||
#include "mainwindow.h"
|
||||
#include "tessocrutils/tessocrutils.h"
|
||||
#include <DWidgetUtil>
|
||||
|
||||
OcrApplication::OcrApplication(QObject *parent) : QObject(parent)
|
||||
@ -31,16 +32,22 @@ OcrApplication::OcrApplication(QObject *parent) : QObject(parent)
|
||||
bool OcrApplication::openFile(QString filePath)
|
||||
{
|
||||
qDebug() << __FUNCTION__ << __LINE__ << filePath;
|
||||
MainWindow *win = new MainWindow();
|
||||
//增加判断,空图片不会启动
|
||||
bool bRet = win->openFile(filePath);
|
||||
if (bRet) {
|
||||
win->show();
|
||||
//第一次启动才居中
|
||||
if (m_loadingCount == 0) {
|
||||
Dtk::Widget::moveToCenter(win);
|
||||
m_loadingCount++;
|
||||
bool bRet = false;
|
||||
if(!TessOcrUtils::instance()->isRunning())
|
||||
{
|
||||
MainWindow *win = new MainWindow();
|
||||
//增加判断,空图片不会启动
|
||||
bRet = win->openFile(filePath);
|
||||
if (bRet) {
|
||||
win->show();
|
||||
//第一次启动才居中
|
||||
if (m_loadingCount == 0) {
|
||||
Dtk::Widget::moveToCenter(win);
|
||||
m_loadingCount++;
|
||||
}
|
||||
}
|
||||
}else {
|
||||
qDebug() << "正在识别中!";
|
||||
}
|
||||
|
||||
return bRet;
|
||||
@ -51,13 +58,18 @@ void OcrApplication::openImage(QImage image)
|
||||
//增加判断,空图片不会启动
|
||||
if (!image.isNull() && image.width() >= 1) {
|
||||
qDebug() << __FUNCTION__ << __LINE__ << image.size();
|
||||
MainWindow *win = new MainWindow();
|
||||
win->openImage(image);
|
||||
win->show();
|
||||
//第一次启动才居中
|
||||
if (m_loadingCount == 0) {
|
||||
Dtk::Widget::moveToCenter(win);
|
||||
m_loadingCount++;
|
||||
if(!TessOcrUtils::instance()->isRunning())
|
||||
{
|
||||
MainWindow *win = new MainWindow();
|
||||
win->openImage(image);
|
||||
win->show();
|
||||
//第一次启动才居中
|
||||
if (m_loadingCount == 0) {
|
||||
Dtk::Widget::moveToCenter(win);
|
||||
m_loadingCount++;
|
||||
}
|
||||
}else {
|
||||
qDebug() << "正在识别中!";
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -66,13 +78,17 @@ void OcrApplication::openImageAndName(QImage image, QString imageName)
|
||||
{
|
||||
//增加判断,空图片不会启动
|
||||
if (!image.isNull() && image.width() >= 1) {
|
||||
MainWindow *win = new MainWindow();
|
||||
win->openImage(image, imageName);
|
||||
win->show();
|
||||
//第一次启动才居中
|
||||
if (m_loadingCount == 0) {
|
||||
Dtk::Widget::moveToCenter(win);
|
||||
m_loadingCount++;
|
||||
if(!TessOcrUtils::instance()->isRunning()){
|
||||
MainWindow *win = new MainWindow();
|
||||
win->openImage(image, imageName);
|
||||
win->show();
|
||||
//第一次启动才居中
|
||||
if (m_loadingCount == 0) {
|
||||
Dtk::Widget::moveToCenter(win);
|
||||
m_loadingCount++;
|
||||
}
|
||||
}else {
|
||||
qDebug() << "正在识别中!";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -52,7 +52,7 @@ TessOcrUtils *TessOcrUtils::m_tessOcrUtils = nullptr;
|
||||
/**
|
||||
* @brief TessOcrUtils::t_Tesseract 三方库实例化
|
||||
*/
|
||||
//tesseract::TessBaseAPI *TessOcrUtils::t_Tesseract = new tesseract::TessBaseAPI();
|
||||
tesseract::TessBaseAPI *TessOcrUtils::t_Tesseract = new tesseract::TessBaseAPI();
|
||||
|
||||
|
||||
TessOcrUtils::TessOcrUtils()
|
||||
@ -62,6 +62,8 @@ TessOcrUtils::TessOcrUtils()
|
||||
setLanguagesPath(TESSDATA_PATH);
|
||||
//设置当前默认使用的识别语言系统语言+英语
|
||||
m_sLangs = getLanguages();
|
||||
//是否正在识别中
|
||||
m_isRunning = false;
|
||||
}
|
||||
|
||||
TessOcrUtils::~TessOcrUtils()
|
||||
@ -80,6 +82,7 @@ TessOcrUtils *TessOcrUtils::instance()
|
||||
//传入待识别图片的路径和想得到的返回结果类型,获取识别结果
|
||||
RecognitionResult TessOcrUtils::getRecogitionResult(const QString &imagePath,const ResultType &resultType)
|
||||
{
|
||||
m_isRunning = true;
|
||||
QString errorMessage = "";
|
||||
ErrorCode errorCode = ErrorCode::UNKNOWN;
|
||||
RecognitionResult t_result;
|
||||
@ -91,6 +94,7 @@ RecognitionResult TessOcrUtils::getRecogitionResult(const QString &imagePath,con
|
||||
errorMessage = "Can't pass an empty image path!";
|
||||
errorCode = ErrorCode::OCR_P_NULL;
|
||||
setResult(errorCode,errorMessage,resultType,t_result);
|
||||
m_isRunning = false;
|
||||
return t_result;
|
||||
}
|
||||
|
||||
@ -102,6 +106,7 @@ RecognitionResult TessOcrUtils::getRecogitionResult(const QString &imagePath,con
|
||||
errorMessage = "Image does not exist! imagePath: " + imagePath;
|
||||
errorCode = ErrorCode::OCR_P_NULL;
|
||||
setResult(errorCode,errorMessage,resultType,t_result);
|
||||
m_isRunning = false;
|
||||
return t_result;
|
||||
}
|
||||
try {
|
||||
@ -116,6 +121,7 @@ RecognitionResult TessOcrUtils::getRecogitionResult(const QString &imagePath,con
|
||||
errorMessage = "Failed to load picture! " + QString(e.what());
|
||||
errorCode = ErrorCode::OCR_LI_F;
|
||||
setResult(errorCode,errorMessage,resultType,t_result);
|
||||
m_isRunning = false;
|
||||
return t_result;
|
||||
}
|
||||
//获取识别结果
|
||||
@ -131,6 +137,7 @@ RecognitionResult TessOcrUtils::getRecogitionResult(const QString &imagePath)
|
||||
//传入待识别图片和想得到的返回结果类型,获取识别结果
|
||||
RecognitionResult TessOcrUtils::getRecogitionResult(QImage *image, const ResultType &resultType)
|
||||
{
|
||||
m_isRunning = true;
|
||||
QString errorMessage = "";
|
||||
ErrorCode errorCode = ErrorCode::UNKNOWN;
|
||||
RecognitionResult t_result;
|
||||
@ -141,6 +148,7 @@ RecognitionResult TessOcrUtils::getRecogitionResult(QImage *image, const ResultT
|
||||
errorMessage = "Can't pass an empty image!";
|
||||
errorCode = ErrorCode::OCR_P_NULL;
|
||||
setResult(errorCode,errorMessage,resultType,t_result);
|
||||
m_isRunning = false;
|
||||
return t_result;
|
||||
}
|
||||
Pix *p_image;
|
||||
@ -172,6 +180,11 @@ RecognitionResult TessOcrUtils::getRecogitionResult(QImage *image)
|
||||
return getRecogitionResult(image,ResultType::RESULT_STRING);
|
||||
}
|
||||
|
||||
bool TessOcrUtils::isRunning()
|
||||
{
|
||||
return m_isRunning;
|
||||
}
|
||||
|
||||
//获取系统当前的语言
|
||||
Languages TessOcrUtils::getSystemLang()
|
||||
{
|
||||
@ -230,9 +243,11 @@ RecognitionResult TessOcrUtils::getRecogitionResult(Pix * image,ResultType resul
|
||||
errorMessage = "The result type does not exist! resultType: " + QString::number(resultType);
|
||||
errorCode = ErrorCode::OCR_RT_NULL;
|
||||
setResult(errorCode,errorMessage,resultType,t_result);
|
||||
m_isRunning = false;
|
||||
return t_result;
|
||||
}
|
||||
tesseract::TessBaseAPI *t_Tesseract = new tesseract::TessBaseAPI();
|
||||
//最新的tesseract库可以new出来,老版本的库无法在appcation后new出来
|
||||
//tesseract::TessBaseAPI *t_Tesseract = new tesseract::TessBaseAPI();
|
||||
try{
|
||||
//初始化语言包
|
||||
if (t_Tesseract->Init(m_sTessdataPath.toLatin1().data(), m_sLangs.toLatin1().data()))
|
||||
@ -241,6 +256,7 @@ RecognitionResult TessOcrUtils::getRecogitionResult(Pix * image,ResultType resul
|
||||
errorMessage = "Could not initialize tesseract! Tesseract couldn't load any languages!";
|
||||
errorCode = ErrorCode::OCR_INI_F;
|
||||
setResult(errorCode,errorMessage,resultType,t_result);
|
||||
m_isRunning = false;
|
||||
return t_result;
|
||||
}
|
||||
}catch(const std::logic_error &e){
|
||||
@ -248,6 +264,7 @@ RecognitionResult TessOcrUtils::getRecogitionResult(Pix * image,ResultType resul
|
||||
errorMessage = "Could not initialize tesseract! Tesseract internal error! " + QString(e.what());
|
||||
errorCode = ErrorCode::OCR_INI_F;
|
||||
setResult(errorCode,errorMessage,resultType,t_result);
|
||||
m_isRunning = false;
|
||||
return t_result;
|
||||
}
|
||||
//为Tesseract设置待识别图片
|
||||
@ -268,6 +285,7 @@ RecognitionResult TessOcrUtils::getRecogitionResult(Pix * image,ResultType resul
|
||||
errorMessage = "Image recognition failed! " + QString(e.what());
|
||||
errorCode = ErrorCode::OCR_LI_F;
|
||||
setResult(errorCode,errorMessage,resultType,t_result);
|
||||
m_isRunning = false;
|
||||
return t_result;
|
||||
}
|
||||
t_Tesseract->End();
|
||||
@ -277,6 +295,7 @@ RecognitionResult TessOcrUtils::getRecogitionResult(Pix * image,ResultType resul
|
||||
t_result.errorCode = ErrorCode::OK;
|
||||
t_result.resultType = resultType;
|
||||
t_result.result = result;
|
||||
m_isRunning = false;
|
||||
return t_result;
|
||||
}
|
||||
|
||||
|
@ -166,6 +166,12 @@ public:
|
||||
*/
|
||||
RecognitionResult getRecogitionResult(QImage *image);
|
||||
|
||||
/**
|
||||
* @brief 是否正在识别中
|
||||
* @return true:正在识别中 false:识别结束
|
||||
*/
|
||||
bool isRunning();
|
||||
|
||||
private :
|
||||
|
||||
/**
|
||||
@ -242,9 +248,14 @@ private :
|
||||
*/
|
||||
QString m_sLangs;
|
||||
|
||||
/**
|
||||
* @brief 是否正在识别中
|
||||
*/
|
||||
bool m_isRunning;
|
||||
|
||||
static TessOcrUtils *m_tessOcrUtils;
|
||||
|
||||
// static tesseract::TessBaseAPI *t_Tesseract;
|
||||
static tesseract::TessBaseAPI *t_Tesseract;
|
||||
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user