Add rotate option

This commit is contained in:
Milan Nikolic
2018-10-10 04:32:00 +02:00
parent 37b19fcfe8
commit c9d77f03e7
8 changed files with 141 additions and 20 deletions

View File

@@ -7,25 +7,34 @@ import (
"fmt"
"image"
"github.com/disintegration/imaging"
"gocv.io/x/gocv"
)
// Options.
type Options struct {
Index int
Rotate int
}
// Camera represents camera.
type Camera struct {
opts Options
camera *gocv.VideoCapture
frame *gocv.Mat
}
// New returns new Camera for given camera index.
func New(index int) (camera *Camera, err error) {
func New(opts Options) (camera *Camera, err error) {
camera = &Camera{}
camera.opts = opts
mat := gocv.NewMat()
camera.frame = &mat
camera.camera, err = gocv.VideoCaptureDevice(index)
camera.camera, err = gocv.VideoCaptureDevice(opts.Index)
if err != nil {
err = fmt.Errorf("camera: can not open camera %d: %s", index, err.Error())
err = fmt.Errorf("camera: can not open camera %d: %s", opts.Index, err.Error())
}
return
@@ -45,6 +54,24 @@ func (c *Camera) Read() (img image.Image, err error) {
return
}
if c.frame == nil {
err = fmt.Errorf("camera: can not grab frame")
return
}
if c.opts.Rotate == 0 {
return
}
switch c.opts.Rotate {
case 90:
img = imaging.Rotate90(img)
case 180:
img = imaging.Rotate180(img)
case 270:
img = imaging.Rotate270(img)
}
return
}