forked from makiuchi-d/gozxing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datamatrix_reader.go
154 lines (138 loc) · 4.27 KB
/
datamatrix_reader.go
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package datamatrix
import (
"github.com/kaxap/gozxing"
"github.com/kaxap/gozxing/common"
"github.com/kaxap/gozxing/datamatrix/decoder"
"github.com/kaxap/gozxing/datamatrix/detector"
)
var (
noPoints = []gozxing.ResultPoint{}
)
type DataMatrixReader struct {
decoder *decoder.Decoder
}
func NewDataMatrixReader() *DataMatrixReader {
return &DataMatrixReader{
decoder: decoder.NewDecoder(),
}
}
func (r *DataMatrixReader) DecodeWithoutHints(image *gozxing.BinaryBitmap) (*gozxing.Result, error) {
return r.Decode(image, nil)
}
// Decode Locates and decodes a Data Matrix code in an image.
//
// @return a String representing the content encoded by the Data Matrix code
// @throws NotFoundException if a Data Matrix code cannot be found
// @throws FormatException if a Data Matrix code cannot be decoded
// @throws ChecksumException if error correction fails
//
func (r *DataMatrixReader) Decode(image *gozxing.BinaryBitmap, hints map[gozxing.DecodeHintType]interface{}) (*gozxing.Result, error) {
var decoderResult *common.DecoderResult
var points []gozxing.ResultPoint
if _, ok := hints[gozxing.DecodeHintType_PURE_BARCODE]; ok {
blackm, e := image.GetBlackMatrix()
if e != nil {
return nil, gozxing.WrapReaderException(e)
}
bits, e := extractPureBits(blackm)
if e != nil {
return nil, e
}
decoderResult, e = r.decoder.Decode(bits)
if e != nil {
return nil, e
}
points = noPoints
} else {
blackm, e := image.GetBlackMatrix()
if e != nil {
return nil, gozxing.WrapReaderException(e)
}
detector, e := detector.NewDetector(blackm)
if e != nil {
return nil, e
}
detectorResult, e := detector.Detect()
if e != nil {
return nil, e
}
decoderResult, e = r.decoder.Decode(detectorResult.GetBits())
if e != nil {
return nil, e
}
points = detectorResult.GetPoints()
}
result := gozxing.NewResult(decoderResult.GetText(), decoderResult.GetRawBytes(), points,
gozxing.BarcodeFormat_DATA_MATRIX)
byteSegments := decoderResult.GetByteSegments()
if byteSegments != nil {
result.PutMetadata(gozxing.ResultMetadataType_BYTE_SEGMENTS, byteSegments)
}
ecLevel := decoderResult.GetECLevel()
if ecLevel != "" {
result.PutMetadata(gozxing.ResultMetadataType_ERROR_CORRECTION_LEVEL, ecLevel)
}
return result, nil
}
func (r *DataMatrixReader) Reset() {
// do nothing
}
// extractPureBits This method detects a code in a "pure" image -- that is, pure monochrome image
// which contains only an unrotated, unskewed, image of a code, with some white border
// around it. This is a specialized method that works exceptionally fast in this special case.
//
func extractPureBits(image *gozxing.BitMatrix) (*gozxing.BitMatrix, error) {
leftTopBlack := image.GetTopLeftOnBit()
rightBottomBlack := image.GetBottomRightOnBit()
if leftTopBlack == nil || rightBottomBlack == nil {
return nil, gozxing.NewNotFoundException(
"leftTopBlack=%v, rightBottomBlack=%v", leftTopBlack, rightBottomBlack)
}
moduleSize, e := moduleSize(leftTopBlack, image)
if e != nil {
return nil, e
}
top := leftTopBlack[1]
bottom := rightBottomBlack[1]
left := leftTopBlack[0]
right := rightBottomBlack[0]
matrixWidth := (right - left + 1) / moduleSize
matrixHeight := (bottom - top + 1) / moduleSize
if matrixWidth <= 0 || matrixHeight <= 0 {
return nil, gozxing.NewNotFoundException(
"matrixWidth=%v, matrixHeight=%v", matrixWidth, matrixHeight)
}
// Push in the "border" by half the module width so that we start
// sampling in the middle of the module. Just in case the image is a
// little off, this will help recover.
nudge := moduleSize / 2
top += nudge
left += nudge
// Now just read off the bits
bits, _ := gozxing.NewBitMatrix(matrixWidth, matrixHeight)
for y := 0; y < matrixHeight; y++ {
iOffset := top + y*moduleSize
for x := 0; x < matrixWidth; x++ {
if image.Get(left+x*moduleSize, iOffset) {
bits.Set(x, y)
}
}
}
return bits, nil
}
func moduleSize(leftTopBlack []int, image *gozxing.BitMatrix) (int, error) {
width := image.GetWidth()
x := leftTopBlack[0]
y := leftTopBlack[1]
for x < width && image.Get(x, y) {
x++
}
if x == width {
return 0, gozxing.NewNotFoundException("x == width, %v", x)
}
moduleSize := x - leftTopBlack[0]
if moduleSize == 0 {
return 0, gozxing.NewNotFoundException("moduleSize == 0")
}
return moduleSize, nil
}