KItinerary

barcodedecoder.h
1/*
2 SPDX-FileCopyrightText: 2018-2019 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#pragma once
8
9#include "kitinerary_export.h"
10
11#include <QFlags>
12#include <QVariant>
13
14#include <unordered_map>
15
16class QByteArray;
17class QImage;
18class QString;
19
20namespace KItinerary {
21
22/** Barcode decoding with result caching.
23 * All non-static functions are using heuristics and cached results before actually
24 * performing an expensive barcode decoding operation, so repreated calls or calls with
25 * implausible arguments are cheap-ish.
26 *
27 * @note This is only functional if zxing is available.
28 * @internal Only exported for unit tests and KItinerary Workbench.
29 */
30class KITINERARY_EXPORT BarcodeDecoder
31{
32public:
35
37 Aztec = 1,
38 QRCode = 2,
39 PDF417 = 4,
40 DataMatrix = 8,
41 Code39 = 16,
42 Code93 = 32,
43 Code128 = 64,
44 IgnoreAspectRatio = 128, /// search for barcodes anywhere in the image, rather than assuming the image is primarily containing the barcode
45 AnySquare = Aztec | QRCode | DataMatrix,
46 Any2D = AnySquare | PDF417,
47 Any1D = Code39 | Code93 | Code128,
48 Any = Any1D | Any2D,
49 None = 0
50 };
51 Q_DECLARE_FLAGS(BarcodeTypes, BarcodeType)
52
53 /** Barcode decoding result.
54 * Can be a QByteArray, a QString or empty.
55 */
56 class KITINERARY_EXPORT Result {
57 public:
58 enum ContentType {
59 None = 0,
60 ByteArray = 1,
61 String = 2,
62 Any = 3
63 };
64 int contentType = None;
65 QVariant content;
66
67 QByteArray toByteArray() const;
68 QString toString() const;
69
70 ///@cond internal
71 BarcodeTypes positive = BarcodeDecoder::None;
72 BarcodeTypes negative = BarcodeDecoder::None;
73 ///@endcond
74 };
75
76 /** Decodes a barcode in @p img based on @p hint.
77 * @param hint has to be validated by something of the likes of maybeBarcode()
78 * before.
79 */
80 Result decode(const QImage &img, BarcodeTypes hint) const;
81
82 /** Decodes multiple barcodes in @p img based on @p hint.
83 * @param hint IgnoreAspectRatio is implied here
84 */
85 std::vector<Result> decodeMulti(const QImage &img, BarcodeTypes hint) const;
86
87 /** Decodes a binary payload barcode in @p img of type @p hint.
88 * @param hint has to be validated by something of the likes of maybeBarcode()
89 * before.
90 */
91 [[deprecated("use decode()")]] QByteArray decodeBinary(const QImage &img, BarcodeTypes hint) const;
92
93 /** Decodes a textual payload barcode in @p img of type @p hint.
94 * @param hint has to be validated by something of the likes of maybeBarcode()
95 * before.
96 */
97 [[deprecated("use decode()")]] QString decodeString(const QImage &img, BarcodeTypes hint) const;
98
99 /** Clears the internal cache. */
100 void clearCache();
101
102 /** Checks if the given image dimensions are plausible for a barcode.
103 * These checks are done first by BarcodeDecoder, it might however useful
104 * to perform them manually if a cheaper way to obtain the image dimension exists
105 * that does not require a full QImage creation.
106 */
107 static BarcodeTypes isPlausibleSize(int width, int height, BarcodeTypes hint);
108
109 /** Checks if the given image dimensions are a barcode of type @p hint.
110 * See above.
111 */
112 static BarcodeTypes isPlausibleAspectRatio(int width, int height, BarcodeTypes hint);
113
114 /** The combination of the above. */
115 static BarcodeTypes maybeBarcode(int width, int height, BarcodeTypes hint);
116
117private:
118 void decodeIfNeeded(const QImage &img, BarcodeTypes hint, Result &result) const;
119 void decodeMultiIfNeeded(const QImage &img, BarcodeTypes hint, std::vector<Result> &results) const;
120
121 mutable std::unordered_map<qint64, std::vector<Result>> m_cache;
122};
123
124Q_DECLARE_OPERATORS_FOR_FLAGS(BarcodeDecoder::BarcodeTypes)
125
126}
127
Barcode decoding with result caching.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:40:32 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.