Ocean
Barcode.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #pragma once
9 
11 
12 namespace Ocean
13 {
14 
15 namespace CV
16 {
17 
18 namespace Detector
19 {
20 
21 namespace Barcodes
22 {
23 
24 /// Forward declaration
25 class Barcode;
26 
27 /// Definition of a vector of barcodes
28 typedef std::vector<Barcode> Barcodes;
29 
30 /**
31  * Definition of barcode types
32  * @ingroup cvdetectorbarcodes
33  */
34 enum class BarcodeType : uint32_t
35 {
36  /// An invalid barcode type
37  INVALID = 0u,
38  /// A barcode of type EAN-13
39  EAN_13 = 1u << 0u,
40  /// A barcode of type UPC-A
41  UPC_A = 1u << 1u,
42 };
43 
44 /// Definition of a set of barcode types
45 typedef std::unordered_set<BarcodeType> BarcodeTypeSet;
46 
47 /**
48  * Definition of a barcode
49  * @ingroup cvdetectorbarcodes
50  */
51 class Barcode
52 {
53  public:
54 
55  /**
56  * Creates an invalid barcode instance
57  */
58  Barcode() = default;
59 
60  /**
61  * Creates a barcode object
62  * @param barcodeType The type of the barcode, must not be `BarcodeType::INVALID`
63  * @param data The data that will be stored for this barcode, must not be empty
64  */
65  inline Barcode(const BarcodeType barcodeType, const std::string& data);
66 
67  /**
68  * Creates a barcode object
69  * @param barcodeType The type of the barcode, must not be `BarcodeType::INVALID`
70  * @param data The data that will be stored for this barcode, must not be empty
71  */
72  inline Barcode(const BarcodeType barcodeType, std::string&& data);
73 
74  /**
75  * Returns whether this is a valid barcode instance
76  * @return True if this is a valid barcode instance, otherwise false
77  */
78  inline bool isValid() const;
79 
80  /**
81  * Returns the type of this barcode
82  * @return The type of this barcode
83  */
84  inline BarcodeType barcodeType() const;
85 
86  /**
87  * Returns the payload of this barcode
88  * @return The payload
89  */
90  inline const std::string& data() const;
91 
92  /**
93  * Translates a barcode type into a human-readable string
94  * @param barcodeType The barcode type that will be converted into a string
95  * @return The translated barcode type
96  */
97  static inline std::string translateBarcodeType(const BarcodeType barcodeType);
98 
99  protected:
100 
101  /// The type of this barcode
103 
104  /// The payload of barcode
105  std::string data_;
106 };
107 
108 inline Barcode::Barcode(const BarcodeType barcodeType, const std::string& data) :
109  barcodeType_(barcodeType),
110  data_(data)
111 {
112  ocean_assert(barcodeType_ != BarcodeType::INVALID);
113  ocean_assert(!data_.empty());
114 }
115 
116 inline Barcode::Barcode(const BarcodeType barcodeType, std::string&& data) :
117  barcodeType_(barcodeType),
118  data_(std::move(data))
119 {
120  ocean_assert(barcodeType_ != BarcodeType::INVALID);
121  ocean_assert(!data_.empty());
122 }
123 
124 inline bool Barcode::isValid() const
125 {
126  return barcodeType() != BarcodeType::INVALID;
127 }
128 
130 {
131  return barcodeType_;
132 }
133 
134 inline const std::string& Barcode::data() const
135 {
136  return data_;
137 }
138 
139 inline std::string Barcode::translateBarcodeType(const BarcodeType barcodeType)
140 {
141  switch (barcodeType)
142  {
144  return "INVALID";
145 
146  case BarcodeType::EAN_13:
147  return "EAN-13";
148 
149  case BarcodeType::UPC_A:
150  return "UPC-A";
151  }
152 
153  ocean_assert(false && "Never be here!");
154  return "Unknown barcode type";
155 }
156 
157 } // namespace QRCodes
158 
159 } // namespace Detector
160 
161 } // namespace CV
162 
163 } // namespace Ocean
Definition of a barcode.
Definition: Barcode.h:52
BarcodeType barcodeType_
The type of this barcode.
Definition: Barcode.h:102
const std::string & data() const
Returns the payload of this barcode.
Definition: Barcode.h:134
bool isValid() const
Returns whether this is a valid barcode instance.
Definition: Barcode.h:124
BarcodeType barcodeType() const
Returns the type of this barcode.
Definition: Barcode.h:129
static std::string translateBarcodeType(const BarcodeType barcodeType)
Translates a barcode type into a human-readable string.
Definition: Barcode.h:139
Barcode()=default
Creates an invalid barcode instance.
std::string data_
The payload of barcode.
Definition: Barcode.h:105
BarcodeType
Definition of barcode types.
Definition: Barcode.h:35
std::unordered_set< BarcodeType > BarcodeTypeSet
Definition of a set of barcode types.
Definition: Barcode.h:45
std::vector< Barcode > Barcodes
Definition of a vector of barcodes.
Definition: Barcode.h:25
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15