-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZOcrProcess.cpp
More file actions
75 lines (63 loc) · 1.83 KB
/
ZOcrProcess.cpp
File metadata and controls
75 lines (63 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "ZOcrProcess.h"
#include "QFuture"
#include "qdebug.h"
#include <QtConcurrent/QtConcurrent>
#include <tesseract/baseapi.h>
#include <tesseract/ocrclass.h>
#include <leptonica/allheaders.h>
#include <qwidget>
#include <QPoint>
bool ProgressCallBack(int progress, int left, int right, int top, int bottom)
{
qDebug()<<"OCR progress...";
qDebug()<< QString::number(progress);
return true;
}
ZOcrProcess::ZOcrProcess(QObject* parent): QObject(parent)
{
}
void ZOcrProcess::OCRThtread(QString imagePath)
{
auto api = new tesseract::TessBaseAPI();
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api->Init(m_trainDataPath.toStdString().c_str(), m_language.toStdString().c_str())) {
qDebug() << "tesseract Init failed";
assert(false);
}
// Open input image with leptonica library
Pix* image = pixRead(imagePath.toStdString().c_str());
api->SetImage(image);
ETEXT_DESC monitor;
monitor.progress_callback = ProgressCallBack;
api->Recognize(&monitor);
char* outText = api->GetUTF8Text();
qDebug() << "OCR finish:";
qDebug() << outText;
m_results = QString(outText);
emit finish();
// Destroy used object and release memory
api->End();
pixDestroy(&image);
}
void ZOcrProcess::setTrainDataPath(const QString path)
{
m_trainDataPath = path;
}
QString ZOcrProcess::getResults()
{
return m_results;
}
void ZOcrProcess::grabScreen(QQuickWindow* window, int x, int y,int width,int height)
{
auto pix = window->screen()->grabWindow(0,x,y,width,height);
pix.save("C:\\Users\\17305\\Pictures\\ff.png");
return;
}
void ZOcrProcess::startOCR(const QString imgPath)
{
auto res = QtConcurrent::run(&ZOcrProcess::OCRThtread,this, imgPath);
}
void ZOcrProcess::setLanguage(const QString language)
{
m_language = language;
}