Skip to content

Commit

Permalink
QR Code support, with auto generate it from string
Browse files Browse the repository at this point in the history
  • Loading branch information
conde2 committed Sep 25, 2023
1 parent 2e8ac80 commit 1d7197d
Show file tree
Hide file tree
Showing 11 changed files with 1,452 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ set(SOURCE_FILES
framework/ui/uiwidgettext.cpp
framework/util/color.cpp
framework/util/crypt.cpp
framework/stdext/qrcodegen.cpp

protobuf/appearances.pb.cc
main.cpp
Expand Down
28 changes: 27 additions & 1 deletion src/framework/graphics/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#include <framework/graphics/apngloader.h>

#include "framework/stdext/math.h"
#include "framework/stdext/qrcodegen.h"

using namespace qrcodegen;

Image::Image(const Size& size, int bpp, uint8_t* pixels) : m_size(size), m_bpp(bpp)
{
Expand Down Expand Up @@ -242,4 +245,27 @@ void Image::reverseChannels()
for (uint8_t* itr = pixelData; itr < pixelData + m_pixels.size(); itr += m_bpp) {
std::swap(*(itr + 0), *(itr + 2));
}
}
}

ImagePtr Image::fromQRCode(const std::string& code, int border)
{
try
{
QrCode qrCode = QrCode::encodeText(code.c_str(), QrCode::Ecc::MEDIUM);

const auto size = qrCode.getSize();
ImagePtr image(new Image(Size(size + border * 2, size + border * 2)));

for (int x = 0; x < size + border * 2; ++x) {
for (int y = 0; y < size + border * 2; ++y) {
image->setPixel(x, y, qrCode.getModule(x - border, y - border) ? Color::black : Color::white);
}
}

return image;
} catch (const std::exception& e) {
g_logger.error(stdext::format("Failed to encode qr-code: '%s': %s", code, e.what()));
}

return {};
}
1 change: 1 addition & 0 deletions src/framework/graphics/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Image

static ImagePtr load(const std::string& file);
static ImagePtr loadPNG(const std::string& file);
static ImagePtr fromQRCode(const std::string& code, int border);

void savePNG(const std::string& fileName);

Expand Down
1 change: 1 addition & 0 deletions src/framework/luafunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<UIWidget>("getFont", &UIWidget::getFont);
g_lua.bindClassMemberFunction<UIWidget>("getTextSize", &UIWidget::getTextSize);
g_lua.bindClassMemberFunction<UIWidget>("hasShader", &UIWidget::hasShader);
g_lua.bindClassMemberFunction<UIWidget>("setQRCode", &UIWidget::setQRCode);

// UILayout
g_lua.registerClass<UILayout>();
Expand Down
Loading

0 comments on commit 1d7197d

Please sign in to comment.