|
| 1 | +//------------------------------------------------------------------------ |
| 2 | +// Copyright 2007-2010 (c) Jeff Brown <spadix@users.sourceforge.net> |
| 3 | +// |
| 4 | +// This file is part of the ZBar Bar Code Reader. |
| 5 | +// |
| 6 | +// The ZBar Bar Code Reader is free software; you can redistribute it |
| 7 | +// and/or modify it under the terms of the GNU Lesser Public License as |
| 8 | +// published by the Free Software Foundation; either version 2.1 of |
| 9 | +// the License, or (at your option) any later version. |
| 10 | +// |
| 11 | +// The ZBar Bar Code Reader is distributed in the hope that it will be |
| 12 | +// useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 13 | +// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +// GNU Lesser Public License for more details. |
| 15 | +// |
| 16 | +// You should have received a copy of the GNU Lesser Public License |
| 17 | +// along with the ZBar Bar Code Reader; if not, write to the Free |
| 18 | +// Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 19 | +// Boston, MA 02110-1301 USA |
| 20 | +// |
| 21 | +// http://sourceforge.net/projects/zbar |
| 22 | +//------------------------------------------------------------------------ |
| 23 | +#ifndef _ZBAR_DECODER_H_ |
| 24 | +#define _ZBAR_DECODER_H_ |
| 25 | + |
| 26 | +/// @file |
| 27 | +/// Decoder C++ wrapper |
| 28 | + |
| 29 | +#ifndef _ZBAR_H_ |
| 30 | +# error "include zbar.h in your application, **not** zbar/Decoder.h" |
| 31 | +#endif |
| 32 | + |
| 33 | +#include <string> |
| 34 | + |
| 35 | +namespace zbar { |
| 36 | + |
| 37 | +/// low-level bar width stream decoder interface. |
| 38 | +/// identifies symbols and extracts encoded data |
| 39 | + |
| 40 | +class Decoder { |
| 41 | + public: |
| 42 | + |
| 43 | + /// Decoder result handler. |
| 44 | + /// applications should subtype this and pass an instance to |
| 45 | + /// set_handler() to implement result processing |
| 46 | + class Handler { |
| 47 | + public: |
| 48 | + virtual ~Handler() { } |
| 49 | + |
| 50 | + /// invoked by the Decoder as decode results become available. |
| 51 | + virtual void decode_callback(Decoder &decoder) = 0; |
| 52 | + }; |
| 53 | + |
| 54 | + /// constructor. |
| 55 | + Decoder () |
| 56 | + : _handler(NULL) |
| 57 | + { |
| 58 | + _decoder = zbar_decoder_create(); |
| 59 | + } |
| 60 | + |
| 61 | + ~Decoder () |
| 62 | + { |
| 63 | + zbar_decoder_destroy(_decoder); |
| 64 | + } |
| 65 | + |
| 66 | + /// clear all decoder state. |
| 67 | + /// see zbar_decoder_reset() |
| 68 | + void reset () |
| 69 | + { |
| 70 | + zbar_decoder_reset(_decoder); |
| 71 | + } |
| 72 | + |
| 73 | + /// mark start of a new scan pass. |
| 74 | + /// see zbar_decoder_new_scan() |
| 75 | + void new_scan () |
| 76 | + { |
| 77 | + zbar_decoder_new_scan(_decoder); |
| 78 | + } |
| 79 | + |
| 80 | + /// process next bar/space width from input stream. |
| 81 | + /// see zbar_decode_width() |
| 82 | + zbar_symbol_type_t decode_width (unsigned width) |
| 83 | + { |
| 84 | + return(zbar_decode_width(_decoder, width)); |
| 85 | + } |
| 86 | + |
| 87 | + /// process next bar/space width from input stream. |
| 88 | + /// see zbar_decode_width() |
| 89 | + Decoder& operator<< (unsigned width) |
| 90 | + { |
| 91 | + zbar_decode_width(_decoder, width); |
| 92 | + return(*this); |
| 93 | + } |
| 94 | + |
| 95 | + /// retrieve color of @em next element passed to Decoder. |
| 96 | + /// see zbar_decoder_get_color() |
| 97 | + zbar_color_t get_color () const |
| 98 | + { |
| 99 | + return(zbar_decoder_get_color(_decoder)); |
| 100 | + } |
| 101 | + |
| 102 | + /// retrieve last decoded symbol type. |
| 103 | + /// see zbar_decoder_get_type() |
| 104 | + zbar_symbol_type_t get_type () const |
| 105 | + { |
| 106 | + return(zbar_decoder_get_type(_decoder)); |
| 107 | + } |
| 108 | + |
| 109 | + /// retrieve string name of last decoded symbol type. |
| 110 | + /// see zbar_get_symbol_name() |
| 111 | + const char *get_symbol_name () const |
| 112 | + { |
| 113 | + return(zbar_get_symbol_name(zbar_decoder_get_type(_decoder))); |
| 114 | + } |
| 115 | + |
| 116 | + /// retrieve string name for last decode addon. |
| 117 | + /// see zbar_get_addon_name() |
| 118 | + /// @deprecated in 0.11 |
| 119 | + const char *get_addon_name () const |
| 120 | + { |
| 121 | + return(zbar_get_addon_name(zbar_decoder_get_type(_decoder))); |
| 122 | + } |
| 123 | + |
| 124 | + /// retrieve last decoded data in ASCII format as a char array. |
| 125 | + /// see zbar_decoder_get_data() |
| 126 | + const char *get_data_chars() const |
| 127 | + { |
| 128 | + return(zbar_decoder_get_data(_decoder)); |
| 129 | + } |
| 130 | + |
| 131 | + /// retrieve last decoded data as a std::string. |
| 132 | + /// see zbar_decoder_get_data() |
| 133 | + const std::string get_data_string() const |
| 134 | + { |
| 135 | + return(std::string(zbar_decoder_get_data(_decoder), |
| 136 | + zbar_decoder_get_data_length(_decoder))); |
| 137 | + } |
| 138 | + |
| 139 | + /// retrieve last decoded data as a std::string. |
| 140 | + /// see zbar_decoder_get_data() |
| 141 | + const std::string get_data() const |
| 142 | + { |
| 143 | + return(get_data_string()); |
| 144 | + } |
| 145 | + |
| 146 | + /// retrieve length of decoded binary data. |
| 147 | + /// see zbar_decoder_get_data_length() |
| 148 | + int get_data_length() const |
| 149 | + { |
| 150 | + return(zbar_decoder_get_data_length(_decoder)); |
| 151 | + } |
| 152 | + |
| 153 | + /// retrieve last decode direction. |
| 154 | + /// see zbar_decoder_get_direction() |
| 155 | + /// @since 0.11 |
| 156 | + int get_direction() const |
| 157 | + { |
| 158 | + return(zbar_decoder_get_direction(_decoder)); |
| 159 | + } |
| 160 | + |
| 161 | + /// setup callback to handle result data. |
| 162 | + void set_handler (Handler &handler) |
| 163 | + { |
| 164 | + _handler = &handler; |
| 165 | + zbar_decoder_set_handler(_decoder, _cb); |
| 166 | + zbar_decoder_set_userdata(_decoder, this); |
| 167 | + } |
| 168 | + |
| 169 | + /// set config for indicated symbology (0 for all) to specified value. |
| 170 | + /// @see zbar_decoder_set_config() |
| 171 | + /// @since 0.4 |
| 172 | + int set_config (zbar_symbol_type_t symbology, |
| 173 | + zbar_config_t config, |
| 174 | + int value) |
| 175 | + { |
| 176 | + return(zbar_decoder_set_config(_decoder, symbology, config, value)); |
| 177 | + } |
| 178 | + |
| 179 | + /// set config parsed from configuration string. |
| 180 | + /// @see zbar_decoder_parse_config() |
| 181 | + /// @since 0.4 |
| 182 | + int set_config (std::string cfgstr) |
| 183 | + { |
| 184 | + return(zbar_decoder_parse_config(_decoder, cfgstr.c_str())); |
| 185 | + } |
| 186 | + |
| 187 | + private: |
| 188 | + friend class Scanner; |
| 189 | + zbar_decoder_t *_decoder; |
| 190 | + Handler *_handler; |
| 191 | + |
| 192 | + static void _cb (zbar_decoder_t *cdcode) |
| 193 | + { |
| 194 | + Decoder *dcode = (Decoder*)zbar_decoder_get_userdata(cdcode); |
| 195 | + if(dcode && dcode->_handler) |
| 196 | + dcode->_handler->decode_callback(*dcode); |
| 197 | + } |
| 198 | +}; |
| 199 | + |
| 200 | +} |
| 201 | + |
| 202 | +#endif |
0 commit comments