Add timestamp option

This commit is contained in:
Milan Nikolic
2019-10-06 05:48:12 +02:00
parent 1c342a67df
commit adf2a742e6
11 changed files with 98 additions and 40 deletions

View File

@@ -2,8 +2,9 @@ package camera
// Options.
type Options struct {
Index int
Rotate int
Width float64
Height float64
Index int
Rotate int
Width float64
Height float64
Timestamp bool
}

View File

@@ -6,6 +6,11 @@ package camera
import (
"fmt"
"image"
"image/color"
"image/draw"
"time"
"github.com/pbnjay/pixfont"
"github.com/disintegration/imaging"
"github.com/gen2brain/go-opencv/opencv"
@@ -36,29 +41,37 @@ func New(opts Options) (camera *Camera, err error) {
// Read reads next frame from camera and returns image.
func (c *Camera) Read() (img image.Image, err error) {
if c.camera.GrabFrame() {
c.frame = c.camera.RetrieveFrame(1)
if c.frame == nil {
err = fmt.Errorf("camera: can not retrieve frame")
return
}
img = c.frame.ToImage()
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)
}
} else {
if !c.camera.GrabFrame() {
err = fmt.Errorf("camera: can not grab frame")
return
}
c.frame = c.camera.RetrieveFrame(1)
if c.frame == nil {
err = fmt.Errorf("camera: can not retrieve frame")
return
}
img = c.frame.ToImage()
switch c.opts.Rotate {
case 90:
img = imaging.Rotate90(img)
case 180:
img = imaging.Rotate180(img)
case 270:
img = imaging.Rotate270(img)
}
if c.opts.Timestamp {
dimg, ok := img.(draw.Image)
if !ok {
err = fmt.Errorf("camera: %T is not a drawable image type", img)
return
}
pixfont.DrawString(dimg, 10, 10, time.Now().Format("2006-01-02 15:04:05"), color.White)
img = dimg
}
return

View File

@@ -6,6 +6,11 @@ package camera
import (
"fmt"
"image"
"image/color"
"image/draw"
"time"
"github.com/pbnjay/pixfont"
"github.com/disintegration/imaging"
"gocv.io/x/gocv"
@@ -56,10 +61,6 @@ func (c *Camera) Read() (img image.Image, err error) {
return
}
if c.opts.Rotate == 0 {
return
}
switch c.opts.Rotate {
case 90:
img = imaging.Rotate90(img)
@@ -69,6 +70,17 @@ func (c *Camera) Read() (img image.Image, err error) {
img = imaging.Rotate270(img)
}
if c.opts.Timestamp {
dimg, ok := img.(draw.Image)
if !ok {
err = fmt.Errorf("camera: %T is not a drawable image type", img)
return
}
pixfont.DrawString(dimg, 10, 10, time.Now().Format("2006-01-02 15:04:05"), color.White)
img = dimg
}
return
}

View File

@@ -6,10 +6,14 @@ package camera
import (
"fmt"
"image"
"image/color"
"image/draw"
"time"
"github.com/disintegration/imaging"
"github.com/korandiz/v4l"
"github.com/korandiz/v4l/fmt/mjpeg"
"github.com/pbnjay/pixfont"
im "github.com/gen2brain/cam2ip/image"
)
@@ -82,10 +86,6 @@ func (c *Camera) Read() (img image.Image, err error) {
return
}
if c.opts.Rotate == 0 {
return
}
switch c.opts.Rotate {
case 90:
img = imaging.Rotate90(img)
@@ -95,6 +95,17 @@ func (c *Camera) Read() (img image.Image, err error) {
img = imaging.Rotate270(img)
}
if c.opts.Timestamp {
dimg, ok := img.(draw.Image)
if !ok {
err = fmt.Errorf("camera: %T is not a drawable image type", img)
return
}
pixfont.DrawString(dimg, 10, 10, time.Now().Format("2006-01-02 15:04:05"), color.White)
img = dimg
}
return
}

View File

@@ -7,9 +7,14 @@ import (
"bytes"
"fmt"
"image"
"image/color"
"image/draw"
"syscall"
"time"
"unsafe"
"github.com/pbnjay/pixfont"
"github.com/disintegration/imaging"
)
@@ -129,9 +134,6 @@ func (c *Camera) Read() (img image.Image, err error) {
}
img = c.frame
if c.opts.Rotate == 0 {
return
}
switch c.opts.Rotate {
case 90:
@@ -142,6 +144,17 @@ func (c *Camera) Read() (img image.Image, err error) {
img = imaging.Rotate270(img)
}
if c.opts.Timestamp {
dimg, ok := img.(draw.Image)
if !ok {
err = fmt.Errorf("camera: %T is not a drawable image type", img)
return
}
pixfont.DrawString(dimg, 10, 10, time.Now().Format("2006-01-02 15:04:05"), color.White)
img = dimg
}
return
}