Skip to content

Commit

Permalink
#9 added the possibility to set formats that should be accepted by SB…
Browse files Browse the repository at this point in the history
…arcodeFilter
  • Loading branch information
kosadev committed Jan 20, 2022
1 parent a41f97e commit a8560db
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 24 deletions.
34 changes: 23 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ There are plenty of supported formats and we constantly work on adding new.

| Format | Supports scanning | Supports generating
| ---------------- | ------------------------ | -------------------
| UPC-A | <center></center> | <center>✔️</center>
| UPC-E | <center></center> | <center>✔️</center>
| EAN-8 | <center></center> | <center>✔️</center>
| EAN-13 | <center></center> | <center>✔️</center>
| DataBar | <center></center> | <center>❌</center>
| UPC-A | <center>✔️</center> | <center>✔️</center>
| UPC-E | <center>✔️</center> | <center>✔️</center>
| EAN-8 | <center>✔️</center> | <center>✔️</center>
| EAN-13 | <center>✔️</center> | <center>✔️</center>
| DataBar | <center>✔️</center> | <center>❌</center>
| DataBar Expanded | <center>❌</center> | <center>❌</center>
| Code 39 | <center></center> | <center>✔️</center>
| Code 93 | <center></center> | <center>✔️</center>
| Code 128 | <center></center> | <center>✔️</center>
| Code 39 | <center>✔️</center> | <center>✔️</center>
| Code 93 | <center>✔️</center> | <center>✔️</center>
| Code 128 | <center>✔️</center> | <center>✔️</center>
| Codabar | <center>✔️</center> | <center>✔️</center>
| ITF | <center>❌</center> | <center>✔️</center>

Expand All @@ -42,9 +42,9 @@ There are plenty of supported formats and we constantly work on adding new.
| Format | Supports scanning | Supports generating
| ------------ | ------------------------ | -------------------
| QR Code | <center>✔️</center> | <center>✔️</center>
| DataMatrix | <center></center> | <center>✔️</center>
| Aztec | <center></center> | <center>✔️</center>
| PDF417 | <center></center> | <center>✔️</center>
| DataMatrix | <center>✔️</center> | <center>✔️</center>
| Aztec | <center>✔️</center> | <center>✔️</center>
| PDF417 | <center>✔️</center> | <center>✔️</center>
| MaxiCode | <center>❌</center> | <center>❌</center>


Expand Down Expand Up @@ -88,6 +88,18 @@ import com.scythestudio.scodes 1.0

