clipboard_unix.go 759 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //go:build !windows
  2. package image
  3. import (
  4. "bytes"
  5. "fmt"
  6. "image"
  7. "github.com/atotto/clipboard"
  8. )
  9. func GetImageFromClipboard() ([]byte, string, error) {
  10. text, err := clipboard.ReadAll()
  11. if err != nil {
  12. return nil, "", fmt.Errorf("Error reading clipboard")
  13. }
  14. if text == "" {
  15. return nil, "", nil
  16. }
  17. binaryData := []byte(text)
  18. imageBytes, err := binaryToImage(binaryData)
  19. if err != nil {
  20. return nil, text, nil
  21. }
  22. return imageBytes, "", nil
  23. }
  24. func binaryToImage(data []byte) ([]byte, error) {
  25. reader := bytes.NewReader(data)
  26. img, _, err := image.Decode(reader)
  27. if err != nil {
  28. return nil, fmt.Errorf("Unable to covert bytes to image")
  29. }
  30. return ImageToBytes(img)
  31. }
  32. func min(a, b int) int {
  33. if a < b {
  34. return a
  35. }
  36. return b
  37. }