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

#include <fmt/format.h>

#include <qrcodegen/qrcodegen.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()};
}