Screen capture in Go?

Is there a cross-platform way to capture screen in Google Go? Or any option, but a cross platform would be preferable.

+12
go
source share
7 answers

Now there are:

https://github.com/vova616/screenshot

go get github.com/vova616/screenshot

Example:

 package main import "github.com/vova616/screenshot" func main() { img, err := screenshot.CaptureScreen() // *image.RGBA myImg := image.Image(img) // can cast to image.Image, but not necessary } 

If you also need macOS support (before combining the receive request), get:

https://github.com/kesarion/screenshot

+7
source share

Unfortunately, there is no library for this. There are several bindings for magickwand (the C programming language and ImageMagick image processing libraries), see http://go-lang.cat-v.org/library-bindings , but they are incomplete and do not have a screen capture function.

Meanwhile, as GeertJohan suggested, you can use os.exec to launch an external program and capture the screen (see code example below). For example, you can use the import command from imagemagick to capture a screen (should work on a platform that can run imagemagick)

 package main import ( "bytes" "fmt" "log" "os/exec" ) func main() { var buf bytes.Buffer path, err := exec.LookPath("import") if err != nil { log.Fatal("import not installed !") } fmt.Printf("import is available at %s\n", path) cmd := exec.Command("import", "-window", "root", "root.png") cmd.Stdout = &buf cmd.Stderr = &buf err = cmd.Run() if err != nil { panic(err) } fmt.Println(buf.String()) } 
+6
source share

I do not know any cross-platform library, but you can do this using the xgbutil library when the X server is present. You can see an example of how to take a screenshot here .

If you want this to work on Mac / Windows systems, I would probably start by exploring the source go.wde , which includes servers for Windows and Mac. I doubt that you will immediately find the code to capture the screenshot, but it may give you some hints or a path to the next.

+1
source share

There is no cross-platform way to capture the screen in Google Go, because screen capture depends on the specific API of the underlying operating systems. But there are libraries for Go that do this.

For example https://github.com/vova616/screenshot

+1
source share

I can not find a library to do this. Stable cross-platform screen capture requires a lot of work. Capturing a screen requires interaction with the display manager / operating system server or frame buffer, which is different for many Linux operating systems and distributions. You will have to write interfaces for each OS API (or wrap libraries that provide functionality), and then abstract all the different methods in one package so that it works cross-platform.

Another way to do this is to run an existing screen capture application (command line) to do screen capture work, including saving to a file. Then read the file in your application. For the Go application to run a third-party application, use the os / exec package, it is in the standard library. For Linux, you can use fbgrab to save the frame buffer to a png file.

0
source share

It. This is a two-step process:

Check out https://github.com/ShareX/ShareX/tree/master/ShareX.ScreenCaptureLib to find out what win32 API calls you need to make to capture a screen / window. Translate this logic to go. You can use one of the few existing Go bindings for win32 api Go (e.g. https://github.com/AllenDang/w32 ). If they lack the necessary functionality, you can add more wrappers.

0
source share

This library seems to meet your needs: https://godoc.org/github.com/kbinani/screenshot

captures screen-shot image as image.RGBA. Mac, Windows, Linux, FreeBSD, OpenBSD, NetBSD, and Solaris are supported.

 func Capture(x, y, width, height int) (*image.RGBA, error) func CaptureDisplay(displayIndex int) (*image.RGBA, error) func CaptureRect(rect image.Rectangle) (*image.RGBA, error) func GetDisplayBounds(displayIndex int) image.Rectangle func NumActiveDisplays() int 
0
source share

All Articles