Warning, file /pim/kitinerary/src/lib/barcodedecoder.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2018-2019 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "kitinerary_export.h"
0010 
0011 #include <QFlags>
0012 #include <QVariant>
0013 
0014 #include <unordered_map>
0015 
0016 class QByteArray;
0017 class QImage;
0018 class QString;
0019 
0020 namespace KItinerary {
0021 
0022 /** Barcode decoding with result caching.
0023  *  All non-static functions are using heuristics and cached results before actually
0024  *  performing an expensive barcode decoding operation, so repreated calls or calls with
0025  *  implausible arguments are cheap-ish.
0026  *
0027  *  @note This is only functional if zxing is available.
0028  *  @internal Only exported for unit tests and KItinerary Workbench.
0029  */
0030 class KITINERARY_EXPORT BarcodeDecoder
0031 {
0032 public:
0033     BarcodeDecoder();
0034     ~BarcodeDecoder();
0035 
0036     enum BarcodeType {
0037         Aztec = 1,
0038         QRCode = 2,
0039         PDF417 = 4,
0040         DataMatrix = 8,
0041         Code39 = 16,
0042         Code93 = 32,
0043         Code128 = 64,
0044         IgnoreAspectRatio = 128, /// search for barcodes anywhere in the image, rather than assuming the image is primarily containing the barcode
0045         AnySquare = Aztec | QRCode | DataMatrix,
0046         Any2D = AnySquare | PDF417,
0047         Any1D = Code39 | Code93 | Code128,
0048         Any = Any1D | Any2D,
0049         None = 0
0050     };
0051     Q_DECLARE_FLAGS(BarcodeTypes, BarcodeType)
0052 
0053     /** Barcode decoding result.
0054      *  Can be a QByteArray, a QString or empty.
0055      */
0056     class KITINERARY_EXPORT Result {
0057     public:
0058         enum ContentType {
0059             None = 0,
0060             ByteArray = 1,
0061             String = 2,
0062             Any = 3
0063         };
0064         int contentType = None;
0065         QVariant content;
0066 
0067         QByteArray toByteArray() const;
0068         QString toString() const;
0069 
0070         ///@cond internal
0071         BarcodeTypes positive = BarcodeDecoder::None;
0072         BarcodeTypes negative = BarcodeDecoder::None;
0073         ///@endcond
0074     };
0075 
0076     /** Decodes a barcode in @p img based on @p hint.
0077      *  @param hint has to be validated by something of the likes of maybeBarcode()
0078      *  before.
0079      */
0080     Result decode(const QImage &img, BarcodeTypes hint) const;
0081 
0082     /** Decodes multiple barcodes in @p img based on @p hint.
0083      *  @param hint IgnoreAspectRatio is implied here
0084      */
0085     std::vector<Result> decodeMulti(const QImage &img, BarcodeTypes hint) const;
0086 
0087     /** Decodes a binary payload barcode in @p img of type @p hint.
0088      *  @param hint has to be validated by something of the likes of maybeBarcode()
0089      *  before.
0090      */
0091     [[deprecated("use decode()")]] QByteArray decodeBinary(const QImage &img, BarcodeTypes hint) const;
0092 
0093     /** Decodes a textual payload barcode in @p img of type @p hint.
0094      *  @param hint has to be validated by something of the likes of maybeBarcode()
0095      *  before.
0096      */
0097     [[deprecated("use decode()")]] QString decodeString(const QImage &img, BarcodeTypes hint) const;
0098 
0099     /** Clears the internal cache. */
0100     void clearCache();
0101 
0102     /** Checks if the given image dimensions are plausible for a barcode.
0103      *  These checks are done first by BarcodeDecoder, it might however useful
0104      *  to perform them manually if a cheaper way to obtain the image dimension exists
0105      *  that does not require a full QImage creation.
0106      */
0107     static BarcodeTypes isPlausibleSize(int width, int height, BarcodeTypes hint);
0108 
0109     /** Checks if the given image dimensions are a barcode of type @p hint.
0110      *  See above.
0111      */
0112     static BarcodeTypes isPlausibleAspectRatio(int width, int height, BarcodeTypes hint);
0113 
0114     /** The combination of the above. */
0115     static BarcodeTypes maybeBarcode(int width, int height, BarcodeTypes hint);
0116 
0117 private:
0118     void decodeIfNeeded(const QImage &img, BarcodeTypes hint, Result &result) const;
0119     void decodeMultiIfNeeded(const QImage &img, BarcodeTypes hint, std::vector<Result> &results) const;
0120 
0121     mutable std::unordered_map<qint64, std::vector<Result>> m_cache;
0122 };
0123 
0124 Q_DECLARE_OPERATORS_FOR_FLAGS(BarcodeDecoder::BarcodeTypes)
0125 
0126 }
0127