File size: 2,596 Bytes
be94e5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
// Copyright (c) 2020-2021 darkliang wangberlinT Certseeds

#ifndef OPENCV_BARCODE_ABS_DECODER_HPP
#define OPENCV_BARCODE_ABS_DECODER_HPP

#include "opencv2/objdetect/barcode.hpp"

namespace cv {
namespace barcode {
using std::string;
using std::vector;
constexpr static uchar BLACK = std::numeric_limits<uchar>::min();
// WHITE elemental area is 0xff
constexpr static uchar WHITE = std::numeric_limits<uchar>::max();


struct Result
{
    enum BarcodeType
    {
        BARCODE_NONE,
        BARCODE_EAN_8,
        BARCODE_EAN_13,
        BARCODE_UPC_A,
        BARCODE_UPC_E,
        BARCODE_UPC_EAN_EXTENSION
    };

    std::string result;
    BarcodeType format = Result::BARCODE_NONE;

    Result() = default;

    Result(const std::string &_result, BarcodeType _format)
    {
        result = _result;
        format = _format;
    }
    string typeString() const

    {
        switch (format)
        {
            case Result::BARCODE_EAN_8: return "EAN_8";
            case Result::BARCODE_EAN_13: return "EAN_13";
            case Result::BARCODE_UPC_E: return "UPC_E";
            case Result::BARCODE_UPC_A: return "UPC_A";
            case Result::BARCODE_UPC_EAN_EXTENSION: return "UPC_EAN_EXTENSION";
            default: return string();
        }
    }
    bool isValid() const

    {
        return format != BARCODE_NONE;
    }
};

struct Counter
{
    std::vector<int> pattern;
    uint sum;

    explicit Counter(const vector<int> &_pattern)

    {
        pattern = _pattern;
        sum = 0;
    }
};

class AbsDecoder
{
public:
    virtual std::pair<Result, float> decodeROI(const Mat &bar_img) const = 0;

    virtual ~AbsDecoder() = default;

protected:
    virtual Result decode(const vector<uchar> &data) const = 0;

    virtual bool isValid(const string &result) const = 0;

    size_t bits_num{};
    size_t digit_number{};
};

void cropROI(const Mat &_src, Mat &_dst, const std::vector<Point2f> &rect);

void fillCounter(const std::vector<uchar> &row, uint start, Counter &counter);

constexpr static uint INTEGER_MATH_SHIFT = 8;
constexpr static uint PATTERN_MATCH_RESULT_SCALE_FACTOR = 1 << INTEGER_MATH_SHIFT;

uint patternMatch(const Counter &counters, const std::vector<int> &pattern, uint maxIndividual);
}
} // namespace cv

#endif // OPENCV_BARCODE_ABS_DECODER_HPP