Skip to content

Commit

Permalink
Merge pull request #48 from mateusz-fejcher/IP-568-SCodes-improvements
Browse files Browse the repository at this point in the history
SCodes improvements
  • Loading branch information
pa-sowa authored Jul 8, 2024
2 parents ee4ce36 + 2876b5f commit c6c7f68
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 13 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ We are a group of Qt and C++ enthusiasts whose goal is to address growing demand

The company offers broad spectrum of services for the clients who wish to bring their ideas to life. We have extensive and practical knowledge about various Qt modules and other technologies allowing to create high quality product in a cost effective approach. If you want to see what Scythe Studio is is capable of and what services we provide, check out [this link](https://scythe-studio.com/en/services).

## Professional Support
We offer professional support for those who need assistance with integrating SCodes library or require additional features. For more information on our support options and pricing, please contact us at [email protected]

## Follow us

Check out those links if you want to see Scythe Studio in action and follow the newest trends saying about Qt Qml development.
Expand Down
3 changes: 2 additions & 1 deletion examples/QmlBarcodeGenerator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ if(ANDROID)

else()

add_executable(${PROJECT_NAME} main.cpp ${RSCS})
add_executable(${PROJECT_NAME} main.cpp ${RSCS}
ColorController.h ColorController.cpp)

endif()

Expand Down
15 changes: 15 additions & 0 deletions examples/QmlBarcodeGenerator/ColorController.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "ColorController.h"

ColorController::ColorController(QObject *parent)
: QObject{parent}
{}

bool ColorController::checkColor(const QString &color)
{
return QColor(color).isValid();
}

QColor ColorController::convertStringToColor(const QString &color)
{
return QColor(color);
}
18 changes: 18 additions & 0 deletions examples/QmlBarcodeGenerator/ColorController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef COLORCONTROLLER_H
#define COLORCONTROLLER_H

#include <QObject>
#include <QColor>

class ColorController : public QObject
{
Q_OBJECT
public:
explicit ColorController(QObject *parent = nullptr);
Q_INVOKABLE static QColor convertStringToColor(const QString &color);

public slots:
bool checkColor(const QString &color);
};

#endif // COLORCONTROLLER_H
2 changes: 2 additions & 0 deletions examples/QmlBarcodeGenerator/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <QQmlApplicationEngine>

#include "SBarcodeGenerator.h"
#include "ColorController.h"

int main(int argc, char *argv[])
{
Expand All @@ -17,6 +18,7 @@ int main(int argc, char *argv[])
qmlRegisterSingletonType(QUrl("qrc:/qml/Theme.qml"), "Theme", 1, 0, "Theme");

qmlRegisterType<SBarcodeGenerator>("com.scythestudio.scodes", 1, 0, "SBarcodeGenerator");
qmlRegisterType<ColorController>("com.scythestudio.scodes", 1, 0, "ColorController");

engine.load(QUrl(QStringLiteral("qrc:/qml/GeneratorPage.qml")));

Expand Down
4 changes: 3 additions & 1 deletion examples/QmlBarcodeGenerator/qml/CTextField.qml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import QtQuick.Controls 2.12
TextField {
id: root

property bool inputIsValid: true

selectByMouse: true

leftPadding: 5
Expand All @@ -16,7 +18,7 @@ TextField {
radius: 2

border {
color: Theme.borderColor
color: inputIsValid ? Theme.borderColor : Theme.invalidInputBorderColor
width: 1
}
}
Expand Down
48 changes: 48 additions & 0 deletions examples/QmlBarcodeGenerator/qml/GeneratorPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,23 @@ ApplicationWindow {
}
}

ColorController {
id: colorController
}

SBarcodeGenerator {
id: barcodeGenerator

onForegroundColorChanged: {
image.source = ""
barcodeGenerator.generate(textField.text)
}

onBackgroundColorChanged: {
image.source = ""
barcodeGenerator.generate(textField.text)
}

onGenerationFinished: function (error) {
if (error === "") {
console.log(barcodeGenerator.filePath)
Expand Down Expand Up @@ -280,6 +294,40 @@ ApplicationWindow {
}
}

CTextField {
id: foregroundColorField

implicitWidth: parent.width
implicitHeight: parent.height / 10

placeholderText: "Current foreground color: " + barcodeGenerator.foregroundColor

onTextChanged: function () {
foregroundColorField.inputIsValid = colorController.checkColor(foregroundColorField.text)

if (colorController.checkColor(foregroundColorField.text)) {
barcodeGenerator.setForegroundColor(colorController.convertStringToColor(foregroundColorField.text))
}
}
}

CTextField {
id: backgroundColorField

implicitWidth: parent.width
implicitHeight: parent.height / 10

placeholderText: "Current background color: " + barcodeGenerator.backgroundColor

onTextChanged: function () {
backgroundColorField.inputIsValid = colorController.checkColor(backgroundColorField.text)

if (colorController.checkColor(backgroundColorField.text)) {
barcodeGenerator.setBackgroundColor(colorController.convertStringToColor(backgroundColorField.text))
}
}
}

CComboBox {
id: formatDropDown

Expand Down
1 change: 1 addition & 0 deletions examples/QmlBarcodeGenerator/qml/Theme.qml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ QtObject {
readonly property color backgroundColor: "#218165"
readonly property color textColor: "#bdbdbd"
readonly property color borderColor: "#333"
readonly property color invalidInputBorderColor: "#F00"
}
2 changes: 1 addition & 1 deletion src/SBarcodeDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <ReadBarcode.h>
#include <exception>
#include <QScopeGuard>
#include "debug.h"
#include "private/debug.h"

/*!
* \brief Provide a interface to access `ZXing::ReadBarcode` method
Expand Down
2 changes: 1 addition & 1 deletion src/SBarcodeFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <QVideoFilterRunnable>

#include "SBarcodeDecoder.h"
#include "debug.h"
#include "private/debug.h"

void processImage(SBarcodeDecoder *decoder, const QImage &image, ZXing::BarcodeFormats formats)
{
Expand Down
51 changes: 45 additions & 6 deletions src/SBarcodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,23 @@ bool SBarcodeGenerator::generate(const QString &inputString)
}
}

ZXing::MultiFormatWriter writer = ZXing::MultiFormatWriter(SCodes::toZXingFormat(m_format)).setMargin(
m_margin).setEccLevel(m_eccLevel);
ZXing::MultiFormatWriter writer = ZXing::MultiFormatWriter(SCodes::toZXingFormat(m_format))
.setMargin(m_margin)
.setEccLevel(m_eccLevel);

auto qrCodeMatrix = writer.encode(inputString.toStdString(), m_width, m_height);
_bitmap = ZXing::ToMatrix<uint8_t>(qrCodeMatrix);

QImage image(_bitmap.data(), m_width, m_height, QImage::Format_Grayscale8);
QImage image(m_width, m_height, QImage::Format_ARGB32);

for (int y = 0; y < m_height; ++y) {
for (int x = 0; x < m_width; ++x) {
if (qrCodeMatrix.get(x, y)) {
image.setPixelColor(x, y, m_foregroundColor);
} else {
image.setPixelColor(x, y, m_backgroundColor);
}
}
}

// Center images works only on QR codes.
if (m_format == SCodes::SBarcodeFormat::QRCode) {
Expand All @@ -55,8 +65,7 @@ bool SBarcodeGenerator::generate(const QString &inputString)

m_filePath = QDir::tempPath() + "/" + m_fileName + "." + m_extension;

// Save the final image with the QR code and center image
QFile file(m_filePath);
QFile file{m_filePath};
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
image.save(&file);
} else {
Expand Down Expand Up @@ -211,3 +220,33 @@ void SBarcodeGenerator::setCenterImageRatio(int centerImageRatio)
m_centerImageRatio = centerImageRatio;
emit centerImageRatioChanged();
}

QColor SBarcodeGenerator::foregroundColor() const
{
return m_foregroundColor;
}

void SBarcodeGenerator::setForegroundColor(const QColor &foregroundColor)
{
if (m_foregroundColor == foregroundColor) {
return;
}

m_foregroundColor = foregroundColor;
emit foregroundColorChanged();
}

QColor SBarcodeGenerator::backgroundColor() const
{
return m_backgroundColor;
}

void SBarcodeGenerator::setBackgroundColor(const QColor &backgroundColor)
{
if (m_backgroundColor == backgroundColor) {
return;
}

m_backgroundColor = backgroundColor;
emit backgroundColorChanged();
}
46 changes: 44 additions & 2 deletions src/SBarcodeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class SBarcodeGenerator : public QQuickItem
Q_PROPERTY(SCodes::SBarcodeFormat format READ format WRITE setFormat NOTIFY formatChanged)
Q_PROPERTY(QString imagePath READ imagePath WRITE setImagePath NOTIFY imagePathChanged)
Q_PROPERTY(int centerImageRatio READ centerImageRatio WRITE setCenterImageRatio NOTIFY centerImageRatioChanged)
Q_PROPERTY(QColor foregroundColor READ foregroundColor WRITE setForegroundColor NOTIFY foregroundColorChanged)
Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged)

public:

Expand Down Expand Up @@ -83,6 +85,19 @@ class SBarcodeGenerator : public QQuickItem
* \param const int &centerImageRatio - new image ratio.
*/
void setCenterImageRatio(int centerImageRatio);

/*!
* \fn QColor foregroundColor() const
* \brief Returns foregroundColor.
*/
QColor foregroundColor() const;


/*!
* \fn QColor backgroundColor() const
* \brief Returns backgroundColor.
*/
QColor backgroundColor() const;

public slots:

Expand All @@ -106,6 +121,20 @@ public slots:
*/
bool saveImage();

/*!
* \fn void setForegroundColor(const QColor &newForegroundColor)
* \brief Sets the foreground.
* \param const QColor &foreground - foreground color.
*/
void setForegroundColor(const QColor &foregroundColor);

/*!
* \fn void setBackgroundColor(const QColor &backgroundColor)
* \brief Sets the backgroundColor.
* \param const QColor &backgroundColor - background color.
*/
void setBackgroundColor(const QColor &backgroundColor);

signals:

/*!
Expand Down Expand Up @@ -160,6 +189,18 @@ public slots:
*/
void centerImageRatioChanged();

/*!
* \brief This signal is emitted to send foregroundColor to QML.
* \param const QColor &foregroundColor - foregroundColor.
*/
void foregroundColorChanged();

/*!
* \brief This signal is emitted to send backgroundColor to QML.
* \param const QColor &backgroundColor - backgroundColor.
*/
void backgroundColorChanged();

private:
int m_width = 500;
int m_height = 500;
Expand All @@ -177,8 +218,6 @@ public slots:

SCodes::SBarcodeFormat m_format = SCodes::SBarcodeFormat::Code128;

ZXing::Matrix<uint8_t> _bitmap = ZXing::Matrix<uint8_t>();

/*!
* \brief This method draws Rectangle and `imageRatio` smaller Image in the center of that Rectangle.
* \param QImage *parentImage - Image parent. It is used for Painter constructor.
Expand All @@ -195,6 +234,9 @@ public slots:
* \param int eccLevel - QR code ecc level.
*/
void setEccLvel(int eccLevel);

QColor m_foregroundColor = "black";
QColor m_backgroundColor = "white";
};

#endif // SBARCODEGENERATOR_H
2 changes: 1 addition & 1 deletion src/SBarcodeScanner.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "SBarcodeScanner.h"
#include <QMediaDevices>
#include "debug.h"
#include "private/debug.h"
SBarcodeScanner::SBarcodeScanner(QObject* parent)
: QVideoSink(parent)
, m_camera(nullptr)
Expand Down

0 comments on commit c6c7f68

Please sign in to comment.