-
Notifications
You must be signed in to change notification settings - Fork 1
/
processor.go
134 lines (110 loc) · 4.66 KB
/
processor.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
package zbar
// #cgo LDFLAGS: -lzbar
// #include <stdlib.h>
// #include <zbar.h>
// #include "image_data_handler.h"
import "C"
import (
"unsafe"
)
// var callBacksMap = map[uintptr]func(img *Image){}
// NewProcessor creates new Processor instance.
// If threaded is set and threading is available the processor will spawn threads
// where appropriate to avoid blocking and improve responsiveness
func NewProcessor(threaded int) *Processor {
p := Processor{}
p.c_processor = C.zbar_processor_create(C.int(threaded))
return &p
}
type Processor struct {
c_processor *C.zbar_processor_t
dataHandler func(image *Image)
userdata unsafe.Pointer
}
// SetConfig sets config for indicated symbology (0 for all) to specified value.
// Returns 0 for success, non-0 for failure (config does not apply to specified symbology, or value out of range)
func (p *Processor) SetConfig(symbology int, config int, value int) int {
return int(C.zbar_processor_set_config(p.c_processor, C.zbar_symbol_type_t(symbology), C.zbar_config_t(config), C.int(value)))
}
// Init - device (re)initialization.
// Opens a video input device and/or prepares to display output
func (p *Processor) Init(device string, enableDisplay int) int {
c_device := C.CString(device)
defer C.free(unsafe.Pointer(c_device))
return int(C.zbar_processor_init(p.c_processor, c_device, C.int(enableDisplay)))
}
// Destroy cleans up all resources associated with the processor
func (p *Processor) Destroy() {
C.zbar_processor_destroy(p.c_processor)
p = nil
}
// RequestSize requests a preferred size for the video image from the device
func (p *Processor) RequestSize(width, height uint) int {
return int(C.zbar_processor_request_size(p.c_processor, C.unsigned(width), C.unsigned(height)))
}
// RequestInterface requests a preferred video driver interface version for debug/testing.
func (p *Processor) RequestInterface(version int) int {
return int(C.zbar_processor_request_interface(p.c_processor, C.int(version)))
}
// RequestIOMode requests a preferred video I/O mode for debug/testing.
func (p *Processor) RequestIOMode(iomode int) int {
return int(C.zbar_processor_request_iomode(p.c_processor, C.int(iomode)))
}
// ForceFormat forces specific input and output formats for debug/testing.
func (p *Processor) ForceFormat(inputFormat, outputFormat uint64) int {
return int(C.zbar_processor_force_format(p.c_processor, C.ulong(inputFormat), C.ulong(outputFormat)))
}
//export image_handler_callback
func image_handler_callback(image *C.zbar_image_t, userdata unsafe.Pointer) {
img := Image{}
img.c_image = image
p := (*Processor)(userdata)
p.dataHandler(&img)
}
// Setup result handler callback.
// The specified function will be called by the processor whenever new results are available from the video stream or a static image.
func (p *Processor) SetDataHandler(fn func(img *Image)) {
p.dataHandler = fn
C.zbar_processor_set_data_handler(p.c_processor, (*C.zbar_image_data_handler_t)(C.image_data_handler), unsafe.Pointer(p))
}
// Associate user specified data value with the processor.
func (p *Processor) SetUserData(userdata unsafe.Pointer) {
C.zbar_processor_set_userdata(p.c_processor, userdata)
}
// Return user specified data value associated with the processor.
func (p *Processor) GetUserData() unsafe.Pointer {
return unsafe.Pointer(C.zbar_processor_get_userdata(p.c_processor))
}
// Show or hide the display window owned by the library.
func (p *Processor) SetVisible(visible int) int {
return int(C.zbar_processor_set_visible(p.c_processor, C.int(visible)))
}
// Control the processor in free running video mode.
func (p *Processor) SetActive(active int) int {
return int(C.zbar_processor_set_active(p.c_processor, C.int(active)))
}
// Process from the video stream until a result is available, or the timeout (in milliseconds) expires.
func (p *Processor) ProcessOne(timeout int) int {
return int(C.zbar_process_one(p.c_processor, C.int(timeout)))
}
// Retrieve decode results for last scanned image/frame.
func (p *Processor) GetResults() *SymbolSet {
ss := SymbolSet{}
ss.c_symbol_set = C.zbar_processor_get_results(p.c_processor)
if ss.c_symbol_set != nil {
return &ss
}
return nil
}
// Wait for input to the display window from the user (via mouse or keyboard).
func (p *Processor) UserWait(timeout int) int {
return int(C.zbar_processor_user_wait(p.c_processor, C.int(timeout)))
}
// Retrieve the detail string for the last processor error
func (p *Processor) ErrorString(verbosity int) string {
return C.GoString(C.zbar_processor_error_string(p.c_processor, C.int(verbosity)))
}
// Retrieve the type code for the last processor error.
func (p *Processor) GetErrorCode() int {
return int(C.zbar_processor_get_error_code(p.c_processor))
}