KItinerary

barcodedecoder.h
1 /*
2  SPDX-FileCopyrightText: 2018-2019 Volker Krause <[email protected]>
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 
16 class QByteArray;
17 class QImage;
18 class QString;
19 
20 namespace 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  */
30 class KITINERARY_EXPORT BarcodeDecoder
31 {
32 public:
34  ~BarcodeDecoder();
35 
36  enum BarcodeType {
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  /** Decodes a binary payload barcode in @p img of type @p hint.
54  * @param hint has to be validated by something of the likes of maybeBarcode()
55  * before.
56  */
57  QByteArray decodeBinary(const QImage &img, BarcodeTypes hint) const;
58 
59  /** Decodes a textual payload barcode in @p img of type @p hint.
60  * @param hint has to be validated by something of the likes of maybeBarcode()
61  * before.
62  */
63  QString decodeString(const QImage &img, BarcodeTypes hint) const;
64 
65  /** Clears the internal cache. */
66  void clearCache();
67 
68  /** Checks if the given image dimensions are plausible for a barcode.
69  * These checks are done first by BarcodeDecoder, it might however useful
70  * to perform them manually if a cheaper way to obtain the image dimension exists
71  * that does not require a full QImage creation.
72  */
73  static BarcodeTypes isPlausibleSize(int width, int height, BarcodeTypes hint);
74 
75  /** Checks if the given image dimensions are a barcode of type @p hint.
76  * See above.
77  */
78  static BarcodeTypes isPlausibleAspectRatio(int width, int height, BarcodeTypes hint);
79 
80  /** The combination of the above. */
81  static BarcodeTypes maybeBarcode(int width, int height, BarcodeTypes hint);
82 
83 private:
84  struct Result {
85  BarcodeTypes positive = BarcodeDecoder::None;
86  BarcodeTypes negative = BarcodeDecoder::None;
87  enum ContentType {
88  None = 0,
89  ByteArray = 1,
90  String = 2,
91  Any = 3
92  };
93  int contentType = None;
94  QVariant content;
95  };
96 
97  void decodeIfNeeded(const QImage &img, BarcodeTypes hint, Result &result) const;
98  void decodeZxing(const QImage &img, BarcodeDecoder::BarcodeTypes format, BarcodeDecoder::Result &result) const;
99 
100  mutable std::unordered_map<qint64, Result> m_cache;
101 };
102 
103 Q_DECLARE_OPERATORS_FOR_FLAGS(BarcodeDecoder::BarcodeTypes)
104 
105 }
106 
Barcode decoding with result caching.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Tue Sep 27 2022 03:59:28 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.