Prison

scanresult.cpp
1/*
2 SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: MIT
4*/
5
6#include "config-prison-scanner.h"
7
8#include "format_p.h"
9#include "scanresult.h"
10#include "scanresult_p.h"
11
12#define ZX_USE_UTF8 1
13#include <ZXing/TextUtfEncoding.h>
14
15using namespace Prison;
16
17ScanResult::ScanResult()
18 : d(new ScanResultPrivate)
19{
20}
21
22ScanResult::ScanResult(const ScanResult &) = default;
23ScanResult::~ScanResult() = default;
24ScanResult &ScanResult::operator=(const ScanResult &) = default;
25
26bool ScanResult::operator==(const ScanResult &other) const
27{
28 return d->content == other.d->content && d->boundingRect == other.d->boundingRect && d->format == other.d->format;
29}
30
31bool ScanResult::hasContent() const
32{
33 return !d->content.isNull();
34}
35
36QVariant ScanResult::content() const
37{
38 return d->content;
39}
40
41bool ScanResult::hasText() const
42{
43 return d->content.userType() == QMetaType::QString;
44}
45
46QString ScanResult::text() const
47{
48 return hasText() ? d->content.toString() : QString();
49}
50
51bool ScanResult::hasBinaryData() const
52{
53 return d->content.userType() == QMetaType::QByteArray;
54}
55
56QByteArray ScanResult::binaryData() const
57{
58 return hasBinaryData() ? d->content.toByteArray() : QByteArray();
59}
60
61Format::BarcodeFormat ScanResult::format() const
62{
63 return d->format;
64}
65
66QRect ScanResult::boundingRect() const
67{
68 return d->boundingRect;
69}
70
71ScanResult ScanResultPrivate::fromZXingResult(const ZXing::Result &zxRes, const QTransform &transform)
72{
73 ScanResult res;
74 if (!zxRes.isValid()) {
75 return res;
76 }
77
78#if ZXING_VERSION < QT_VERSION_CHECK(1, 4, 0)
79 // distinguish between binary and text content
80 const auto hasWideChars = std::any_of(zxRes.text().begin(), zxRes.text().end(), [](auto c) {
81 return c > 255;
82 });
83 const auto hasControlChars = std::any_of(zxRes.text().begin(), zxRes.text().end(), [](auto c) {
84 return c < 0x20 && c != 0x0a && c != 0x0d;
85 });
86 if (hasWideChars || !hasControlChars) {
87 res.d->content = QString::fromStdString(ZXing::TextUtfEncoding::ToUtf8(zxRes.text()));
88 } else {
89 QByteArray b;
90 b.resize(zxRes.text().size());
91 std::copy(zxRes.text().begin(), zxRes.text().end(), b.begin());
92 res.d->content = b;
93 }
94#else
95 if (zxRes.contentType() == ZXing::ContentType::Text) {
96 res.d->content = QString::fromStdString(zxRes.text());
97 } else {
98 QByteArray b;
99 b.resize(zxRes.bytes().size());
100 std::copy(zxRes.bytes().begin(), zxRes.bytes().end(), b.begin());
101 res.d->content = b;
102 }
103#endif
104
105 // determine the bounding rect
106 // the cooridinates we get from ZXing are a polygon, we need to determine the
107 // bounding rect manually from its coordinates
108 const auto p = zxRes.position();
109 int x1 = std::numeric_limits<int>::max();
110 int y1 = std::numeric_limits<int>::max();
111 int x2 = std::numeric_limits<int>::min();
112 int y2 = std::numeric_limits<int>::min();
113 for (int i = 0; i < 4; ++i) {
114 x1 = std::min(x1, p[i].x);
115 y1 = std::min(y1, p[i].y);
116 x2 = std::max(x2, p[i].x);
117 y2 = std::max(y2, p[i].y);
118 }
119 res.d->boundingRect = QRect(QPoint(x1, y1), QPoint(x2, y2));
120
121 // apply frame transformations to the bounding rect
122 res.d->boundingRect = transform.mapRect(res.d->boundingRect);
123 res.d->format = Format::toFormat(zxRes.format());
124 return res;
125}
126
127#include "moc_scanresult.cpp"
Result of a barcode scan attempt.
Definition scanresult.h:31
KDOCTOOLS_EXPORT QString transform(const QString &file, const QString &stylesheet, const QList< const char * > &params=QList< const char * >())
BarcodeFormat
Barcode formats.
Definition format.h:27
Provides classes and methods for generating barcodes.
Definition barcode.h:24
iterator begin()
void resize(qsizetype newSize, char c)
QString fromStdString(const std::string &str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:55:54 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.