Zoltan Hudak / zbar

Dependents:   BarcodeReader_F103

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Decoder.h Source File

Decoder.h

Go to the documentation of this file.
00001 //------------------------------------------------------------------------
00002 //  Copyright 2007-2009 (c) Jeff Brown <spadix@users.sourceforge.net>
00003 //
00004 //  This file is part of the ZBar Bar Code Reader.
00005 //
00006 //  The ZBar Bar Code Reader is free software; you can redistribute it
00007 //  and/or modify it under the terms of the GNU Lesser Public License as
00008 //  published by the Free Software Foundation; either version 2.1 of
00009 //  the License, or (at your option) any later version.
00010 //
00011 //  The ZBar Bar Code Reader is distributed in the hope that it will be
00012 //  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00013 //  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU Lesser Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU Lesser Public License
00017 //  along with the ZBar Bar Code Reader; if not, write to the Free
00018 //  Software Foundation, Inc., 51 Franklin St, Fifth Floor,
00019 //  Boston, MA  02110-1301  USA
00020 //
00021 //  http://sourceforge.net/projects/zbar
00022 //------------------------------------------------------------------------
00023 #ifndef _ZBAR_DECODER_H_
00024 #define _ZBAR_DECODER_H_
00025 
00026 /// @file
00027 /// Decoder C++ wrapper
00028 
00029 #ifndef _ZBAR_H_
00030 # error "include zbar.h in your application, **not** zbar/Decoder.h"
00031 #endif
00032 
00033 #include <string>
00034 
00035 namespace zbar {
00036 
00037 /// low-level bar width stream decoder interface.
00038 /// identifies symbols and extracts encoded data
00039 
00040 class Decoder {
00041  public:
00042 
00043     /// Decoder result handler.
00044     /// applications should subtype this and pass an instance to
00045     /// set_handler() to implement result processing
00046     class Handler {
00047     public:
00048         virtual ~Handler() { }
00049 
00050         /// invoked by the Decoder as decode results become available.
00051         virtual void decode_callback(Decoder &decoder) = 0;
00052     };
00053 
00054     /// constructor.
00055     Decoder ()
00056         : _handler(NULL)
00057     {
00058         _decoder = zbar_decoder_create();
00059     }
00060 
00061     ~Decoder ()
00062     {
00063         zbar_decoder_destroy(_decoder);
00064     }
00065 
00066     /// clear all decoder state.
00067     /// see zbar_decoder_reset()
00068     void reset ()
00069     {
00070         zbar_decoder_reset(_decoder);
00071     }
00072 
00073     /// mark start of a new scan pass.
00074     /// see zbar_decoder_new_scan()
00075     void new_scan ()
00076     {
00077         zbar_decoder_new_scan(_decoder);
00078     }
00079 
00080     /// process next bar/space width from input stream.
00081     /// see zbar_decode_width()
00082     zbar_symbol_type_t decode_width (unsigned width)
00083     {
00084         return(zbar_decode_width(_decoder, width));
00085     }
00086 
00087     /// process next bar/space width from input stream.
00088     /// see zbar_decode_width()
00089     Decoder& operator<< (unsigned width)
00090     {
00091         zbar_decode_width(_decoder, width);
00092         return(*this);
00093     }
00094 
00095     /// retrieve color of @em next element passed to Decoder.
00096     /// see zbar_decoder_get_color()
00097     zbar_color_t get_color () const
00098     {
00099         return(zbar_decoder_get_color(_decoder));
00100     }
00101 
00102     /// retrieve last decoded symbol type.
00103     /// see zbar_decoder_get_type()
00104     zbar_symbol_type_t get_type () const
00105     {
00106         return(zbar_decoder_get_type(_decoder));
00107     }
00108 
00109     /// retrieve string name of last decoded symbol type.
00110     /// see zbar_get_symbol_name()
00111     const char *get_symbol_name () const
00112     {
00113         return(zbar_get_symbol_name(zbar_decoder_get_type(_decoder)));
00114     }
00115 
00116     /// retrieve string name for last decode addon.
00117     /// see zbar_get_addon_name()
00118     const char *get_addon_name () const
00119     {
00120         return(zbar_get_addon_name(zbar_decoder_get_type(_decoder)));
00121     }
00122 
00123     /// retrieve last decoded data in ASCII format as a char array.
00124     /// see zbar_decoder_get_data()
00125     const char *get_data_chars() const
00126     {
00127         return(zbar_decoder_get_data(_decoder));
00128     }
00129 
00130     /// retrieve last decoded data as a std::string.
00131     /// see zbar_decoder_get_data()
00132     const std::string get_data_string() const
00133     {
00134         return(std::string(zbar_decoder_get_data(_decoder),
00135                            zbar_decoder_get_data_length(_decoder)));
00136     }
00137 
00138     /// retrieve last decoded data as a std::string.
00139     /// see zbar_decoder_get_data()
00140     const std::string get_data() const
00141     {
00142         return(get_data_string());
00143     }
00144 
00145     /// retrieve length of decoded binary data.
00146     /// see zbar_decoder_get_data_length()
00147     int get_data_length() const
00148     {
00149         return(zbar_decoder_get_data_length(_decoder));
00150     }
00151 
00152     /// setup callback to handle result data.
00153     void set_handler (Handler &handler)
00154     {
00155         _handler = &handler;
00156         zbar_decoder_set_handler(_decoder, _cb);
00157         zbar_decoder_set_userdata(_decoder, this);
00158     }
00159 
00160     /// set config for indicated symbology (0 for all) to specified value.
00161     /// @see zbar_decoder_set_config()
00162     /// @since 0.4
00163     int set_config (zbar_symbol_type_t symbology,
00164                     zbar_config_t config,
00165                     int value)
00166     {
00167         return(zbar_decoder_set_config(_decoder, symbology, config, value));
00168     }
00169 
00170     /// set config parsed from configuration string.
00171     /// @see zbar_decoder_parse_config()
00172     /// @since 0.4
00173     int set_config (std::string cfgstr)
00174     {
00175         return(zbar_decoder_parse_config(_decoder, cfgstr.c_str()));
00176     }
00177 
00178  private:
00179     friend class Scanner;
00180     zbar_decoder_t *_decoder;
00181     Handler *_handler;
00182 
00183     static void _cb (zbar_decoder_t *cdcode)
00184     {
00185         Decoder *dcode = (Decoder*)zbar_decoder_get_userdata(cdcode);
00186         if(dcode && dcode->_handler)
00187             dcode->_handler->decode_callback(*dcode);
00188     }
00189 };
00190 
00191 }
00192 
00193 #endif