-
Notifications
You must be signed in to change notification settings - Fork 0
/
capture.go
136 lines (121 loc) · 3.23 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"encoding/json"
"fmt"
"image"
"io/fs"
"math"
"os"
"path/filepath"
"github.com/chai2010/webp"
"github.com/kbinani/screenshot"
_ "github.com/mattn/go-sqlite3"
"github.com/nfnt/resize"
"github.com/sirupsen/logrus"
)
var IS_DUMP = true
type ScreenCapturer struct {
binPath string
}
func (cap *ScreenCapturer) CaptureImage() (*image.RGBA, error) {
minX, minY, maxX, maxY := getScreenBounds()
bounds := image.Rectangle{
Min: image.Point{
X: minX,
Y: minY,
},
Max: image.Point{
X: maxX,
Y: maxY,
},
}
img, err := screenshot.CaptureRect(bounds)
if err != nil {
return nil, err
}
return img, nil
}
func (cap *ScreenCapturer) GetScreenInfo() ([]*ScreenWindow, error) {
return grabScreenInfo()
}
func (cap *ScreenCapturer) LoadDump(dumpFilePath string) (string, []image.Rectangle, []*ScreenWindow, error) {
b, err := os.ReadFile(dumpFilePath)
if err != nil {
logrus.Error("LoadDump read")
return "", nil, nil, err
}
fd := &FullDump{}
err = json.Unmarshal(b, fd)
if err != nil {
logrus.Error("LoadDump read")
return "", nil, nil, err
}
sws, err := cap.extractScreenWindow([]byte(fd.ScreenWindowsRaw))
if err != nil {
logrus.Error("LoadDump extractScreenWindowDump")
return "", nil, nil, err
}
// screens := make([]image.Rectangle, 0)
// for _, sw := range sws {
// screens = append(screens, sw.Bounds.ToRectangle())
// }
return fd.ImagePath, fd.Screens, sws, nil
}
func (cap *ScreenCapturer) SaveDump(dumpFilePath string, imagePath string, screens []image.Rectangle, rejections []string) error {
d, err := json.Marshal(&FullDump{
ImagePath: imagePath,
Screens: screens,
})
if err != nil {
logrus.Errorf("SaveDump json %v", err)
return err
}
err = os.WriteFile(dumpFilePath, d, fs.ModePerm)
if err != nil {
logrus.Errorf("SaveDump write %v", err)
return err
}
logrus.Info("y ->", dumpFilePath)
return nil
}
func (cap *ScreenCapturer) extractScreenWindow(raw []byte) ([]*ScreenWindow, error) {
windows := make([]*ScreenWindow, 0)
if err := json.Unmarshal(raw, &windows); err != nil {
return nil, err
}
return windows, nil
}
func (cap *ScreenCapturer) ImageFromWebp(imgPath string) (image.Image, error) {
// Open the PNG image file
file, err := os.Open(imgPath) // THIS IS WEBP deal with that...
if err != nil {
return nil, fmt.Errorf("Error opening the file: %s with err %v", imgPath, err)
}
defer file.Close()
wbp, err := webp.Decode(file)
if err != nil {
return nil, err
}
return wbp, nil
}
func (cap *ScreenCapturer) SaveWebp(fullpath string, filename string, img *image.RGBA) (string, error) {
fullFilePath := filepath.Join(fullpath, "/", filename)
err := os.MkdirAll(fullpath, os.ModePerm)
if err != nil {
return "", err
}
file, err := os.Create(fullFilePath)
if err != nil {
return "", err
}
defer file.Close()
// Encode lossless webp
imageWidth := math.Abs(float64(img.Bounds().Min.X)) + math.Abs(float64(img.Bounds().Max.X))
resizedWidth := math.Round(imageWidth * 0.60) // RESIZING breaks all glimpse logic on dumps
newImage := resize.Resize(uint(resizedWidth), 0, img, resize.Lanczos3)
err = webp.Encode(file, newImage, &webp.Options{Lossless: false, Quality: 60, Exact: false})
if err != nil {
return "", err
}
return fullFilePath, nil
}