From 2fbd770917340ccc7f3874d900744f7bf407c3d3 Mon Sep 17 00:00:00 2001 From: vcaesar Date: Wed, 8 Aug 2018 09:19:17 -0400 Subject: [PATCH] add simple ocr support --- robotgo.go | 20 ++++++++++++++++++++ robotgo_ocr.go | 26 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 robotgo_ocr.go diff --git a/robotgo.go b/robotgo.go index d0148e9..51233fa 100644 --- a/robotgo.go +++ b/robotgo.go @@ -61,6 +61,7 @@ import ( "time" "unsafe" // "syscall" + "os/exec" "github.com/go-vgo/robotgo/clipboard" "github.com/shirou/gopsutil/process" @@ -786,6 +787,25 @@ func TocharBitmap(bit C.MMBitmapRef) *C.char { return strBit } +// GetText get the image text by tesseract ocr +func GetText(imgPath string, args ...string) (string, error) { + var lang = "eng" + + if len(args) > 0 { + lang = args[0] + if lang == "zh" { + lang = "chi_sim" + } + } + + body, err := exec.Command("tesseract", imgPath, + "stdout", "-l", lang).Output() + if err != nil { + return "", err + } + return string(body), nil +} + func internalFindBitmap(bit, sbit C.MMBitmapRef, tolerance float64) (int, int) { pos := C.find_bitmap(bit, sbit, C.float(tolerance)) // fmt.Println("pos----", pos) diff --git a/robotgo_ocr.go b/robotgo_ocr.go new file mode 100644 index 0000000..dab34cf --- /dev/null +++ b/robotgo_ocr.go @@ -0,0 +1,26 @@ +// +build ocr + +package robotgo + +import ( + "github.com/otiai10/gosseract" +) + +// GetText get the image text by tesseract ocr +func GetText(imgPath string, args ...string) (string, error) { + var lang = "eng" + + if len(args) > 0 { + lang = args[0] + if lang == "zh" { + lang = "chi_sim" + } + } + + client := gosseract.NewClient() + defer client.Close() + + client.SetImage(imgPath) + client.SetLanguage(lang) + return client.Text() +}