-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcamera.go
More file actions
84 lines (71 loc) · 1.66 KB
/
Copy pathcamera.go
File metadata and controls
84 lines (71 loc) · 1.66 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
package camera
import (
"fmt"
"os/exec"
"path/filepath"
"strconv"
"time"
)
const (
still = "libcamera-still"
vflip = "--vflip"
timeout = "-t"
width = "--width"
height = "--height"
output = "-o"
filetype = ".jpg"
timestamp = "2006-01-02_15:04:05"
defaultTimeout = 1
)
type resolution struct {
width int
height int
}
// Camera with params
type Camera struct {
timeout int
resolution resolution
path string
}
// New Camera with path
func New(path string, width int, height int) *Camera {
return &Camera{defaultTimeout, resolution{width, height}, path}
}
func makeArgs(c *Camera) []string {
args := make([]string, 0)
args = append(args, timeout)
args = append(args, strconv.Itoa(c.timeout))
args = append(args, vflip)
args = append(args, strconv.Itoa(1))
args = append(args, width)
args = append(args, strconv.Itoa(c.resolution.width))
args = append(args, height)
args = append(args, strconv.Itoa(c.resolution.height))
args = append(args, output)
args = append(args, filepath.Join(c.path, getFilename()))
return args
}
func getFilename() string {
return time.Now().Format(timestamp) + filetype
}
// Capture an image or a timelapse
func (c *Camera) Capture() (string, error) {
args := makeArgs(c)
fullPath := args[len(args)-1]
cmd := exec.Command(still, args...)
_, err := cmd.StdoutPipe()
if err != nil {
fmt.Println("error on camera command line 1: ", err)
return fullPath, err
}
err = cmd.Start()
if err != nil {
fmt.Println("error on camera command line 2: ", err)
return fullPath, err
}
err = cmd.Wait()
if err != nil {
fmt.Println("error on camera command line 3: ", err)
}
return fullPath, nil
}