6. You are done. Get inspired by [QML Barcode Reader demo](https://github.com/scytheStudio/SCodes/blob/master/examples/QmlBarcodeReader/qml/ScannerPage.qml) to test wrapper.

### Trying various formats
`SBarcodeFilter` is a class that you need to use for scanning case. By default it scans only specific basic formats of code (Code 39, Code 93, Code 128, QR Code and DataMatrix.).
The goal of that is to limit number of possible formats and improve performance.

To specify formats that should be accepted by the `SBarcodeFilter` instance, you need to set it's `format` property accordingly. This property allows setting multiple enum values as it's for flags. Add the following to your `SBarcodeFilter` item in Qml code:
```qml
Component.onCompleted: {
barcodeFilter.format = SCodes.OneDCodes
}
```
See the enumeration values that represent supported formats in [SBarcodeFormat.h](https://github.com/scytheStudio/SCodes/blob/master/src/SBarcodeFormat.h)
To accept all supported formats use `SCodes.Any`.

## Note

Expand Down
5 changes: 5 additions & 0 deletions examples/QmlBarcodeReader/QmlBarcodeReader.pro
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
include("../../src/SCodes.pri")

QT += quick
CONFIG += c++17

DEFINES += QT_DEPRECATED_WARNINGS

CONFIG += qmltypes
QML_IMPORT_NAME = com.scythestudio.scodes
QML_IMPORT_MAJOR_VERSION = 1

SOURCES += \
main.cpp

Expand Down
7 changes: 2 additions & 5 deletions src/SBarcodeDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,12 @@ bool SBarcodeDecoder::isDecoding() const
return _isDecoding;
}

void SBarcodeDecoder::process(const QImage capturedImage)
void SBarcodeDecoder::process(const QImage capturedImage, ZXing::BarcodeFormats formats)
{
setIsDecoding(true);

const auto hints = DecodeHints()
.setFormats(BarcodeFormat::QR_CODE
| BarcodeFormat::DATA_MATRIX
| BarcodeFormat::CODE_128
| BarcodeFormat::CODABAR)
.setFormats(formats)
.setTryHarder(true)
.setTryRotate(true)
.setIsPure(false)
Expand Down
4 changes: 3 additions & 1 deletion src/SBarcodeDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <QObject>
#include <QVideoFrame>

#include "SBarcodeFormat.h"

class SBarcodeDecoder : public QObject
{
Q_OBJECT
Expand All @@ -20,7 +22,7 @@ class SBarcodeDecoder : public QObject
static QImage imageFromVideoFrame(const QVideoFrame &videoFrame);

public slots:
void process(const QImage capturedImage);
void process(const QImage capturedImage, ZXing::BarcodeFormats formats);

signals:
void isDecodingChanged(bool isDecoding);
Expand Down
25 changes: 21 additions & 4 deletions src/SBarcodeFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include "SBarcodeDecoder.h"

void processImage(SBarcodeDecoder *decoder, const QImage &image)
void processImage(SBarcodeDecoder *decoder, const QImage &image, ZXing::BarcodeFormats formats)
{
decoder->process(image);
decoder->process(image, formats);
};

class SBarcodeFilterRunnable : public QVideoFilterRunnable
Expand Down Expand Up @@ -36,8 +36,11 @@ class SBarcodeFilterRunnable : public QVideoFilterRunnable
return *input;
}

const QImage croppedCapturedImage = SBarcodeDecoder::videoFrameToImage(*input, _filter->captureRect().toRect());
_filter->getImageFuture() = QtConcurrent::run(processImage, _filter->getDecoder(), croppedCapturedImage);
const QImage croppedCapturedImage =
SBarcodeDecoder::videoFrameToImage(*input,_filter->captureRect().toRect());
_filter->getImageFuture() =
QtConcurrent::run(processImage, _filter->getDecoder(), croppedCapturedImage, SCodes::toZXingFormat(_filter->format()));

return *input;
}

Expand Down Expand Up @@ -109,3 +112,17 @@ QFuture<void> SBarcodeFilter::getImageFuture() const
{
return _imageFuture;
}

const SCodes::SBarcodeFormats &SBarcodeFilter::format() const
{
return m_format;
}

void SBarcodeFilter::setFormat(const SCodes::SBarcodeFormats &format)
{
qDebug() << "set format " << format << ", old format " << m_format;
if (m_format != format) {
m_format = format;
emit formatChanged(m_format);
}
}
9 changes: 8 additions & 1 deletion src/SBarcodeFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
#include <QtConcurrent/QtConcurrent>
#include <qqml.h>

#include "./SBarcodeDecoder.h"
#include "SBarcodeDecoder.h"
#include "SBarcodeFormat.h"

class SBarcodeFilter : public QAbstractVideoFilter
{
Q_OBJECT
Q_PROPERTY(QString captured READ captured NOTIFY capturedChanged)
Q_PROPERTY(QRectF captureRect READ captureRect WRITE setCaptureRect NOTIFY captureRectChanged)
Q_PROPERTY(SCodes::SBarcodeFormats format READ format WRITE setFormat NOTIFY formatChanged)
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
QML_ELEMENT
#endif
Expand All @@ -29,9 +31,13 @@ class SBarcodeFilter : public QAbstractVideoFilter

QVideoFilterRunnable * createFilterRunnable() override;

const SCodes::SBarcodeFormats &format() const;
void setFormat(const SCodes::SBarcodeFormats &format);

signals:
void capturedChanged(const QString &captured);
void captureRectChanged(const QRectF &captureRect);
void formatChanged(const SCodes::SBarcodeFormats &format);

private slots:
void setCaptured(const QString &captured);
Expand All @@ -43,6 +49,7 @@ private slots:

SBarcodeDecoder *_decoder;
QFuture<void> _imageFuture;
SCodes::SBarcodeFormats m_format = SCodes::SBarcodeFormat::Basic;
};

#endif // QRSCANNERFILTER_H
19 changes: 19 additions & 0 deletions src/SBarcodeFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,22 @@ SCodes::SBarcodeFormat SCodes::fromString(const QString &formatName)

return SCodes::SBarcodeFormat::None;
}

ZXing::BarcodeFormats SCodes::toZXingFormat(SBarcodeFormats formats)
{
ZXing::BarcodeFormats zXingformats;

int formatEnumIndex = SCodes::staticMetaObject.indexOfEnumerator("SBarcodeFormat");
QMetaEnum sBarcodeFormatEnumMeta = SCodes::staticMetaObject.enumerator(formatEnumIndex);

for (int i = 0; i < sBarcodeFormatEnumMeta.keyCount(); ++i) {
const auto key = sBarcodeFormatEnumMeta.key(i);
const auto value = sBarcodeFormatEnumMeta.keyToValue(key);
const auto enumValue = static_cast<SCodes::SBarcodeFormat>(value);
if (formats.testFlag(enumValue)) {
zXingformats |= toZXingFormat(enumValue);
}
}

return zXingformats;
}
5 changes: 3 additions & 2 deletions src/SBarcodeFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace SCodes {
Q_NAMESPACE
QML_ELEMENT

enum class SBarcodeFormat {
enum class SBarcodeFormat : int {
None = 0,
Aztec = (1 << 0),
Codabar = (1 << 1),
Expand All @@ -30,14 +30,15 @@ namespace SCodes {
OneDCodes = Codabar | Code39 | Code93 | Code128 | EAN8 | EAN13 | ITF | DataBar | DataBarExpanded | UPCA | UPCE,
TwoDCodes = Aztec | DataMatrix | MaxiCode | PDF417 | QRCode,
Any = OneDCodes | TwoDCodes,
Basic = Code128 | QRCode,
Basic = Code39 | Code93 | Code128 | QRCode | DataMatrix,
};

Q_DECLARE_FLAGS(SBarcodeFormats, SBarcodeFormat)
Q_DECLARE_OPERATORS_FOR_FLAGS(SBarcodeFormats)
Q_FLAG_NS(SBarcodeFormats)

ZXing::BarcodeFormat toZXingFormat(SBarcodeFormat format);
ZXing::BarcodeFormats toZXingFormat(SBarcodeFormats formats);

QString toString(SBarcodeFormat format);
SBarcodeFormat fromString(const QString &formatName);
Expand Down

0 comments on commit a8560db

Please sign in to comment.