mirror of
https://github.com/gen2brain/cam2ip.git
synced 2026-01-10 00:28:32 +00:00
Add rotate option
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user