summaryrefslogtreecommitdiffhomepage
path: root/qrcode.cpp
blob: 5e45afc5d59c62b9f5fed839491207d8f2d9e6c3 (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
#include "qrcode.h"

#include <fmt/format.h>

#include <qrcodegen/QrCode.hpp>

#include <ImageMagick-6/Magick++.h>

using namespace qrcodegen;
using namespace Magick;

std::string 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);
 //image.size(fmt::format("{0}x{0}", size));

 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()};
}