Prison

zxingutil.cpp
1/*
2 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: MIT
4*/
5
6#include "zxingutil_p.h"
7
8#include <QImage>
9#include <QVariant>
10
11#include <ZXing/BitMatrix.h>
12
13using namespace Prison;
14
15std::wstring ZXingUtil::toStdWString(const QVariant &data)
16{
17 if (data.userType() == QMetaType::QString) {
18 return data.toString().toStdWString();
19 }
20 if (data.userType() == QMetaType::QByteArray) {
21 const auto b = data.toByteArray();
22 std::wstring ws;
23 ws.reserve(b.size());
24 // ensure we explicitly copy unsigned bytes here, ie. each byte ends up in the least significant byte of
25 // the std::wstring. If we end up converting to a signed value inbetween, values > 127 end up negative and
26 // will be wrongly represented in the std::wstring
27 for (uint8_t c : b) {
28 ws.push_back(c);
29 }
30 return ws;
31 }
32
33 return {};
34}
35
36QImage ZXingUtil::toImage(const ZXing::BitMatrix &matrix, const QColor &foreground, const QColor &background)
37{
38 QImage image(matrix.width(), matrix.height(), QImage::Format_ARGB32);
39 for (int y = 0; y < matrix.height(); ++y) {
40 for (int x = 0; x < matrix.width(); ++x) {
41 image.setPixel(x, y, matrix.get(x, y) ? foreground.rgb() : background.rgb());
42 }
43 }
44 return image;
45}
Provides classes and methods for generating barcodes.
Definition barcode.h:24
QRgb rgb() const const
std::wstring toStdWString() const const
QTextStream & ws(QTextStream &stream)
QByteArray toByteArray() const const
QString toString() const const
int userType() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:12:17 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.