Skip to content

Commit 334a866

Browse files
author
Damir Porobic
committed
Allow providing data path when calling plugin
1 parent 85b10d4 commit 334a866

File tree

7 files changed

+28
-16
lines changed

7 files changed

+28
-16
lines changed

example/IPluginOcrOther.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class IPluginOcrOther
2727
public:
2828
virtual ~IPluginOcrOther() = default;
2929
virtual QString recognize(const QPixmap &pixmap) const = 0;
30+
virtual QString recognize(const QPixmap &pixmap, const QString &dataPath) const = 0;
3031
virtual QString version() const = 0;
3132
};
3233

example/main.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ int main(int argc, char **argv)
6060

6161
textLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
6262

63-
QDir pluginsDir(QLatin1String("../src/"));
63+
QDir pluginsDir(QLatin1String("../src"));
6464

6565
#if defined(_WIN32)
6666
// Under Windows the 3rd Party Dlls are next to the plugin in the same directory
@@ -82,7 +82,12 @@ int main(int argc, char **argv)
8282
auto pluginOcr = qobject_cast<IPluginOcrOther*>(plugin);
8383
if (pluginOcr) {
8484
qDebug() << "Loaded plugin " << fileName << " with version " << pluginOcr->version();
85+
86+
#if defined(_WIN32)
87+
auto text = pluginOcr->recognize(pixmap, pluginsDir.path() + QString("\\tessdata"));
88+
#else
8589
auto text = pluginOcr->recognize(pixmap);
90+
#endif
8691
textLabel->setText(text);
8792
} else {
8893
qCritical() << "Unable to cast to interface";

src/IPluginOcr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class IPluginOcr
2727
public:
2828
virtual ~IPluginOcr() = default;
2929
virtual QString recognize(const QPixmap &pixmap) const = 0;
30+
virtual QString recognize(const QPixmap &pixmap, const QString &dataPath) const = 0;
3031
virtual QString version() const = 0;
3132
};
3233

src/OcrWrapper.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,17 @@ OcrWrapper::~OcrWrapper()
3131

3232
QString OcrWrapper::recognize(const QPixmap &pixmap) const
3333
{
34-
if (mTessApi->Init(getDataPath(), "eng") == 0)
34+
return recognizeInner(pixmap, nullptr);
35+
}
36+
37+
QString OcrWrapper::recognize(const QPixmap &pixmap, const QString &dataPath) const
38+
{
39+
return recognizeInner(pixmap, dataPath.toLocal8Bit().data());
40+
}
41+
42+
QString OcrWrapper::recognizeInner(const QPixmap &pixmap, char * dataPath) const
43+
{
44+
if (mTessApi->Init(dataPath, "eng") == 0)
3545
{
3646
auto pix = makePixFromPixmap(pixmap);
3747
mTessApi->SetImage(pix);
@@ -57,16 +67,3 @@ PIX* OcrWrapper::makePixFromPixmap(const QPixmap &pixmap)
5767

5868
return pixReadMemBmp((l_uint8 *)byteArray.constData(), byteArray.size());
5969
}
60-
61-
const char *OcrWrapper::getDataPath()
62-
{
63-
#if defined(_WIN32)
64-
// Under Windows we expect the trained data to be next to the plugin
65-
// in as directory called tessdata
66-
return ".\\tessdata\\";
67-
#else
68-
// Under Linux/Unix/MacOS we expect the TESSDATA_PREFIX to point to the
69-
// trained data.
70-
return nullptr;
71-
#endif
72-
}

src/OcrWrapper.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ class OcrWrapper
3535
~OcrWrapper();
3636

3737
QString recognize(const QPixmap &pixmap) const;
38+
QString recognize(const QPixmap &pixmap, const QString &dataPath) const;
3839

3940
private:
4041
QSharedPointer<TessBaseAPI> mTessApi;
4142

43+
QString recognizeInner(const QPixmap &pixmap, char * dataPath) const;
44+
4245
static PIX* makePixFromPixmap(const QPixmap &pixmap);
43-
static const char *getDataPath();
4446
};
4547

4648
#endif //PLUGIN_OCR_OCRWRAPPER_H

src/PluginOcr.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ QString PluginOcr::recognize(const QPixmap &pixmap) const
2424
return mOcrWrapper.recognize(pixmap);
2525
}
2626

27+
QString PluginOcr::recognize(const QPixmap &pixmap, const QString &dataPath) const
28+
{
29+
return mOcrWrapper.recognize(pixmap, dataPath);
30+
}
31+
2732
QString PluginOcr::version() const
2833
{
2934
return QLatin1String(PROJECT_VERSION);

src/PluginOcr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Q_OBJECT
3737
public:
3838
~PluginOcr() override = default;
3939
QString recognize(const QPixmap &pixmap) const override;
40+
QString recognize(const QPixmap &pixmap, const QString &dataPath) const override;
4041
QString version() const override;
4142

4243
private:

0 commit comments

Comments
 (0)