-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
107 lines (87 loc) · 2.18 KB
/
main.go
File metadata and controls
107 lines (87 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"bytes"
"fmt"
"image/png"
"io/ioutil"
"os"
"github.com/atotto/clipboard"
"github.com/kbinani/screenshot"
vision "cloud.google.com/go/vision/apiv1"
"golang.org/x/net/context"
)
func captureScreenshot() (string, error) {
screenRect := screenshot.GetDisplayBounds(0)
img, err := screenshot.CaptureRect(screenRect)
if err != nil {
return "", fmt.Errorf("error capturing screenshot: %v", err)
}
tempFile, err := ioutil.TempFile("", "screenshot-*.png")
if err != nil {
return "", fmt.Errorf("error creating temp file: %v", err)
}
err = png.Encode(tempFile, img)
if err != nil {
return "", fmt.Errorf("error encoding image: %v", err)
}
tempImagePath := tempFile.Name()
tempFile.Close()
return tempImagePath, nil
}
func detectText(imagePath string) (string, error) {
if imagePath == "" {
return "", nil
}
ctx := context.Background()
client, err := vision.NewImageAnnotatorClient(ctx)
if err != nil {
return "", fmt.Errorf("error creating image annotator client: %v", err)
}
imageData, err := ioutil.ReadFile(imagePath)
if err != nil {
return "", fmt.Errorf("error reading image file: %v", err)
}
image, err := vision.NewImageFromReader(bytes.NewReader(imageData))
annotations, err := client.DetectTexts(ctx, image, nil, 10)
if err != nil {
return "", fmt.Errorf("error detecting text: %v", err)
}
if len(annotations) == 0 {
return "", nil
}
return annotations[0].Description, nil
}
func copyToClipboard(text string) error {
if text == "" {
return nil
}
err := clipboard.WriteAll(text)
if err != nil {
return fmt.Errorf("error copying text to clipboard: %v", err)
}
return nil
}
func main() {
imagePath, err := captureScreenshot()
if err != nil {
fmt.Printf("Error capturing screenshot: %v\n", err)
return
}
text, err := detectText(imagePath)
if err != nil {
fmt.Printf("Error detecting text: %v\n", err)
return
}
err = copyToClipboard(text)
if err != nil {
fmt.Printf("Error copying text to clipboard: %v\n", err)
} else {
fmt.Println("Text copied to clipboard.")
}
if imagePath != "" {
err = os.Remove(imagePath)
if err != nil {
fmt.Printf("Error removing temporary image file: %v\n", err)
}
}
}