summaryrefslogtreecommitdiffhomepage
path: root/qrcode.cpp
blob: da747a5bff7d1be26c2a1dc78aa031776ccba4f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "qrcode.h"

#include <fmt/format.h>

#include <qrcodegen/QrCode.hpp>

#include <Magick++.h>

using namespace qrcodegen;
using namespace Magick;

void QRCode::init()
{
 Magick::InitializeMagick(NULL);
}

std::string QRCode::getQRCode(const std::string& data)
{
 QrCode qrc {QrCode::encodeText(data.c_str(), QrCode::Ecc::MEDIUM)};

 int size {qrc.getSize()};

 Image image(fmt::format("{0}x{0}", size).c_str(), "white");
 image.type(GrayscaleType);

 for (int x = 0; x < size; x++) {
  for (int y = 0; y < size; y++) {
   image.pixelColor(x, y, qrc.getModule(x, y) ? "black" : "white");
  }
 }

 image.magick("PNG");

 Blob blob;
 image.write(&blob);
 return std::string{(char*)blob.data(), blob.length()};
}