-
Notifications
You must be signed in to change notification settings - Fork 1
/
screenshot_linux.go
40 lines (32 loc) · 1023 Bytes
/
screenshot_linux.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
package screenshot
import (
"image"
"github.com/BurntSushi/xgb/xproto"
)
func (ss *Start) ScreenRect() (image.Rectangle, error) {
screen := xproto.Setup(ss.XgbConn).DefaultScreen(ss.XgbConn)
x := screen.WidthInPixels
y := screen.HeightInPixels
return image.Rect(0, 0, int(x), int(y)), nil
}
func (ss *Start) CaptureScreen() (*image.RGBA, error) {
r, e := ss.ScreenRect()
if e != nil {
return nil, e
}
return ss.CaptureRect(r)
}
func (ss *Start) CaptureRect(rect image.Rectangle) (*image.RGBA, error) {
screen := xproto.Setup(ss.XgbConn).DefaultScreen(ss.XgbConn)
x, y := rect.Dx(), rect.Dy()
xImg, err := xproto.GetImage(ss.XgbConn, xproto.ImageFormatZPixmap, xproto.Drawable(screen.Root), int16(rect.Min.X), int16(rect.Min.Y), uint16(x), uint16(y), 0xffffffff).Reply()
if err != nil {
return nil, err
}
data := xImg.Data
for i := 0; i < len(data); i += 4 {
data[i], data[i+2], data[i+3] = data[i+2], data[i], 255
}
img := &image.RGBA{data, 4 * x, image.Rect(0, 0, x, y)}
return img, nil
}