forked from clsung/grcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zbar_image.go
68 lines (55 loc) · 1.49 KB
/
zbar_image.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
package grcode
// #cgo pkg-config: zbar
// #include <zbar.h>
import "C"
import "image"
import "image/draw"
import "unsafe"
type ZbarImage struct {
image *C.zbar_image_t
}
// VX_DF_IMAGE_Y800 is grayscale.
// Y800, GRAY is required
const VX_DF_IMAGE_Y800 = 0x30303859
// NewSymbol new the zbar image object
func NewZbarImage(img image.Image) *ZbarImage {
bounds := img.Bounds()
w := bounds.Max.X - bounds.Min.X
h := bounds.Max.Y - bounds.Min.Y
zImg := &ZbarImage{image: C.zbar_image_create()}
// draw image to zbar image with garyscale
gray := image.NewGray(bounds)
draw.Draw(gray, bounds, img, image.Point{}, draw.Over)
C.zbar_image_set_format(zImg.image, C.ulong(VX_DF_IMAGE_Y800))
C.zbar_image_set_size(zImg.image, C.uint(w), C.uint(h))
C.zbar_image_set_data(zImg.image, unsafe.Pointer(&gray.Pix[0]), C.ulong(len(gray.Pix)), nil)
// finalizer
// see notes in scanner.go
// runtime.SetFinalizer(zImg, (*ZbarImage).Destroy)
return zImg
}
// GetSymbol returns the first symbol of result
// use symbol.Next() to retrive next
func (z *ZbarImage) GetSymbol() *Symbol {
s := C.zbar_image_first_symbol(z.image)
if s == nil {
return nil
}
return NewSymbol(s)
}
// Symbols returns all the symbols.
func (z *ZbarImage) DerefSymbols() []DerefSymbol {
s := z.GetSymbol()
if s == nil {
return nil
}
var symbols []DerefSymbol
for ; s != nil; s = s.Next() {
symbols = append(symbols, s.Deref())
}
return symbols
}
// Close suicides
func (z *ZbarImage) Close() {
C.zbar_image_destroy(z.image)
}