CVB++ 14.0
reader.hpp
1#pragma once
2
3#include "reader_config.hpp"
4
5#include "../image.hpp"
6#include "cvb/rect.hpp"
7
8#include <typeindex>
9#include <typeinfo>
10
11#include "_detail/read_result.hpp"
12#include "_detail/read_result_1d.hpp"
13#include "_detail/read_result_code128.hpp"
14#include "_detail/read_result_code39_code93.hpp"
15#include "_detail/read_result_data_matrix.hpp"
16#include "_detail/read_result_pdf417.hpp"
17#include "_detail/read_result_qr.hpp"
18#include "_detail/read_result_sony_code.hpp"
19#include "_detail/read_result_upc_e.hpp"
20
21namespace Cvb
22{
23 CVB_BEGIN_INLINE_NS
24
25 namespace Barcode
26 {
27
29
31 class Reader
32 {
33 public:
34
35
36
38
46 template<class T>
47 static std::shared_ptr<T> Decode(ReaderConfigPtr configMap, const ImagePlane& imagePlane, Rect<int> aoi = Rect<int>())
48 {
49 static_assert(std::is_base_of<ReadResult, T>::value, "CVB: ReadResult must be a base of T");
50 auto baseResult = Decode(configMap, imagePlane, aoi);
51 if (!baseResult)
52 return std::shared_ptr<T>();
53
54 auto resultT = std::dynamic_pointer_cast<T>(baseResult);
55 if (!resultT)
56 throw std::runtime_error("ReadResult cannot be casted");
57
58 return resultT;
59 }
60
62
70 static ReadResultPtr Decode(ReaderConfigPtr configMap, const ImagePlane& imagePlane, Rect<int> aoi = Rect<int>())
71 {
72 Cvb::CExports::cvbrect_t cRect =
73 {
74 static_cast<long>(aoi.Left()),
75 static_cast<long>(aoi.Top()),
76 static_cast<long>(aoi.Right()),
77 static_cast<long>(aoi.Bottom())
78 };
79 Cvb::CExports::cvbrect_t* pCRect = (aoi != Rect<int>()) ? &cRect : nullptr;
80
81 configMap->Flush();
82
83 Cvb::String barcodeText;
84 auto resultInfoH = Internal::DoResCallValueOut<CExports::CVC_BC_INFO>([&](CExports::CVC_BC_INFO& resultInfo_)
85 {
86 std::vector<char> textBuff(MaxNumCodeChar_);
87
88 auto retVal = CVB_CALL_CAPI(CvcBcDecodeBarcode(
89 imagePlane.Parent().Handle(),
90 static_cast<long>(imagePlane.Plane()),
91 pCRect,
92 static_cast<Cvb::CExports::CVC_BC_DIRECTION>(configMap->ReadoutMode()),
93 reinterpret_cast<intptr_t>(configMap->Handle()),
94 textBuff.data(),
95 static_cast<short>(MaxNumCodeChar_),
96 &resultInfo_,
97 static_cast<short>(sizeof(CExports::CVC_BC_INFO))));
98 // string conversion
99 std::string stdString(textBuff.data());
100 barcodeText = Cvb::String(stdString.begin(), stdString.end());
101
102 return retVal;
103 });
104
105 // Get Ctor of specific Result type
106 auto resultSymbology = static_cast<Symbology>(resultInfoH.type);
107 auto ResultConstructor = ConfigResultMapping.at(resultSymbology);
108 auto pReadResult = ResultConstructor(resultInfoH);
109 pReadResult->text_ = barcodeText;
110
111 // Add grading info to readResult
112 if ((resultSymbology == Symbology::DataMatrix) && configMap->ContainsSymbology(Symbology::DataMatrixGrading))
113 std::dynamic_pointer_cast<ReadResultDataMatrix>(pReadResult)->gradeResult_ = std::make_shared<GradeResultDataMatrix>(resultInfoH);
114 else if((resultSymbology == Symbology::Qr) && configMap->ContainsSymbology(Symbology::QrGrading))
115 std::dynamic_pointer_cast<ReadResultQr>(pReadResult)->gradeResult_ = std::make_shared<GradeResultQr>(resultInfoH);
116 else if (IsGradeable1DCode(resultSymbology) && configMap->ContainsSymbology(Symbology::BarcodeGrading))
117 std::dynamic_pointer_cast<ReadResult1D>(pReadResult)->gradeResult_ = std::make_shared<GradeResult1D>(resultInfoH);
118
119 return pReadResult;
120 }
121
122
124
133 {
134 if (!configMap->ContainsSymbology(Symbology::DataMatrix))
135 throw std::runtime_error("Requires a DataMatrix configuration");
136
137 Cvb::CExports::cvbrect_t cRect =
138 {
139 static_cast<long>(aoi.Left()),
140 static_cast<long>(aoi.Top()),
141 static_cast<long>(aoi.Right()),
142 static_cast<long>(aoi.Bottom())
143 };
144 Cvb::CExports::cvbrect_t* pCRect = (aoi != Rect<int>()) ? &cRect : nullptr;
145
146 configMap->Flush();
147
148 Cvb::String barcodeText;
149 auto resultInfoH = Internal::DoResCallValueOut<CExports::CVC_BC_INFO>([&](CExports::CVC_BC_INFO& resultInfo_)
150 {
151 std::vector<char> textBuff(MaxNumCodeChar_);
152
153 auto retVal = CVB_CALL_CAPI(CvcBcTeachDottedDataMatrix(
154 imagePlane.Parent().Handle(),
155 static_cast<long>(imagePlane.Plane()),
156 pCRect,
157 static_cast<Cvb::CExports::CVC_BC_DIRECTION>(configMap->ReadoutMode()),
158 reinterpret_cast<intptr_t>(configMap->Handle()),
159 textBuff.data(),
160 static_cast<short>(MaxNumCodeChar_),
161 &resultInfo_,
162 static_cast<short>(sizeof(CExports::CVC_BC_INFO))));
163 // string conversion
164 std::string stdString(textBuff.data());
165 barcodeText = Cvb::String(stdString.begin(), stdString.end());
166
167 return retVal;
168 });
169
170 auto dataMatrixResult = std::make_shared<ReadResultDataMatrix>(resultInfoH);
171 dataMatrixResult->text_ = barcodeText;
172 return dataMatrixResult;
173 }
174
175 private:
176 //Reader();
177 static const size_t MaxNumCodeChar_ = 7090;
178
179 static const std::map<Cvb::Barcode::Symbology, std::function<ReadResultPtr(Cvb::CExports::CVC_BC_INFO&)> > ConfigResultMapping;
180
181 private:
182 static bool IsGradeable1DCode(Symbology sym)
183 {
184 switch (sym)
185 {
188 case Symbology::Ean13:
189 case Symbology::Ean8:
190 case Symbology::UpcA:
191 case Symbology::UpcE:
199 case Symbology::RSS:
202 return true;
203 default:
204 return false;
205 }
206 }
207 };
208 }
209
210 CVB_END_INLINE_NS
211}
212
213
214const std::map<Cvb::Barcode::Symbology, std::function<Cvb::Barcode::ReadResultPtr(Cvb::CExports::CVC_BC_INFO&)> > Cvb::Barcode::Reader::ConfigResultMapping =
215{
216 { Cvb::Barcode::Symbology::Codabar, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResult1D>(handle); } },
217 { Cvb::Barcode::Symbology::Code11, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResult1D>(handle); } },
218 { Cvb::Barcode::Symbology::Code32, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResult>(handle); } },
219 { Cvb::Barcode::Symbology::Discrete2of5, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResult1D>(handle); } },
220 { Cvb::Barcode::Symbology::Ean8, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResult1D>(handle); } },
221 { Cvb::Barcode::Symbology::Ean13, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResult1D>(handle); } },
222 { Cvb::Barcode::Symbology::FourStateAustralian, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResult>(handle); } },
223 { Cvb::Barcode::Symbology::FourStateKix, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResult>(handle); } },
224 { Cvb::Barcode::Symbology::FourStateRoyalMail, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResult>(handle); } },
225 { Cvb::Barcode::Symbology::FourStateUsps, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResult>(handle); } },
226 { Cvb::Barcode::Symbology::Interleaved2of5, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResult1D>(handle); } },
227 { Cvb::Barcode::Symbology::MicroPdf417, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResult>(handle); } },
228 { Cvb::Barcode::Symbology::MsiPlessey, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResult1D>(handle); } },
229 { Cvb::Barcode::Symbology::PharmaCode, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResult>(handle); } },
230 { Cvb::Barcode::Symbology::Planet, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResult>(handle); } },
231 { Cvb::Barcode::Symbology::Postnet, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResult>(handle); } },
232 { Cvb::Barcode::Symbology::RSS, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResult1D>(handle); } },
233 { Cvb::Barcode::Symbology::UpcA, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResult1D>(handle); } },
234 { Cvb::Barcode::Symbology::UpcE, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResultUpcE>(handle); } },
235 { Cvb::Barcode::Symbology::Pdf417, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResultPdf417>(handle); } },
236 { Cvb::Barcode::Symbology::DataMatrix, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResultDataMatrix>(handle); } },
237 { Cvb::Barcode::Symbology::PharmaCode2D, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResultDataMatrix>(handle); } },
238 { Cvb::Barcode::Symbology::Qr, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResultQr>(handle); } },
239 { Cvb::Barcode::Symbology::SonyCode, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResultSonyCode>(handle); } },
240 { Cvb::Barcode::Symbology::Code128, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResultCode128>(handle); } },
241 { Cvb::Barcode::Symbology::Code39, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResultCode39Code93>(handle); } },
242 { Cvb::Barcode::Symbology::Code93, [](Cvb::CExports::CVC_BC_INFO& handle) { return std::make_shared<Cvb::Barcode::ReadResultCode39Code93>(handle); } }
243};
The key class for reading barcodes.
Definition: reader.hpp:32
static ReadResultPtr Decode(ReaderConfigPtr configMap, const ImagePlane &imagePlane, Rect< int > aoi=Rect< int >())
This function initiates the decoding of barcodes.
Definition: reader.hpp:70
static std::shared_ptr< T > Decode(ReaderConfigPtr configMap, const ImagePlane &imagePlane, Rect< int > aoi=Rect< int >())
This function initiates the decoding of barcodes and dynamically casts the result.
Definition: reader.hpp:47
static ReadResultDataMatrixPtr TeachDottedDataMatrix(ReaderConfigPtr configMap, const ImagePlane &imagePlane, Rect< int > aoi=Rect< int >())
Teach a dotted data matrix code into a configuration.
Definition: reader.hpp:132
void * Handle() const noexcept
Classic API image handle.
Definition: decl_image.hpp:223
Image plane information container.
Definition: decl_image_plane.hpp:33
int Plane() const noexcept
Plane index in the image, to which this plane refers to.
Definition: decl_image_plane.hpp:169
const Image & Parent() const noexcept
Image to which this plane descriptor refers to.
Definition: detail_image_plane.hpp:87
Symbology
The symbologies supported by Barcode.
Definition: barcode.hpp:97
@ Code11
Code 11 Barcode.
@ Code128
Code 128 Barcode.
@ SonyCode
SonyCode Barcode.
@ PharmaCode2D
PharmaCode Matrix Code.
@ Qr
QR Matrix Code.
@ Ean13
EAN 13 Barcode.
@ DataMatrixGrading
DataMatrix grading.
@ FourStateUsps
Four State USPS Barcode.
@ Codabar
Codabar Barcode.
@ FourStateRoyalMail
Four State Royal Mail Barcode.
@ FourStateKix
Four State KIX Barcode.
@ BarcodeGrading
1D Barcode grading
@ FourStateAustralian
Four State Australian Barcode.
@ Code39
Code 39 Barcode.
@ Code32
Code 32 Barcode.
@ Pdf417
PDF417 Matrix Code.
@ Planet
Planet Barcode.
@ Interleaved2of5
2 of 5 Interleaved Barcode
@ PharmaCode
PharmaCode Barcode.
@ Postnet
Postnet Barcode.
@ MicroPdf417
Micro PDF417 Matrix Code.
@ UpcA
UPC-A Barcode.
@ Ean8
EAN 8 Barcode.
@ Code93
Code 93 Barcode.
@ MsiPlessey
MSI Plessey Barcode.
@ QrGrading
QR code grading.
@ DataMatrix
Data Matrix Code.
@ Discrete2of5
2 of 5 Discrete Barcode
@ UpcE
UPC-E Barcode.
std::shared_ptr< ReadResult > ReadResultPtr
Convenience shared pointer for ReadResult.
Definition: barcode.hpp:63
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24
std::string String
String for wide characters or unicode characters.
Definition: string.hpp:45