forked from MordFustang21/gozbar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
56 lines (48 loc) · 1.33 KB
/
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
// Package gozbar scanner bindings for golang.
// Read the ZBar documents for details
package gozbar
// #cgo LDFLAGS: -lzbar
// #include <zbar.h>
import "C"
import (
"errors"
"fmt"
)
// Scanner contains a reference to zbar scanner.
type Scanner struct {
scanner *C.zbar_image_scanner_t
}
// NewScanner returns a new instance of Scanner.
func NewScanner() *Scanner {
return &Scanner{
scanner: C.zbar_image_scanner_create(),
}
}
// SetConfig gives the zbar scanner the configuration to run..
// Read the ZBar docs for details.
func (s *Scanner) SetConfig(symbology C.zbar_symbol_type_t, config C.zbar_config_t, value int) error {
// returns 0 for success, non-0 for failure
resp := int(C.zbar_image_scanner_set_config(s.scanner, symbology, config, C.int(value)))
if resp != 0 {
return fmt.Errorf("error encountered when setting config status: %d", resp)
}
return nil
}
// Scan parses the given image and loads the scanner.
func (s *Scanner) Scan(img *Image) error {
status := int(C.zbar_scan_image(s.scanner, img.image))
switch status {
// no symbols were found
case 0:
return errors.New("no symbols found")
// an error occurred
case -1:
return errors.New("an error was encountered during scan")
default:
return nil
}
}
// Destroy destroys the scanner.
func (s *Scanner) Destroy() {
C.zbar_image_scanner_destroy(s.scanner)
}