add simple ocr support

This commit is contained in:
vcaesar 2018-08-08 09:19:17 -04:00
parent 530b8d6960
commit 2fbd770917
2 changed files with 46 additions and 0 deletions

View File

@ -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)

26
robotgo_ocr.go Normal file
View File

@ -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()
}