entry.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package main
  2. import (
  3. "fmt"
  4. "image"
  5. "image/png"
  6. "os"
  7. "os/exec"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/kbinani/screenshot"
  12. )
  13. // GetWindowGeometry ...
  14. // xdotool search --name "Yu-Gi-Oh! Duel Links" A besoin en parametre le resultat de cette commande
  15. func GetWindowGeometry(nwin string) image.Rectangle {
  16. win := image.Rectangle{}
  17. cmd := exec.Command("/bin/xdotool", "getwindowgeometry", nwin)
  18. out, err := cmd.Output()
  19. if err == nil {
  20. strOut := string(out)
  21. posStartPos := strings.Index(strOut, "Position") + len("Position: ")
  22. posMidPos := strings.Index(strOut, ",")
  23. posEndPos := strings.Index(strOut[posMidPos:], " (") + posMidPos
  24. win.Min.X, _ = strconv.Atoi(strOut[posStartPos:posMidPos])
  25. win.Min.Y, _ = strconv.Atoi(strOut[posMidPos+1 : posEndPos])
  26. posStartGeo := strings.Index(strOut, "Geometry") + len("Geometry: ")
  27. posMidGeo := strings.Index(strOut, "x")
  28. posEndGeo := len(string(out)) - 1
  29. win.Max.X, _ = strconv.Atoi(strOut[posStartGeo:posMidGeo])
  30. win.Max.Y, _ = strconv.Atoi(strOut[posMidGeo+1 : posEndGeo])
  31. }
  32. return win
  33. }
  34. // ScreenShoot ...
  35. func ScreenShoot(rect image.Rectangle) string {
  36. n := screenshot.NumActiveDisplays()
  37. for i := 0; i < n; i++ {
  38. img, err := screenshot.CaptureRect(rect)
  39. if err != nil {
  40. panic(err)
  41. }
  42. fileName := fmt.Sprintf("%d_%dx%d.png", i, rect.Dx(), rect.Dy())
  43. file, _ := os.Create(fileName)
  44. defer file.Close()
  45. png.Encode(file, img)
  46. return fileName
  47. }
  48. return ""
  49. }
  50. // screenshoot [nwin]
  51. func main() {
  52. time.Sleep(15)
  53. rect := GetWindowGeometry(os.Args[1])
  54. filename := ScreenShoot(rect)
  55. if filename == "" {
  56. panic("ScreenShoot Error...\n")
  57. } else {
  58. fmt.Println(filename)
  59. }
  60. }