File indexing completed on 2024-05-12 05:09:21

0001 /***************************************************************************
0002     Copyright (C) 2007-2009 Sebastian Held <sebastian.held@gmx.de>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *  based on BaToo: http://www.vs.inf.ethz.ch/res/show.html?what=barcode   *
0008  *                                                                         *
0009  *   This program is free software; you can redistribute it and/or         *
0010  *   modify it under the terms of the GNU General Public License as        *
0011  *   published by the Free Software Foundation; either version 2 of        *
0012  *   the License or (at your option) version 3 or any later version        *
0013  *   accepted by the membership of KDE e.V. (or its successor approved     *
0014  *   by the membership of KDE e.V.), which shall act as a proxy            *
0015  *   defined in Section 14 of version 3 of the license.                    *
0016  *                                                                         *
0017  *   This program is distributed in the hope that it will be useful,       *
0018  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0019  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0020  *   GNU General Public License for more details.                          *
0021  *                                                                         *
0022  *   You should have received a copy of the GNU General Public License     *
0023  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0024  *                                                                         *
0025  ***************************************************************************/
0026 
0027 #ifndef BARCODE_H
0028 #define BARCODE_H
0029 
0030 #include "barcode_v4l.h"
0031 
0032 #include <QThread>
0033 #include <QImage>
0034 #include <QVector>
0035 #include <QMutex>
0036 
0037 #include <math.h>
0038 
0039 //#define BarcodeDecoder_DEBUG
0040 //#define Decoder_EAN13_DEBUG
0041 
0042 namespace barcodeRecognition {
0043   static int code_odd[][4] = { { 30, 20, 10, 10 },
0044                             { 20, 20, 20, 10 },
0045                             { 20, 10, 20, 20 },
0046                             { 10, 40, 10, 10 },
0047                             { 10, 10, 30, 20 },
0048                             { 10, 20, 30, 10 },
0049                             { 10, 10, 10, 40 },
0050                             { 10, 30, 10, 20 },
0051                             { 10, 20, 10, 30 },
0052                             { 30, 10, 10, 20 } };
0053 
0054   static int code_even[][4] = { { 10, 10, 20, 30 },
0055                              { 10, 20, 20, 20 },
0056                              { 20, 20, 10, 20 },
0057                              { 10, 10, 40, 10 },
0058                              { 20, 30, 10, 10 },
0059                              { 10, 30, 20, 10 },
0060                              { 40, 10, 10, 10 },
0061                              { 20, 10, 30, 10 },
0062                              { 30, 10, 20, 10 },
0063                              { 20, 10, 10, 30 } };
0064 
0065   static bool parity_pattern_list[][6] = { { false, false, false, false, false, false },
0066                                            { false, false, true, false, true, true },
0067                                            { false, false, true, true, false, true },
0068                                            { false, false, true, true, true, false },
0069                                            { false, true, false, false, true, true },
0070                                            { false, true, true, false, false, true },
0071                                            { false, true, true, true, false, false },
0072                                            { false, true, false, true, false, true },
0073                                            { false, true, false, true, true, false },
0074                                            { false, true, true, false, true, false } };
0075 
0076   class Barcode_EAN13 {
0077   public:
0078     Barcode_EAN13();
0079     Barcode_EAN13( QVector<int> code );
0080     bool isNull() const { return m_null; }
0081     bool isValid() const;
0082     QVector<int> getNumbers() const;
0083     void setCode( QVector<int> code );
0084     QString toString() const;
0085     bool operator!= ( const Barcode_EAN13 &code );
0086   protected:
0087     QVector<int> m_numbers;
0088     bool m_null;
0089   };
0090 
0091   class MatchMakerResult {
0092   public:
0093     MatchMakerResult( bool even, int digit );
0094     bool isEven() const {return m_even;}
0095     int getDigit() const {return m_digit;}
0096   protected:
0097     int m_digit;
0098     bool m_even;
0099   };
0100 
0101   class Decoder_EAN13 {
0102   public:
0103     enum { BOTH_TABLES = 0, EVEN_TABLE = 1, ODD_TABLE = 2 };
0104     static Barcode_EAN13 recognize( QVector< QVector<int> > fields );
0105     static QVector<int> decode( QVector< QVector<int> > fields, int start_i, int end_i );
0106     static MatchMakerResult recognizeNumber( QVector< QVector<int> > fields, int code_table_to_use );
0107     static MatchMakerResult recognizeSystemCode( bool parity_pattern[6] );
0108   };
0109 
0110   /** \brief this thread handles barcode recognition using webcams
0111    *  @author Sebastian Held <sebastian.held@gmx.de>
0112    */
0113   class barcodeRecognitionThread : public QThread {
0114     Q_OBJECT
0115   public:
0116     barcodeRecognitionThread();
0117     ~barcodeRecognitionThread();
0118     virtual void run() Q_DECL_OVERRIDE;
0119     void stop();
0120     void recognizeBarcode( QImage img );
0121     bool isWebcamAvailable();
0122   QSize getPreviewSize() const;
0123   Q_SIGNALS:
0124     void recognized( QString barcode );
0125     void gotImage( const QImage &img );
0126   protected:
0127     volatile bool m_stop;
0128     QImage m_barcode_img;
0129     QMutex m_stop_mutex, m_barcode_img_mutex;
0130     barcode_v4l *m_barcode_v4l;
0131 
0132     Barcode_EAN13 recognize( QImage img );
0133     Barcode_EAN13 recognizeCode( QImage img, int x1, int x2, int y );
0134     void addNumberToPossibleNumbers( QVector<int> number, int possible_numbers[10][13][2], bool correct_code );
0135     void sortDigits( int possible_numbers[10][13][2] );
0136     Barcode_EAN13 extractBarcode( int possible_numbers[10][13][2] );
0137     QVector<int> transformPathToBW( QVector<QRgb> line);
0138     QVector< QVector<int> > extractFieldInformation( QVector<int> string );
0139     Barcode_EAN13 detectValidBarcode ( int possible_numbers[10][13][2], int max_amount_of_considered_codes );
0140     bool isValid( int numbers[13] );
0141     bool isValid( QVector<int> numbers );
0142     void printArray( int array[10][13][2], int level );
0143   };
0144 }
0145 
0146 #endif