|
18 | 18 | using namespace ZXing; |
19 | 19 |
|
20 | 20 | static thread_local std::string lastErrorMsg; |
| 21 | +static Barcodes emptyBarcodes{}; // used to prevent new heap allocation for each empty result |
21 | 22 |
|
22 | 23 | template<typename R, typename T> R transmute_cast(const T& v) noexcept |
23 | 24 | { |
@@ -66,19 +67,6 @@ static uint8_t* copy(const ByteArray& ba, int* len) noexcept |
66 | 67 | ZX_CATCH({}) |
67 | 68 |
|
68 | 69 |
|
69 | | -static std::tuple<Barcodes, bool> ReadBarcodesAndSetLastError(const ZXing_ImageView* iv, const ZXing_ReaderOptions* opts, |
70 | | - int maxSymbols) |
71 | | -{ |
72 | | - ZX_CHECK(iv, "ImageView param is NULL") |
73 | | - try { |
74 | | - auto o = opts ? *opts : ReaderOptions{}; |
75 | | - if (maxSymbols) |
76 | | - o.setMaxNumberOfSymbols(maxSymbols); |
77 | | - return {ReadBarcodes(*iv, o), true}; |
78 | | - } |
79 | | - ZX_CATCH({Barcodes{}, false}) |
80 | | -} |
81 | | - |
82 | 70 | extern "C" { |
83 | 71 | /* |
84 | 72 | * ZXing/ImageView.h |
@@ -238,7 +226,8 @@ void ZXing_Barcode_delete(ZXing_Barcode* barcode) |
238 | 226 |
|
239 | 227 | void ZXing_Barcodes_delete(ZXing_Barcodes* barcodes) |
240 | 228 | { |
241 | | - delete barcodes; |
| 229 | + if (barcodes != &emptyBarcodes) |
| 230 | + delete barcodes; |
242 | 231 | } |
243 | 232 |
|
244 | 233 | int ZXing_Barcodes_size(const ZXing_Barcodes* barcodes) |
@@ -314,16 +303,14 @@ ZX_ENUM_PROPERTY(TextMode, textMode, TextMode) |
314 | 303 | * ZXing/ReadBarcode.h |
315 | 304 | */ |
316 | 305 |
|
317 | | -ZXing_Barcode* ZXing_ReadBarcode(const ZXing_ImageView* iv, const ZXing_ReaderOptions* opts) |
318 | | -{ |
319 | | - auto [res, ok] = ReadBarcodesAndSetLastError(iv, opts, 1); |
320 | | - return !res.empty() ? new Barcode(std::move(res.front())) : NULL; |
321 | | -} |
322 | | - |
323 | 306 | ZXing_Barcodes* ZXing_ReadBarcodes(const ZXing_ImageView* iv, const ZXing_ReaderOptions* opts) |
324 | 307 | { |
325 | | - auto [res, ok] = ReadBarcodesAndSetLastError(iv, opts, 0); |
326 | | - return !res.empty() || ok ? new Barcodes(std::move(res)) : NULL; |
| 308 | + ZX_CHECK(iv, "ImageView param is NULL") |
| 309 | + try { |
| 310 | + auto res = ReadBarcodes(*iv, opts ? *opts : ReaderOptions{}); |
| 311 | + return res.empty() ? &emptyBarcodes : new Barcodes(std::move(res)); |
| 312 | + } |
| 313 | + ZX_CATCH(NULL); |
327 | 314 | } |
328 | 315 |
|
329 | 316 |
|
|
0 commit comments