-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqzxingnu.cpp
121 lines (104 loc) · 3.31 KB
/
qzxingnu.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "qzxingnu.h"
#include "qzxingnufilter.h"
#include <QCoreApplication>
#include <QtQml/QQmlEngine>
#include <zxing-cpp/core/src/BarcodeFormat.h>
#include <zxing-cpp/core/src/DecodeHints.h>
#include <zxing-cpp/core/src/GenericLuminanceSource.h>
#include <zxing-cpp/core/src/HybridBinarizer.h>
#include <zxing-cpp/core/src/MultiFormatReader.h>
#include <zxing-cpp/core/src/Result.h>
namespace QZXingNu {
using ZXingFormats = std::vector<ZXing::BarcodeFormat>;
using ZXing::DecodeHints;
using ZXing::GenericLuminanceSource;
using ZXing::HybridBinarizer;
using ZXing::MultiFormatReader;
using ZXing::Result;
static QVector<QPointF> toQVectorOfQPoints(const std::vector<ZXing::ResultPoint> &points)
{
QVector<QPointF> result;
for (const auto &point : points) {
result.append(QPointF(point.x(), point.y()));
}
return result;
}
static QZXingNuDecodeResult toQZXingNuDecodeResult(const ZXing::Result &result)
{
return { static_cast<DecodeStatus>(result.status()), static_cast<BarcodeFormat>(result.format()),
QString::fromStdWString(result.text()), QByteArray((const char *)result.rawBytes().data(), result.rawBytes().size()),
toQVectorOfQPoints(result.resultPoints()), result.isValid() };
}
static ZXingFormats zxingFormats(const QVector<int> &from)
{
ZXingFormats result;
result.reserve(static_cast<ZXingFormats::size_type>(from.size()));
std::transform(from.begin(), from.end(), std::back_inserter(result), [](int a) { return static_cast<ZXing::BarcodeFormat>(a); });
return result;
}
QZXingNu::QZXingNu(QObject *parent)
: QObject(parent)
{
connect(this, &QZXingNu::queueDecodeResult, this, &QZXingNu::setDecodeResult);
}
QVector<int> QZXingNu::formats() const
{
return m_formats;
}
bool QZXingNu::tryHarder() const
{
return m_tryHarder;
}
bool QZXingNu::tryRotate() const
{
return m_tryRotate;
}
QZXingNuDecodeResult QZXingNu::decodeResult() const
{
return m_decodeResult;
}
QZXingNuDecodeResult QZXingNu::decodeImage(const QImage &image)
{
// reentrant
auto luminanceSource = std::make_shared<GenericLuminanceSource>(image.width(), image.height(), image.bits(), image.bytesPerLine());
DecodeHints hints;
auto convertFormats = [this]() { return zxingFormats(m_formats); };
hints.setPossibleFormats(convertFormats());
hints.setTryHarder(m_tryHarder);
hints.setTryRotate(m_tryRotate);
MultiFormatReader reader(hints);
auto result = reader.read(HybridBinarizer(luminanceSource));
if (result.isValid()) {
auto qzxingResult = toQZXingNuDecodeResult(result);
emit queueDecodeResult(qzxingResult);
return qzxingResult;
}
return {};
}
void QZXingNu::setFormats(QVector<int> formats)
{
if (m_formats == formats)
return;
m_formats = formats;
emit formatsChanged(m_formats);
}
void QZXingNu::setTryHarder(bool tryHarder)
{
if (m_tryHarder == tryHarder)
return;
m_tryHarder = tryHarder;
emit tryHarderChanged(m_tryHarder);
}
void QZXingNu::setTryRotate(bool tryRotate)
{
if (m_tryRotate == tryRotate)
return;
m_tryRotate = tryRotate;
emit tryRotateChanged(m_tryRotate);
}
void QZXingNu::setDecodeResult(QZXingNuDecodeResult decodeResult)
{
m_decodeResult = decodeResult;
emit decodeResultChanged(m_decodeResult);
}
} // namespace QZXingNu