Skip to content

Commit

Permalink
feat: QR Code support, with auto generate it from string
Browse files Browse the repository at this point in the history
  • Loading branch information
conde2 authored Sep 25, 2023
1 parent 2e8ac80 commit 1494437
Show file tree
Hide file tree
Showing 12 changed files with 1,456 additions and 2 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@
- Create a folder called "files" in your www folder and paste init.lua, modules, data, and exe files
- Uncomment and change this line (https://github.com/mehah/otclient/blob/main/init.lua#L6)
- Colored text [@conde2](https://github.com/conde2)
- ex: widget:setColoredText("{Colored text, #ff00ff} normal text")
- widget:setColoredText("{Colored text, #ff00ff} normal text")
- QR Code support, with auto generate it from string [@conde2]
- qr-code-border: 2
- qr-code: Hail OTClient Redemption - Conde2 Dev

##### [OTClient V8](https://github.com/OTCv8) (Features)
- Lighting System
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ set(SOURCE_FILES
framework/stdext/string.cpp
framework/stdext/time.cpp
framework/stdext/uri.cpp
framework/stdext/qrcodegen.cpp
framework/ui/uianchorlayout.cpp
framework/ui/uiboxlayout.cpp
framework/ui/uigridlayout.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 1494437

Please sign in to comment.