-
Notifications
You must be signed in to change notification settings - Fork 35
/
image_scanner.go
350 lines (305 loc) · 9.67 KB
/
image_scanner.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* Copyright (c) 2015, Robert Bieber
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package barcode
import (
"errors"
"image"
"runtime"
"unsafe"
"gocv.io/x/gocv"
)
// #include <zbar.h>
import "C"
// ImageScanner wraps zbar's internal image scanner type. You can set
// configuration options on an ImageScanner (make sure to enable some
// or all symbologies first) and then use it to scan an Image for
// barcodes.
type ImageScanner struct {
zbarScanner *C.zbar_image_scanner_t
}
// NewScanner creates a new ImageScanner and returns a pointer to it.
func NewScanner() *ImageScanner {
scanner := &ImageScanner{
zbarScanner: C.zbar_image_scanner_create(),
}
runtime.SetFinalizer(
scanner,
func(s *ImageScanner) {
C.zbar_image_scanner_destroy(s.zbarScanner)
},
)
return scanner
}
// ScanImage scans an Image and returns a slice of all Symbols found,
// or nil and an error if an error is encountered.
func (s *ImageScanner) ScanImage(img *Image) ([]*Symbol, error) {
resultCode := C.zbar_scan_image(s.zbarScanner, img.zbarImage)
if resultCode == 0 {
return []*Symbol{}, nil
} else if resultCode < 0 {
// There doesn't seem to be an error code function for the
// image scanner type
return nil, errors.New("zbar: Error scanning image")
}
getFirst := func() *C.zbar_symbol_t {
return C.zbar_image_first_symbol(img.zbarImage)
}
getNext := func(symbol *C.zbar_symbol_t) *C.zbar_symbol_t {
return C.zbar_symbol_next(symbol)
}
symbols := []*Symbol{}
for s := getFirst(); s != nil; s = getNext(s) {
newSym := Symbol{
Type: SymbolType(C.zbar_symbol_get_type(s)),
Data: C.GoString(C.zbar_symbol_get_data(s)),
Quality: int(C.zbar_symbol_get_quality(s)),
Boundary: []image.Point{},
}
for i := 0; i < int(C.zbar_symbol_get_loc_size(s)); i++ {
newSym.Boundary = append(
newSym.Boundary,
image.Pt(
int(C.zbar_symbol_get_loc_x(s, C.uint(i))),
int(C.zbar_symbol_get_loc_y(s, C.uint(i))),
),
)
}
symbols = append(symbols, &newSym)
}
return symbols, nil
}
// ScanMat scans an gocv.Mat avoiding the costly conversion and returns a slice
// of all Symbols found, or nil and an error if an error is encountered.
func (s *ImageScanner) ScanMat(img *gocv.Mat) (symbols []*Symbol, err error) {
imgBW := gocv.NewMat()
defer imgBW.Close()
imgType := img.Type()
if imgType == gocv.MatTypeCV8UC1 {
img.CopyTo(&imgBW)
} else if imgType == gocv.MatTypeCV8UC3 {
gocv.CvtColor(*img, &imgBW, gocv.ColorBGRToGray)
} else {
return nil, errors.New("only MatTypeCV8UC1 and MatTypeCV8UC3 is supported")
}
imgBwDataPtr, err := imgBW.DataPtrUint8()
if err != nil {
return nil, err
}
zbarImage := C.zbar_image_create()
dims := img.Size()
C.zbar_image_set_size(zbarImage, C.uint(dims[1]), C.uint(dims[0]))
C.zbar_image_set_format(zbarImage, C.ulong(y800))
C.zbar_image_set_data(
zbarImage,
unsafe.Pointer(&imgBwDataPtr[0]),
C.ulong(len(imgBwDataPtr)),
nil,
)
resultCode := C.zbar_scan_image(s.zbarScanner, zbarImage)
if resultCode > 0 {
for s := C.zbar_image_first_symbol(zbarImage); s != nil; s = C.zbar_symbol_next(s) {
newSym := Symbol{
Type: SymbolType(C.zbar_symbol_get_type(s)),
Data: C.GoString(C.zbar_symbol_get_data(s)),
Quality: int(C.zbar_symbol_get_quality(s)),
Boundary: []image.Point{},
}
for i := 0; i < int(C.zbar_symbol_get_loc_size(s)); i++ {
newSym.Boundary = append(
newSym.Boundary,
image.Pt(
int(C.zbar_symbol_get_loc_x(s, C.uint(i))),
int(C.zbar_symbol_get_loc_y(s, C.uint(i))),
),
)
}
symbols = append(symbols, &newSym)
}
} else if resultCode < 0 {
// There doesn't seem to be an error code function for the
// image scanner type
err = errors.New("zbar: Error scanning image")
}
C.zbar_image_set_data(zbarImage, nil, 0, nil)
C.zbar_image_destroy(zbarImage)
return symbols, err
}
// SetEnabledAll turns the ImageScanner on or off for all symbologies.
func (s *ImageScanner) SetEnabledAll(enabled bool) *ImageScanner {
return s.SetEnabledSymbology(None, enabled)
}
// SetEnabledSymbology turns the ImageScanner on or off for a specific
// SymbolType.
func (s *ImageScanner) SetEnabledSymbology(
symbology SymbolType,
enabled bool,
) *ImageScanner {
s.setBooleanConfig(C.ZBAR_CFG_ENABLE, symbology, enabled)
return s
}
// SetAddCheckAll enables or disables check digit when optional for
// all symbologies.
func (s *ImageScanner) SetAddCheckAll(enabled bool) *ImageScanner {
return s.SetAddCheckSymbology(None, enabled)
}
// SetAddCheckSymbology enables or disables check digit when optional
// for a specific SymbolType.
func (s *ImageScanner) SetAddCheckSymbology(
symbology SymbolType,
enabled bool,
) *ImageScanner {
s.setBooleanConfig(C.ZBAR_CFG_ADD_CHECK, symbology, enabled)
return s
}
// SetEmitCheckAll enables or disables return of the check digit when
// present for all symbologies.
func (s *ImageScanner) SetEmitCheckAll(enabled bool) *ImageScanner {
return s.SetEmitCheckSymbology(None, enabled)
}
// SetEmitCheckSymbology enables or disables return of the check digit
// when present for a specific SymbolType.
func (s *ImageScanner) SetEmitCheckSymbology(
symbology SymbolType,
enabled bool,
) *ImageScanner {
s.setBooleanConfig(C.ZBAR_CFG_EMIT_CHECK, symbology, enabled)
return s
}
// SetASCIIAll enables or disables the full ASCII character set for
// all symbologies.
func (s *ImageScanner) SetASCIIAll(enabled bool) *ImageScanner {
return s.SetASCIISymbology(None, enabled)
}
// SetASCIISymbology enables or disables the full ASCII character set
// for a specific SymbolType.
func (s *ImageScanner) SetASCIISymbology(
symbology SymbolType,
enabled bool,
) *ImageScanner {
s.setBooleanConfig(C.ZBAR_CFG_ASCII, symbology, enabled)
return s
}
// SetMinLengthAll sets a minimum data length for all symbologies.
func (s *ImageScanner) SetMinLengthAll(length int) *ImageScanner {
return s.SetMinLengthSymbology(None, length)
}
// SetMinLengthSymbology sets a minimum data length for a specific
// SymbolType.
func (s *ImageScanner) SetMinLengthSymbology(
symbology SymbolType,
length int,
) *ImageScanner {
s.setIntConfig(C.ZBAR_CFG_MIN_LEN, symbology, length)
return s
}
// SetMaxLengthAll sets a maximum data length for all symbologies.
func (s *ImageScanner) SetMaxLengthAll(length int) *ImageScanner {
return s.SetMaxLengthSymbology(None, length)
}
// SetMaxLengthSymbology sets a maximum data length for a specific
// SymbolType.
func (s *ImageScanner) SetMaxLengthSymbology(
symbology SymbolType,
length int,
) *ImageScanner {
s.setIntConfig(C.ZBAR_CFG_MAX_LEN, symbology, length)
return s
}
// SetPositionEnabledAll enables or disables the collection of
// position data for all symbologies.
func (s *ImageScanner) SetPositionEnabledAll(enabled bool) *ImageScanner {
return s.SetPositionEnabledSymbology(None, enabled)
}
// SetPositionEnabledSymbology enables or disables the collection of
// position data for a specific SymbolType.
func (s *ImageScanner) SetPositionEnabledSymbology(
symbology SymbolType,
enabled bool,
) *ImageScanner {
s.setBooleanConfig(C.ZBAR_CFG_POSITION, symbology, enabled)
return s
}
// SetXDensityAll sets the scanner's vertical scan density for all
// symbologies.
func (s *ImageScanner) SetXDensityAll(density int) *ImageScanner {
return s.SetXDensitySymbology(None, density)
}
// SetXDensitySymbology sets the scanner's vertical scan density for a
// specific SymbolType.
func (s *ImageScanner) SetXDensitySymbology(
symbology SymbolType,
density int,
) *ImageScanner {
s.setIntConfig(C.ZBAR_CFG_X_DENSITY, symbology, density)
return s
}
// SetYDensityAll sets the scanner's horizontal scan density for all
// symbologies.
func (s *ImageScanner) SetYDensityAll(density int) *ImageScanner {
return s.SetYDensitySymbology(None, density)
}
// SetYDensitySymbology sets the scanner's horizontal scan density for
// a specific SymbolType.
func (s *ImageScanner) SetYDensitySymbology(
symbology SymbolType,
density int,
) *ImageScanner {
s.setIntConfig(C.ZBAR_CFG_Y_DENSITY, symbology, density)
return s
}
func (s *ImageScanner) setBooleanConfig(
option C.zbar_config_t,
symbology SymbolType,
enabled bool,
) {
var e = 0
if enabled {
e = 1
}
C.zbar_image_scanner_set_config(
s.zbarScanner,
symbology.toEnum(),
option,
C.int(e),
)
}
func (s *ImageScanner) setIntConfig(
option C.zbar_config_t,
symbology SymbolType,
value int,
) {
C.zbar_image_scanner_set_config(
s.zbarScanner,
symbology.toEnum(),
option,
C.int(value),
)
}