Skip to content

Commit befe744

Browse files
committed
Qt: improve example code
* fix moc warning * fix timeout logic in QML code * add TextMode, tryInvert and isPure properties
1 parent 99a83b3 commit befe744

File tree

4 files changed

+45
-14
lines changed

4 files changed

+45
-14
lines changed

example/ZXingQt5CamReader.qml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Window {
2222

2323
Timer {
2424
id: resetInfo
25-
interval: 2000
25+
interval: 1000
2626
}
2727

2828
BarcodeReader {
@@ -31,7 +31,9 @@ Window {
3131
formats: (linearSwitch.checked ? (ZXing.LinearCodes) : ZXing.None) | (matrixSwitch.checked ? (ZXing.MatrixCodes) : ZXing.None)
3232
tryRotate: tryRotateSwitch.checked
3333
tryHarder: tryHarderSwitch.checked
34+
tryInvert: tryInvertSwitch.checked
3435
tryDownscale: tryDownscaleSwitch.checked
36+
textMode: ZXing.HRI
3537

3638
// callback with parameter 'result', called for every successfully processed frame
3739
// onFoundBarcode: {}
@@ -42,11 +44,13 @@ Window {
4244
? [result.position.topLeft, result.position.topRight, result.position.bottomRight, result.position.bottomLeft]
4345
: nullPoints
4446

45-
if (result.isValid)
47+
if (result.isValid) {
4648
resetInfo.restart()
49+
info.text = qsTr("Format: \t %1 \nText: \t %2 \nType: \t %3 \nTime: \t %4 ms").arg(result.formatName).arg(result.text).arg(result.contentTypeName).arg(result.runTime)
50+
}
4751

48-
if (result.isValid || !resetInfo.running)
49-
info.text = qsTr("Format: \t %1 \nText: \t %2 \nError: \t %3 \nTime: \t %4 ms").arg(result.formatName).arg(result.text).arg(result.status).arg(result.runTime)
52+
if (!resetInfo.running)
53+
info.text = "No barcode found"
5054

5155
// console.log(result)
5256
}
@@ -138,6 +142,7 @@ Window {
138142

139143
Switch {id: tryRotateSwitch; text: qsTr("Try Rotate"); checked: true }
140144
Switch {id: tryHarderSwitch; text: qsTr("Try Harder"); checked: true }
145+
Switch {id: tryInvertSwitch; text: qsTr("Try Invert"); checked: true }
141146
Switch {id: tryDownscaleSwitch; text: qsTr("Try Downscale"); checked: true }
142147
Switch {id: linearSwitch; text: qsTr("Linear Codes"); checked: true }
143148
Switch {id: matrixSwitch; text: qsTr("Matrix Codes"); checked: true }

example/ZXingQt6CamReader.qml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Window {
2222

2323
Timer {
2424
id: resetInfo
25-
interval: 2000
25+
interval: 1000
2626
}
2727

2828
BarcodeReader {
@@ -32,7 +32,9 @@ Window {
3232
formats: (linearSwitch.checked ? (ZXing.LinearCodes) : ZXing.None) | (matrixSwitch.checked ? (ZXing.MatrixCodes) : ZXing.None)
3333
tryRotate: tryRotateSwitch.checked
3434
tryHarder: tryHarderSwitch.checked
35+
tryInvert: tryInvertSwitch.checked
3536
tryDownscale: tryDownscaleSwitch.checked
37+
textMode: ZXing.TextMode.HRI
3638

3739
// callback with parameter 'result', called for every successfully processed frame
3840
// onFoundBarcode: {}
@@ -43,11 +45,13 @@ Window {
4345
? [result.position.topLeft, result.position.topRight, result.position.bottomRight, result.position.bottomLeft]
4446
: nullPoints
4547

46-
if (result.isValid)
48+
if (result.isValid) {
4749
resetInfo.restart()
50+
info.text = qsTr("Format: \t %1 \nText: \t %2 \nType: \t %3 \nTime: \t %4 ms").arg(result.formatName).arg(result.text).arg(result.contentTypeName).arg(result.runTime)
51+
}
4852

49-
if (result.isValid || !resetInfo.running)
50-
info.text = qsTr("Format: \t %1 \nText: \t %2 \nError: \t %3 \nTime: \t %4 ms").arg(result.formatName).arg(result.text).arg(result.status).arg(result.runTime)
53+
if (!resetInfo.running)
54+
info.text = "No barcode found"
5155

5256
// console.log(result)
5357
}
@@ -132,7 +136,7 @@ Window {
132136
Shape {
133137
id: polygon
134138
anchors.fill: parent
135-
visible: control.points.length === 4
139+
visible: points.length === 4
136140

137141
ShapePath {
138142
strokeWidth: 3
@@ -175,6 +179,7 @@ Window {
175179

176180
Switch {id: tryRotateSwitch; text: qsTr("Try Rotate"); checked: true }
177181
Switch {id: tryHarderSwitch; text: qsTr("Try Harder"); checked: true }
182+
Switch {id: tryInvertSwitch; text: qsTr("Try Invert"); checked: true }
178183
Switch {id: tryDownscaleSwitch; text: qsTr("Try Downscale"); checked: true }
179184
Switch {id: linearSwitch; text: qsTr("Linear Codes"); checked: true }
180185
Switch {id: matrixSwitch; text: qsTr("Matrix Codes"); checked: true }

example/ZXingQtReader.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@ int main(int argc, char* argv[])
1818

1919
QString filePath = argv[1];
2020

21-
QImage fileImage = QImage(filePath);
21+
QImage image = QImage(filePath);
2222

23-
if (fileImage.isNull()) {
23+
if (image.isNull()) {
2424
qDebug() << "Could not load the filename as an image:" << filePath;
2525
return 1;
2626
}
2727

2828
auto options = ReaderOptions()
29-
.setFormats(BarcodeFormat::Any)
29+
.setFormats(BarcodeFormat::MatrixCodes)
3030
.setTryRotate(false)
31+
.setTextMode(TextMode::HRI)
3132
.setMaxNumberOfSymbols(10);
3233

33-
auto results = ReadBarcodes(fileImage, options);
34+
auto results = ReadBarcodes(image, options);
3435

3536
for (auto& result : results) {
3637
qDebug() << "Text: " << result.text();

example/ZXingQtReader.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,12 @@ enum class BarcodeFormat
5959

6060
enum class ContentType { Text, Binary, Mixed, GS1, ISO15434, UnknownECI };
6161

62+
enum class TextMode { Plain, ECI, HRI, Hex, Escaped };
63+
6264
#else
6365
using ZXing::BarcodeFormat;
6466
using ZXing::ContentType;
67+
using ZXing::TextMode;
6568
#endif
6669

6770
using ZXing::ReaderOptions;
@@ -70,6 +73,7 @@ using ZXing::BarcodeFormats;
7073

7174
Q_ENUM_NS(BarcodeFormat)
7275
Q_ENUM_NS(ContentType)
76+
Q_ENUM_NS(TextMode)
7377

7478
template<typename T, typename = decltype(ZXing::ToString(T()))>
7579
QDebug operator<<(QDebug dbg, const T& v)
@@ -102,6 +106,7 @@ class Result : private ZXing::Result
102106
Q_PROPERTY(QByteArray bytes READ bytes)
103107
Q_PROPERTY(bool isValid READ isValid)
104108
Q_PROPERTY(ContentType contentType READ contentType)
109+
Q_PROPERTY(QString contentTypeName READ contentTypeName)
105110
Q_PROPERTY(Position position READ position)
106111

107112
QString _text;
@@ -124,6 +129,7 @@ class Result : private ZXing::Result
124129
BarcodeFormat format() const { return static_cast<BarcodeFormat>(ZXing::Result::format()); }
125130
ContentType contentType() const { return static_cast<ContentType>(ZXing::Result::contentType()); }
126131
QString formatName() const { return QString::fromStdString(ZXing::ToString(ZXing::Result::format())); }
132+
QString contentTypeName() const { return QString::fromStdString(ZXing::ToString(ZXing::Result::contentType())); }
127133
const QString& text() const { return _text; }
128134
const QByteArray& bytes() const { return _bytes; }
129135
const Position& position() const { return _position; }
@@ -347,9 +353,22 @@ class BarcodeReader : public QObject, private ReaderOptions
347353
}
348354
Q_SIGNAL void formatsChanged();
349355

356+
Q_PROPERTY(TextMode textMode READ textMode WRITE setTextMode NOTIFY textModeChanged)
357+
TextMode textMode() const noexcept { return static_cast<TextMode>(ReaderOptions::textMode()); }
358+
Q_SLOT void setTextMode(TextMode newVal)
359+
{
360+
if (textMode() != newVal) {
361+
ReaderOptions::setTextMode(static_cast<ZXing::TextMode>(newVal));
362+
emit textModeChanged();
363+
}
364+
}
365+
Q_SIGNAL void textModeChanged();
366+
350367
ZQ_PROPERTY(bool, tryRotate, setTryRotate)
351368
ZQ_PROPERTY(bool, tryHarder, setTryHarder)
369+
ZQ_PROPERTY(bool, tryInvert, setTryInvert)
352370
ZQ_PROPERTY(bool, tryDownscale, setTryDownscale)
371+
ZQ_PROPERTY(bool, isPure, setIsPure)
353372

354373
public slots:
355374
ZXingQt::Result process(const QVideoFrame& image)
@@ -389,7 +408,7 @@ public slots:
389408
_sink = sink;
390409
connect(_sink, &QVideoSink::videoFrameChanged, this, &BarcodeReader::process);
391410
}
392-
Q_PROPERTY(QVideoSink* videoSink WRITE setVideoSink)
411+
Q_PROPERTY(QVideoSink* videoSink MEMBER _sink WRITE setVideoSink)
393412
#endif
394413

395414
};
@@ -435,6 +454,7 @@ inline void registerQmlAndMetaTypes()
435454
{
436455
qRegisterMetaType<ZXingQt::BarcodeFormat>("BarcodeFormat");
437456
qRegisterMetaType<ZXingQt::ContentType>("ContentType");
457+
qRegisterMetaType<ZXingQt::TextMode>("TextMode");
438458

439459
// supposedly the Q_DECLARE_METATYPE should be used with the overload without a custom name
440460
// but then the qml side complains about "unregistered type"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy