IDS peak ICV v1.2.0
Loading...
Searching...
No Matches
peak_icv_code_reader.hpp
Go to the documentation of this file.
1
11#pragma once
14#include <peak_icv_c/algorithms/code_reader/peak_icv_code_reader.h>
15#include <algorithm>
16
17namespace peak
18{
19namespace icv
20{
21namespace experimental
22{
23
33{
34public:
41 CodeReader();
49
50 CodeReader(const CodeReader& other) = delete;
57 CodeReader(CodeReader&& other) noexcept;
58 CodeReader& operator=(const CodeReader& other) = delete;
65 CodeReader& operator=(CodeReader&& other) noexcept;
66
76 PEAK_COMMON_NO_DISCARD std::vector<CodeType> GetCodeTypesToDetect() const;
77
86 void setCodeTypesToDetect(const std::vector<CodeType>& codeTypesToDetect) const;
87
98 PEAK_COMMON_NO_DISCARD std::vector<CodeReaderResult> DetectAndDecode(const Image& image) const;
99
109 void SetMaximumNumberOfCodesToDetect(size_t count) const;
118 PEAK_COMMON_NO_DISCARD size_t GetMaximumNumberOfCodesToDetect() const;
119
120private:
121 peak_icv_code_reader_handle m_handle;
122
123 static peak_icv_code_reader_handle CreateFromHandle();
124};
125
126inline CodeReader::CodeReader(CodeReader&& other) noexcept
127{
128 m_handle = std::exchange(other.m_handle, nullptr);
129}
130
132{
133 if (m_handle != other.m_handle)
134 {
135 if (m_handle != nullptr)
136 {
138 return PEAK_ICV_C_ABI_PREFIX peak_icv_BarcodeReader_Destroy(m_handle);
139 });
140 }
141
142 m_handle = std::exchange(other.m_handle, nullptr);
143 }
144
145 return *this;
146}
147
148inline peak_icv_code_reader_handle CodeReader::CreateFromHandle()
149{
150 peak_icv_code_reader_handle handle{};
152 return PEAK_ICV_C_ABI_PREFIX peak_icv_BarcodeReader_Create(&handle);
153 });
154 return handle;
155}
156
158 : m_handle{ CreateFromHandle() }
159{}
160
162{
163 std::ignore = PEAK_ICV_C_ABI_PREFIX peak_icv_BarcodeReader_Destroy(m_handle);
164}
165
166inline std::vector<CodeType> CodeReader::GetCodeTypesToDetect() const
167{
168 size_t codeTypesCount{};
170 return PEAK_ICV_C_ABI_PREFIX peak_icv_BarcodeReader_GetCodeTypesGetCount(m_handle, &codeTypesCount);
171 });
172
173 std::vector<peak_icv_code_type> cTypes(codeTypesCount);
175 return PEAK_ICV_C_ABI_PREFIX peak_icv_BarcodeReader_GetCodeTypes(m_handle, cTypes.data(), codeTypesCount);
176 });
177
178 std::vector<CodeType> result;
179 result.reserve(cTypes.size());
180 std::transform(cTypes.begin(), cTypes.end(), std::back_inserter(result), [](const auto& cType) {
181 return static_cast<CodeType>(cType);
182 });
183 return result;
184}
185
186inline void CodeReader::setCodeTypesToDetect(const std::vector<CodeType>& codeTypesToDetect) const
187{
188 std::vector<peak_icv_code_type> cTypes;
189 cTypes.reserve(codeTypesToDetect.size());
190 std::transform(codeTypesToDetect.begin(), codeTypesToDetect.end(), std::back_inserter(cTypes), [](const auto& cType) {
191 return static_cast<peak_icv_code_type>(cType);
192 });
193
195 return PEAK_ICV_C_ABI_PREFIX peak_icv_BarcodeReader_SetCodeTypes(m_handle, cTypes.data(), cTypes.size());
196 });
197}
198
199inline std::vector<CodeReaderResult> CodeReader::DetectAndDecode(const Image& image) const
200{
201 std::vector<peak_icv_code_reader_result_handle> result_handles(GetMaximumNumberOfCodesToDetect(), PEAK_ICV_INVALID_HANDLE);
203 return PEAK_ICV_C_ABI_PREFIX peak_icv_BarcodeReaderResult_Array_Create(result_handles.data(), result_handles.size());
204 });
205 try
206 {
207 size_t found_count{};
209 return PEAK_ICV_C_ABI_PREFIX peak_icv_BarcodeReader_DetectAndDecode(m_handle,
210 peak::common::detail::BackendAccessor<Image>::BackendHandle(image), result_handles.data(), result_handles.size(),
211 &found_count);
212 });
213 for (size_t i = found_count; i < result_handles.size(); ++i)
214 {
216 return PEAK_ICV_C_ABI_PREFIX peak_icv_BarcodeReaderResult_Destroy(result_handles[i]);
217 });
218 }
219 result_handles.resize(found_count);
220 }
221 catch (const std::exception&)
222 {
223 for (auto& result_handle : result_handles)
224 {
226 return PEAK_ICV_C_ABI_PREFIX peak_icv_BarcodeReaderResult_Destroy(result_handle);
227 });
228 }
229 throw;
230 }
231 return peak::common::detail::BackendAccessor<CodeReaderResult>::CreateInstances(result_handles);
232}
233
234inline void CodeReader::SetMaximumNumberOfCodesToDetect(size_t count) const
235{
237 return PEAK_ICV_C_ABI_PREFIX peak_icv_BarcodeReader_SetMaximumNumberOfCodesToDetect(m_handle, count);
238 });
239}
240
242{
243 size_t count{};
245 return PEAK_ICV_C_ABI_PREFIX peak_icv_BarcodeReader_GetMaximumNumberOfCodesToDetect(m_handle, &count);
246 });
247 return count;
248}
249
250} // namespace experimental
251} // namespace icv
252} // namespace peak
For detailed information about the use of ids_peak_icv Image refer to the Image Guide.
Class to read codes from an image.
CodeReader()
Creates an instance of a code reader.
std::vector< CodeType > GetCodeTypesToDetect() const
Retrieves the number of configured code types.
void SetMaximumNumberOfCodesToDetect(size_t count) const
Sets the maximum number of codes to detect per call.
size_t GetMaximumNumberOfCodesToDetect() const
Retrieves the maximum number of codes to detect per call.
std::vector< CodeReaderResult > DetectAndDecode(const Image &image) const
Detects and decodes codes in an input image.
void setCodeTypesToDetect(const std::vector< CodeType > &codeTypesToDetect) const
Sets the allowed code types for detection and decoding.
CodeReader(const CodeReader &other)=delete
CodeReader & operator=(const CodeReader &other)=delete
void ExecuteAndMapReturnCodes(const CallableType &callableObject)