mirror of
https://github.com/gen2brain/cam2ip.git
synced 2026-05-01 02:12:36 +00:00
Add support for native V4L implementation on Linux/RPi
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// +build !cv3
|
||||
// +build !cv3,!native
|
||||
|
||||
// Package camera.
|
||||
package camera
|
||||
@@ -15,6 +15,8 @@ import (
|
||||
type Options struct {
|
||||
Index int
|
||||
Rotate int
|
||||
Width float64
|
||||
Height float64
|
||||
}
|
||||
|
||||
// Camera represents camera.
|
||||
@@ -34,6 +36,9 @@ func New(opts Options) (camera *Camera, err error) {
|
||||
err = fmt.Errorf("camera: can not open camera %d", opts.Index)
|
||||
}
|
||||
|
||||
camera.SetProperty(PropFrameWidth, opts.Width)
|
||||
camera.SetProperty(PropFrameHeight, opts.Height)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -43,7 +48,7 @@ func (c *Camera) Read() (img image.Image, err error) {
|
||||
c.frame = c.camera.RetrieveFrame(1)
|
||||
|
||||
if c.frame == nil {
|
||||
err = fmt.Errorf("camera: can not grab frame")
|
||||
err = fmt.Errorf("camera: can not retrieve frame")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
// +build !native
|
||||
|
||||
package camera
|
||||
|
||||
// Property identifiers.
|
||||
|
||||
21
camera/camera_const_native.go
Normal file
21
camera/camera_const_native.go
Normal file
@@ -0,0 +1,21 @@
|
||||
// +build native
|
||||
|
||||
package camera
|
||||
|
||||
import (
|
||||
"github.com/korandiz/v4l"
|
||||
)
|
||||
|
||||
// Property identifiers.
|
||||
const (
|
||||
PropBrightness = v4l.CtrlBrightness
|
||||
PropContrast = v4l.CtrlContrast
|
||||
PropSaturation = v4l.CtrlSaturation
|
||||
PropHue = v4l.CtrlHue
|
||||
PropGain = v4l.CtrlGain
|
||||
PropExposure = v4l.CtrlExposure
|
||||
PropWhiteBalanceU = v4l.CtrlWhiteBalance
|
||||
PropSharpness = v4l.CtrlSharpness
|
||||
PropWhiteBalanceV = v4l.CtrlDoWhiteBalance
|
||||
PropBacklight = v4l.CtrlBacklightCompensation
|
||||
)
|
||||
@@ -1,4 +1,4 @@
|
||||
// +build cv3
|
||||
// +build cv3,!native
|
||||
|
||||
// Package camera.
|
||||
package camera
|
||||
@@ -15,6 +15,8 @@ import (
|
||||
type Options struct {
|
||||
Index int
|
||||
Rotate int
|
||||
Width float64
|
||||
Height float64
|
||||
}
|
||||
|
||||
// Camera represents camera.
|
||||
@@ -37,6 +39,9 @@ func New(opts Options) (camera *Camera, err error) {
|
||||
err = fmt.Errorf("camera: can not open camera %d: %s", opts.Index, err.Error())
|
||||
}
|
||||
|
||||
camera.SetProperty(PropFrameWidth, opts.Width)
|
||||
camera.SetProperty(PropFrameHeight, opts.Height)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -55,7 +60,7 @@ func (c *Camera) Read() (img image.Image, err error) {
|
||||
}
|
||||
|
||||
if c.frame == nil {
|
||||
err = fmt.Errorf("camera: can not grab frame")
|
||||
err = fmt.Errorf("camera: can not retrieve frame")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
132
camera/camera_native_linux.go
Normal file
132
camera/camera_native_linux.go
Normal file
@@ -0,0 +1,132 @@
|
||||
// +build native
|
||||
|
||||
// Package camera.
|
||||
package camera
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
|
||||
"github.com/disintegration/imaging"
|
||||
"github.com/korandiz/v4l"
|
||||
"github.com/korandiz/v4l/fmt/mjpeg"
|
||||
|
||||
im "github.com/gen2brain/cam2ip/image"
|
||||
)
|
||||
|
||||
// Options.
|
||||
type Options struct {
|
||||
Index int
|
||||
Rotate int
|
||||
Width float64
|
||||
Height float64
|
||||
}
|
||||
|
||||
// Camera represents camera.
|
||||
type Camera struct {
|
||||
opts Options
|
||||
camera *v4l.Device
|
||||
}
|
||||
|
||||
// New returns new Camera for given camera index.
|
||||
func New(opts Options) (camera *Camera, err error) {
|
||||
camera = &Camera{}
|
||||
camera.opts = opts
|
||||
|
||||
devices := v4l.FindDevices()
|
||||
if len(devices) < opts.Index+1 {
|
||||
err = fmt.Errorf("camera: no camera at index %d", opts.Index)
|
||||
return
|
||||
}
|
||||
|
||||
camera.camera, err = v4l.Open(devices[opts.Index].Path)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("camera: %s", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if camera.camera == nil {
|
||||
err = fmt.Errorf("camera: can not open camera %d", opts.Index)
|
||||
return
|
||||
}
|
||||
|
||||
config, err := camera.camera.GetConfig()
|
||||
if err != nil {
|
||||
err = fmt.Errorf("camera: %s", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
config.Format = mjpeg.FourCC
|
||||
config.Width = int(opts.Width)
|
||||
config.Height = int(opts.Height)
|
||||
|
||||
err = camera.camera.SetConfig(config)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("camera: %s", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
err = camera.camera.TurnOn()
|
||||
if err != nil {
|
||||
err = fmt.Errorf("camera: %s", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Read reads next frame from camera and returns image.
|
||||
func (c *Camera) Read() (img image.Image, err error) {
|
||||
|
||||
buffer, err := c.camera.Capture()
|
||||
if err != nil {
|
||||
err = fmt.Errorf("camera: can not grab frame: %s", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
img, err = im.NewDecoder(buffer).Decode()
|
||||
if err != nil {
|
||||
err = fmt.Errorf("camera: %s", err.Error())
|
||||
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
|
||||
}
|
||||
|
||||
// GetProperty returns the specified camera property.
|
||||
func (c *Camera) GetProperty(id int) float64 {
|
||||
ret, _ := c.camera.GetControl(uint32(id))
|
||||
return float64(ret)
|
||||
}
|
||||
|
||||
// SetProperty sets a camera property.
|
||||
func (c *Camera) SetProperty(id int, value float64) {
|
||||
c.camera.SetControl(uint32(id), int32(value))
|
||||
}
|
||||
|
||||
// Close closes camera.
|
||||
func (c *Camera) Close() (err error) {
|
||||
if c.camera == nil {
|
||||
err = fmt.Errorf("camera: camera is not opened")
|
||||
return
|
||||
}
|
||||
|
||||
c.camera.TurnOff()
|
||||
|
||||
c.camera.Close()
|
||||
c.camera = nil
|
||||
return
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
func TestCamera(t *testing.T) {
|
||||
camera, err := New(1)
|
||||
camera, err := New(Options{0, 0, 640, 480})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -25,18 +25,6 @@ func TestCamera(t *testing.T) {
|
||||
|
||||
defer os.RemoveAll(tmpdir)
|
||||
|
||||
var width, height float64 = 640, 480
|
||||
camera.SetProperty(PropFrameWidth, width)
|
||||
camera.SetProperty(PropFrameHeight, height)
|
||||
|
||||
if camera.GetProperty(PropFrameWidth) != width {
|
||||
t.Error("FrameWidth not correct")
|
||||
}
|
||||
|
||||
if camera.GetProperty(PropFrameHeight) != height {
|
||||
t.Error("FrameHeight not correct")
|
||||
}
|
||||
|
||||
var i int
|
||||
var n int = 10
|
||||
|
||||
|
||||
Reference in New Issue
Block a user