-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocumenthandler.cpp
More file actions
49 lines (37 loc) · 1.13 KB
/
documenthandler.cpp
File metadata and controls
49 lines (37 loc) · 1.13 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
#include "documenthandler.h"
#include <tesseract/baseapi.h>
#include <QImage>
#include <QSettings>
#include <QSqlError>
#include <QSqlQuery>
Q_LOGGING_CATEGORY(EDIM_DOCUMENTHANDLER, "edim.DocumentHandler")
QStringList DocumentHandler::supportedFileTypes()
{
QStringList result = {
"*.jpg",
"*.png"
};
return result;
}
DocumentHandler::DocumentHandler()
{
}
QString DocumentHandler::text(const QFileInfo& document) const
{
// tesseract crashes with LC_NUMERIC != C
std::string currentLocale(setlocale(LC_NUMERIC, nullptr));
setlocale(LC_NUMERIC, "C");
tesseract::TessBaseAPI* ocr = new tesseract::TessBaseAPI();
// TODO read proper language from settings
if (ocr->Init(nullptr, "deu")) {
qCWarning(EDIM_DOCUMENTHANDLER) << "Could not initialize OCR library.";
}
QImage image(document.absoluteFilePath());
ocr->SetImage(image.bits(), image.width(), image.height(), image.depth() / 8, image.bytesPerLine());
QString result(ocr->GetUTF8Text());
// Restore locale
setlocale(LC_NUMERIC, currentLocale.c_str());
// Clean up
ocr->End();
return result;
}