-
Notifications
You must be signed in to change notification settings - Fork 0
/
capture.go
85 lines (71 loc) · 2.11 KB
/
capture.go
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
85
package main
import (
"errors"
"image"
"image/png"
"os"
"path/filepath"
"strconv"
"time"
"github.com/google/uuid"
"github.com/kbinani/screenshot"
)
var ocr ocrType
type metaType struct {
dateTime time.Time
fileName string
hOcrText string
ocrText string
resolution string
sessionUuid string
}
type captureType struct {
image *image.RGBA
width int
height int
meta metaType
}
type captureOptionsType struct {
interval uint
ocrLanguages string
}
func (capture *captureType) start() {
if screenshot.NumActiveDisplays() <= 0 {
helper.catch(errors.New("No active display found."))
}
displayBounds := screenshot.GetDisplayBounds(0)
capture.width = displayBounds.Dx()
capture.height = displayBounds.Dy()
capture.meta.resolution = strconv.Itoa(capture.width) + "x" + strconv.Itoa(capture.height)
capture.meta.sessionUuid = uuid.New().String()
intervalDuration := time.Duration(captureOptions.interval) * time.Second
for {
go capture.capture()
time.Sleep(intervalDuration)
}
}
func (capture *captureType) capture() {
var err error
capture.image, err = screenshot.Capture(0, 0, capture.width, capture.height)
helper.catch(err)
capture.meta.dateTime = time.Now()
capture.meta.hOcrText = ocr.getHocrText()
capture.saveToFile()
capture.saveToDatabase()
}
func (capture *captureType) saveToFile() {
capture.meta.fileName = capture.meta.dateTime.Format("2006-01-02-15-04-05") + ".png"
imgPath := helper.getCaptureSubDirs(capture.meta.fileName)
imgPath = filepath.Join(imgPath, capture.meta.fileName)
file, err := os.Create(imgPath)
helper.catch(err)
defer file.Close()
helper.catch(png.Encode(file, capture.image))
}
func (capture *captureType) saveToDatabase() {
statement, err := database.connection.Prepare("INSERT INTO captures (capture_date_time, capture_file, capture_resolution, capture_interval, session_uuid, hocr_text) VALUES(?, ?, ?, ?, ?, ?);")
helper.catch(err)
defer statement.Close()
_, err = statement.Exec(capture.meta.dateTime, capture.meta.fileName, capture.meta.resolution, captureOptions.interval, capture.meta.sessionUuid, capture.meta.hOcrText)
helper.catch(err)
}