-
Notifications
You must be signed in to change notification settings - Fork 1
/
ocrrr.go
108 lines (87 loc) · 2.24 KB
/
ocrrr.go
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
108
package ocr
import (
"fmt"
"github.com/Ericwyn/EzeTranslate/log"
"github.com/Ericwyn/GoTools/file"
"github.com/Ericwyn/GoTools/shell"
"math"
"os"
"os/user"
"strings"
"time"
)
var OCRTextTempPath = ""
var ScrPng = OCRTextTempPath + ".png"
var initFlag = false
func getOcrTextTempPath() string {
if OCRTextTempPath != "" {
return OCRTextTempPath
}
current, err := user.Current()
if err != nil {
return ""
}
homePath := current.HomeDir
log.D("homePath :", homePath)
//"/home/mi/Pictures/ocr-temp"
picPath := homePath + "/Pictures/"
log.D("picPath :", picPath)
dir := file.OpenFile(picPath)
if !dir.IsDir() {
dir.Mkdirs()
}
OCRTextTempPath = picPath + "/ocr-temp"
log.D("OCRTextTempPath :", OCRTextTempPath)
return OCRTextTempPath
}
// RunOcr 运行 OCR 识别文字并返回
func RunOcr() (string, bool) {
if !initFlag {
ocrResTempFile := file.OpenFile(getOcrTextTempPath())
if !ocrResTempFile.IsFile() {
ocrResTempFile.CreateFile()
_ = ocrResTempFile.Close()
}
initFlag = true
}
shell.Debug(true)
shell.RunShellRes("gnome-screenshot", "-a", "-f", ScrPng)
// 获取文件修改时间,看看是不是1s内修改的
modTime := GetFileModTime(ScrPng)
if math.Abs((float64)(modTime-time.Now().Unix())) > 10 {
fmt.Println("文件未修改,不做识别")
return "", false
}
shell.RunShellRes("mogrify", "-modulate", "100,0", "-resize", "400%", ScrPng)
tranRes := shell.RunShellRes("tesseract", ScrPng, "stdout", "-l", "eng")
tranRes = strings.Trim(tranRes, " ")
tranRes = strings.Trim(tranRes, "\n")
tranRes = strings.Trim(tranRes, "\r")
tranRes = strings.Trim(tranRes, "\n\f")
//fmt.Println(tranRes)
//
//// 复制内容到剪切板
//err := clipboard.WriteAll(tranRes)
//
//if err != nil {
// shell.RunShellRes("notify-send", "设置剪贴板错误")
//} else {
// shell.RunShellRes("notify-send", "OCRTextTempPath 成功")
//}
return tranRes, true
}
// GetFileModTime 获取文件修改时间 返回unix时间戳
func GetFileModTime(path string) int64 {
f, err := os.Open(path)
if err != nil {
log.E("open file error")
return time.Now().Unix()
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
log.E("stat fileinfo error")
return time.Now().Unix()
}
return fi.ModTime().Unix()
}