-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOcrHandler.cpp
63 lines (54 loc) · 1.82 KB
/
OcrHandler.cpp
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
#include "OcrHandler.h"
#include "Logger.h"
#include <QDebug>
#include <QSettings>
#include <QFile>
namespace {
const char* TESSERACT_NAMES[] = { "tesseract", "tesseract.exe", "C:/Program Files (x86)/Tesseract-OCR/tesseract.exe", NULL };
}
bool FindTesseract(QProcess &tesseractProcess) {
int index = 0;
bool foundTesseract = false;
while(TESSERACT_NAMES[index] != 0) {
QString command(TESSERACT_NAMES[index]);
tesseractProcess.setProgram(command);
tesseractProcess.start(command, QStringList("--version"));
tesseractProcess.waitForFinished(-1);
QByteArray procOutput = tesseractProcess.readAllStandardError();
Logger::Log(tesseractProcess, procOutput);
if(tesseractProcess.error() == QProcess::UnknownError && procOutput.size() > 0) {
foundTesseract = true;
qDebug() << "Found " << TESSERACT_NAMES[index];
break;
}
qDebug() << "Tesseract executable not found yet: " << TESSERACT_NAMES[index];
index++;
}
if(!foundTesseract) {
qWarning("No working Tesseract executable found");
}
return foundTesseract;
}
OcrHandler::OcrHandler()
{
IsTesseractReady = FindTesseract(TesseractProcess);
}
QString OcrHandler::AddTextToPDF(const QString &pdfInputName, const QString &pdfOutputName)
{
QSettings settings;
QStringList arguments;
arguments.append("-l");
arguments.append(settings.value("Ocr/language", "deu").toString());
arguments.append(pdfInputName);
arguments.append(pdfOutputName);
arguments.append("pdf");
TesseractProcess.setArguments(arguments);
TesseractProcess.start();
TesseractProcess.waitForFinished(-1);
Logger::Log(TesseractProcess);
return pdfOutputName + ".pdf";
}
bool OcrHandler::IsReady() const
{
return IsTesseractReady;
}