Friday, May 17, 2024
HomeGolangScreenshot with wayland - Getting Assist

Screenshot with wayland – Getting Assist


Hey guys,
I wish to seize display with golang.
All the things in X11 is okay. However when in wayland, I obtained black display
I attempt to seize display by way of DBUS. It captured display
However I would like seize display and save to file in identical folder (with out flash + popup)

Please assist me repair it
That is my code I utilizing

package deal essential

import (
	"fmt"
	"io"
	"log"
	"web/http"
	"os"

	"github.com/godbus/dbus"
)

func essential() {
	conn, err := dbus.SessionBus()
	if err != nil {
		log.Fatalf("Failed to hook up with session bus: %v", err)
	}
	defer conn.Shut()

	dest := "org.freedesktop.portal.Desktop"
	path := "/org/freedesktop/portal/desktop"
	iface := "org.freedesktop.portal.Screenshot"
	choices := map[string]dbus.Variant{
		"Path":  dbus.MakeVariant("/house/capture-wayland/screenshot.png"),
		"Flash": dbus.MakeVariant(false),
	}

	obj := conn.Object(dest, dbus.ObjectPath(path))
	caller := obj.Name(iface+".Screenshot", 0, "", choices)

	fmt.Println(caller)
	fmt.Println(caller.Path)
	fmt.Println(caller.Vacation spot)
	fmt.Println(caller.Methodology)
	fmt.Println(caller.Args)
	var response map[string]dbus.Variant
	// caller.Retailer(&response)
	// fmt.Println("response:", response)
	// a, _ := json.Marshal(caller)
	// fmt.Println("caller string:", string(a))
	_ = obj.Name(iface+".Screenshot", 1, "", choices).Retailer(&response)
	fmt.Println(response)
	if err != nil {
		log.Fatalf("Didn't name Screenshot methodology: %v", err)
	}

	uri, okay := response["uri"]
	if !okay {
		log.Deadly("No URI discovered within the response")
	}

	// Obtain the picture from the URI and reserve it to picture.png
	downloadAndSave(uri.Worth().(string), "picture.png")
}

func downloadAndSave(url, filename string) {
	resp, err := http.Get(url)
	if err != nil {
		log.Fatalf("Didn't obtain the picture: %v", err)
	}
	defer resp.Physique.Shut()

	out, err := os.Create(filename)
	if err != nil {
		log.Fatalf("Didn't create the picture file: %v", err)
	}
	defer out.Shut()

	_, err = io.Copy(out, resp.Physique)
	if err != nil {
		log.Fatalf("Failed to save lots of the picture: %v", err)
	}
	fmt.Println("Saved screenshot to", filename)
}

A distinct route than by means of dbus could be to make use of this library, which I’ve efficiently used on each Home windows and Darwin so I’m unsure it really works on X or Wayland.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments