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  /** Checks if @p img contains a barcode of type @p hint. */
54  bool isBarcode(const QImage &img, BarcodeTypes hint = Any2D) const;
55 
56  /** Decodes a binary payload barcode in @p img of type @p hint. */
57  QByteArray decodeBinary(const QImage &img, BarcodeTypes hint = Any2D) const;
58 
59  /** Decodes a textual payload barcode in @p img of type @p hint. */
60  QString decodeString(const QImage &img, BarcodeTypes hint = Any2D) const;
61 
62  /** Clears the internal cache. */
63  void clearCache();
64 
65  /** Checks if the given image dimensions are plausible for a barcode.
66  * These checks are done first by BarcodeDecoder, it might however useful
67  * to perform them manually if a cheaper way to obtain the image dimension exists
68  * that does not require a full QImage creation.
69  */
70  static bool isPlausibleSize(int width, int height, BarcodeTypes hint);
71 
72  /** Checks if the given image dimensions are a barcode of type @p hint.
73  * See above.
74  */
75  static bool isPlausibleAspectRatio(int width, int height, BarcodeTypes hint);
76 
77  /** The combination of the above. */
78  static bool maybeBarcode(int width, int height, BarcodeTypes hint);
79 
80 private:
81  struct Result {
82  BarcodeTypes positive = BarcodeDecoder::None;
83  BarcodeTypes negative = BarcodeDecoder::None;
84  enum ContentType {
85  None = 0,
86  ByteArray = 1,
87  String = 2,
88  Any = 3
89  };
90  int contentType = None;
91  QVariant content;
92  };
93 
94  void decodeIfNeeded(const QImage &img, BarcodeTypes hint, Result &result) const;
95  void decodeZxing(const QImage &img, BarcodeDecoder::BarcodeTypes format, BarcodeDecoder::Result &result) const;
96 
97  mutable std::unordered_map<qint64, Result> m_cache;
98 };
99 
100 }
101 
102 Q_DECLARE_OPERATORS_FOR_FLAGS(KItinerary::BarcodeDecoder::BarcodeTypes)
103 
Barcode decoding with result caching.
Classes for reservation/travel data models, data extraction and data augmentation.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Oct 19 2021 23:17:08 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.