Procházet zdrojové kódy

Add project screenshoot with xdotool

Sylvrec před 5 roky
rodič
revize
7aa1fe02ce
2 změnil soubory, kde provedl 68 přidání a 0 odebrání
  1. 65 0
      screenshoot/entry.go
  2. 3 0
      screenshoot/script.sh

+ 65 - 0
screenshoot/entry.go

@@ -0,0 +1,65 @@
+package main
+
+import (
+	"fmt"
+	"image"
+	"image/png"
+	"os"
+	"os/exec"
+	"strconv"
+	"strings"
+	"time"
+
+	"github.com/kbinani/screenshot"
+)
+
+// GetWindowGeometry ...
+// xdotool search --name "Yu-Gi-Oh! Duel Links" A besoin en parametre le resultat de cette commande
+func GetWindowGeometry(nwin string) image.Rectangle {
+	win := image.Rectangle{}
+	cmd := exec.Command("/bin/xdotool", "getwindowgeometry", nwin)
+	out, err := cmd.Output()
+	if err == nil {
+		strOut := string(out)
+		posStartPos := strings.Index(strOut, "Position") + len("Position: ")
+		posMidPos := strings.Index(strOut, ",")
+		posEndPos := strings.Index(strOut[posMidPos:], " (") + posMidPos
+		win.Min.X, _ = strconv.Atoi(strOut[posStartPos:posMidPos])
+		win.Min.Y, _ = strconv.Atoi(strOut[posMidPos+1 : posEndPos])
+		posStartGeo := strings.Index(strOut, "Geometry") + len("Geometry: ")
+		posMidGeo := strings.Index(strOut, "x")
+		posEndGeo := len(string(out)) - 1
+		win.Max.X, _ = strconv.Atoi(strOut[posStartGeo:posMidGeo])
+		win.Max.Y, _ = strconv.Atoi(strOut[posMidGeo+1 : posEndGeo])
+	}
+	return win
+}
+
+// ScreenShoot ...
+func ScreenShoot(rect image.Rectangle) string {
+	n := screenshot.NumActiveDisplays()
+	for i := 0; i < n; i++ {
+		img, err := screenshot.CaptureRect(rect)
+		if err != nil {
+			panic(err)
+		}
+		fileName := fmt.Sprintf("%d_%dx%d.png", i, rect.Dx(), rect.Dy())
+		file, _ := os.Create(fileName)
+		defer file.Close()
+		png.Encode(file, img)
+		return fileName
+	}
+	return ""
+}
+
+// screenshoot [nwin]
+func main() {
+	time.Sleep(15)
+	rect := GetWindowGeometry(os.Args[1])
+	filename := ScreenShoot(rect)
+	if filename == "" {
+		panic("ScreenShoot Error...\n")
+	} else {
+		fmt.Println(filename)
+	}
+}

+ 3 - 0
screenshoot/script.sh

@@ -0,0 +1,3 @@
+#!/bin/bash
+
+/usr/sbin/xdotool search --pid 61779 focus getwindowgeometry