-
Notifications
You must be signed in to change notification settings - Fork 4
/
image.go
71 lines (57 loc) · 1.59 KB
/
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
69
70
71
// Package gozbar image bindings for golang.
// Read the ZBar documents for details
package gozbar
// #cgo LDFLAGS: -lzbar
// #include <zbar.h>
import "C"
import (
"image"
"runtime"
"unsafe"
)
// Image contains a zbar image and the grayscale values.
type Image struct {
image *C.zbar_image_t
gray *image.Gray
}
// FromImage will create an ZBar image object from an image.Image.
// To scan the image, call a Scanner.
func FromImage(img image.Image) *Image {
// allocate the image wrapper
ret := &Image{
image: C.zbar_image_create(),
}
// get the height and width of the given image
bounds := img.Bounds()
w := bounds.Max.X - bounds.Min.X
h := bounds.Max.Y - bounds.Min.Y
// Create a grayscale image
ret.gray = image.NewGray(bounds)
// populate all the pixels in the gray image faster than draw.Draw()
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
ret.gray.Set(x, y, img.At(x, y))
}
}
C.zbar_image_set_format(ret.image, C.ulong(0x30303859)) // Y800 (grayscale)
C.zbar_image_set_size(ret.image, C.uint(w), C.uint(h))
C.zbar_image_set_data(ret.image, unsafe.Pointer(&ret.gray.Pix[0]), C.ulong(len(ret.gray.Pix)), nil)
// finalizer
runtime.SetFinalizer(ret, (*Image).Destroy)
return ret
}
// First will return the first scanned symbol of this image.
// To iterate over the symbols, use Symbol.Each() function
func (i *Image) First() *Symbol {
s := C.zbar_image_first_symbol(i.image)
if s == nil {
return nil
}
return &Symbol{
symbol: s,
}
}
// Destroy this object
func (i *Image) Destroy() {
C.zbar_image_destroy(i.image)
}