summaryrefslogtreecommitdiffstats
path: root/windows/zxing-cpp-test.cpp
diff options
context:
space:
mode:
authorAnton <anton@picapica.im>2026-12-02 03:52:50 +0400
committerAnton <anton@picapica.im>2026-12-02 03:52:50 +0400
commitc3298beb0779a8cc09b6d4ea0d2132c0612ea776 (patch)
tree910842506249d59a05cae4a72229b61049fcab19 /windows/zxing-cpp-test.cpp
parent56863d74399c8d7354a00bc8ddc87b924ef870ab (diff)
downloadpica-pica-qr_codes.tar.gz
pica-pica-qr_codes.zip
pica-client: show and scan contact ID as a QR codeqr_codes
Diffstat (limited to 'windows/zxing-cpp-test.cpp')
-rw-r--r--windows/zxing-cpp-test.cpp49
1 files changed, 49 insertions, 0 deletions
diff --git a/windows/zxing-cpp-test.cpp b/windows/zxing-cpp-test.cpp
new file mode 100644
index 0000000..cf03c67
--- /dev/null
+++ b/windows/zxing-cpp-test.cpp
@@ -0,0 +1,49 @@
+/*
+ * This file is part of MXE. See LICENSE.md for licensing information.
+ */
+
+// Round trip: encode a QR code with the writer, render it to a grayscale
+// image and read it back with the reader.
+
+#include <ZXing/BitMatrix.h>
+#include <ZXing/MultiFormatWriter.h>
+#include <ZXing/ReadBarcode.h>
+
+#include <iostream>
+#include <string>
+#include <vector>
+
+int main()
+{
+ const std::string text = "MXE zxing-cpp test";
+ const int scale = 4;
+ const int quiet = 4;
+
+ ZXing::BitMatrix matrix =
+ ZXing::MultiFormatWriter(ZXing::BarcodeFormat::QRCode).setMargin(0).encode(text, 0, 0);
+
+ const int size = (matrix.width() + 2 * quiet) * scale;
+ std::vector<unsigned char> pixels(size * size, 0xff);
+
+ for (int y = 0; y < matrix.height(); y++)
+ for (int x = 0; x < matrix.width(); x++)
+ if (matrix.get(x, y))
+ for (int dy = 0; dy < scale; dy++)
+ for (int dx = 0; dx < scale; dx++)
+ pixels[((y + quiet) * scale + dy) * size + (x + quiet) * scale + dx] = 0;
+
+ ZXing::ImageView image(pixels.data(), size, size, ZXing::ImageFormat::Lum);
+ ZXing::ReaderOptions options;
+ options.setFormats(ZXing::BarcodeFormat::QRCode);
+
+ const ZXing::Result result = ZXing::ReadBarcode(image, options);
+
+ if (!result.isValid() || result.text() != text)
+ {
+ std::cerr << "decoded \"" << result.text() << "\" instead of \"" << text << "\"" << std::endl;
+ return 1;
+ }
+
+ std::cout << result.text() << std::endl;
+ return 0;
+}