screenshot.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/pkg/errors"
  7. )
  8. func screenshotWithMinicap(filename, thumbnailSize string) (err error) {
  9. output, err := runShellOutput("LD_LIBRARY_PATH=/data/local/tmp", "/data/local/tmp/minicap", "-i")
  10. if err != nil {
  11. return
  12. }
  13. var f MinicapInfo
  14. if er := json.Unmarshal([]byte(output), &f); er != nil {
  15. err = fmt.Errorf("minicap not supported: %v", er)
  16. return
  17. }
  18. if thumbnailSize == "" {
  19. thumbnailSize = fmt.Sprintf("%dx%d", f.Width, f.Height)
  20. }
  21. if _, err = runShell(
  22. "LD_LIBRARY_PATH=/data/local/tmp",
  23. "/data/local/tmp/minicap",
  24. "-P", fmt.Sprintf("%dx%d@%s/%d", f.Width, f.Height, thumbnailSize, f.Rotation),
  25. "-s", ">"+filename); err != nil {
  26. err = errors.Wrap(err, "minicap")
  27. return
  28. }
  29. return nil
  30. }
  31. func screenshotWithScreencap(filename string) (err error) {
  32. _, err = runShellOutput("screencap", "-p", filename)
  33. err = errors.Wrap(err, "screencap")
  34. return
  35. }
  36. func isMinicapSupported() bool {
  37. output, err := runShellOutput("LD_LIBRARY_PATH=/data/local/tmp", "/data/local/tmp/minicap", "-i")
  38. if err != nil {
  39. return false
  40. }
  41. var f MinicapInfo
  42. if er := json.Unmarshal([]byte(output), &f); er != nil {
  43. return false
  44. }
  45. output, err = runShell(
  46. "LD_LIBRARY_PATH=/data/local/tmp",
  47. "/data/local/tmp/minicap",
  48. "-P", fmt.Sprintf("%dx%d@%dx%d/%d", f.Width, f.Height, f.Width, f.Height, f.Rotation),
  49. "-s", "2>/dev/null")
  50. if err != nil {
  51. return false
  52. }
  53. return bytes.Equal(output[:2], []byte("\xff\xd8")) // JpegFormat
  54. }